unjs / ipx

🖼️ High performance, secure and easy-to-use image optimizer.
MIT License
1.54k stars 61 forks source link

Can't use ipx in serverMiddleware with @nuxt/bridge #50

Closed nathanchase closed 2 years ago

nathanchase commented 2 years ago

When adding /server/middleware/ipx.js to nuxt.config.js (as indicated at https://image.nuxtjs.org/providers/ipx/#advanced-custom-servermiddleware) while using @nuxt/bridge...

import { createIPX, createIPXMiddleware } from 'ipx'
const ipx = createIPX({
  dir: '',
  domains: [],
  alias: {},
  sharp: {},
})
export default createIPXMiddleware(ipx)

it results in an error:

 WARN  [worker] Named export 'stat' not found. The requested module 'fs-extra' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'fs-extra';
const { stat, readFile } = pkg;

  import { stat, readFile } from 'fs-extra';
  ^^^^
  SyntaxError: Named export 'stat' not found. The requested module 'fs-extra' is a CommonJS module, which may not support all module.exports as named exports.
  CommonJS modules can always be imported via the default export, for example using:

  import pkg from 'fs-extra';
  const { stat, readFile } = pkg;

  at ModuleJob._instantiate (internal/modules/esm/module_job.js:121:21)
  at async ModuleJob.run (internal/modules/esm/module_job.js:166:5)
  at async Loader.import (internal/modules/esm/loader.js:178:24)
  at async Object.loadESM (internal/process/esm_loader.js:68:5)
nathanchase commented 2 years ago

Here's a repro: https://codesandbox.io/s/bold-cookies-gpjuv

ms-fadaei commented 2 years ago

@pi0, Can we use this pattern?

- import { readFile, stat, Stats } from 'fs-extra'
+ import fsExtra, { Stats } from 'fs-extra'
+ const { stat, readFile } = fsExtra;
thomas4Bitcraft commented 2 years ago

@pi0 isn't this is a quick way to solve this issue?

nathanchase commented 2 years ago

This is still unfortunately the only thing that is holding me back from using @nuxt/bridge. Is /server/middleware no longer viable in Nuxt 3 or Bridge?

thomas4Bitcraft commented 2 years ago

Yes all our projects too. I really don't understand why this takes so long as this and nuxt/image are official packages from the nuxt team...

pi0 commented 2 years ago

fs-extra was a CommonJS dependency that should be removed from the latest release already and direct usage with Nitro (Nuxt 3 or Nuxt 2 Bridge) possible. I'm working on image module for Nuxt 3.

BTW if you have other problems to use directly with Nuxt3, please use a new issue.