martpie / next-transpile-modules

Next.js plugin to transpile code from node_modules. Please see: https://github.com/martpie/next-transpile-modules/issues/291
MIT License
1.13k stars 85 forks source link

Use Next.js 11.1+ `esmExternals` option; skip `enhanced-resolve`? #232

Closed lunelson closed 2 years ago

lunelson commented 2 years ago

Is your feature request related to a problem? Please describe.

I'm currently struggling with a project that has the experimental.esmExternals option activated, and the transpiling of a single module which is failing on CI (Vercel)—though it works locally. I suspect it's something about how the package.json fields are specified.

Looking at the source and the source of enhanced-resolve it occurs to me: couldn't we now just rely on native resolution via require.resolve, if Next.js is enabling ESM modules?

Describe the solution you'd like If the experimental.esmExternals option is true, use native node 14+ resolution to find the package. (Would this work? 🤷🏻)

martpie commented 2 years ago

We should not get rid of enhanced-resolve because this is a requirement for Yarn/PNP/other advanced package manager usages.

That said we can for sure tweak enhanced-resolve to be compatible with the new experimental.esmExternals.

What problem are you facing exactly?

lunelson commented 2 years ago

I've got an error appearing on the Vercel production deployment when I try to use "preview mode" by accessing the /api/preview path. I can't reproduce this error in a local production build (created with next build && next start—where preview works fine).

I recently updated the package @rasahq/react-tabula (I specify it by name like that in the nextTM config) with an ESM-conformant configuration in package.json like this—some time earlier I had only "module": "index.js"—but otherwise it's hard to pinpoint when this problem started since it doesn't happen locally and I hadn't tested preview mode in some weeks.

"type": "module",
"main": "index.js",

...main is supported in both "commonjs" and "module" modes (ref), and I didn't want to use the exports field because it seems to create a lot of new problems by restricting possible import paths. Anyway below is the function log from Vercel when trying to access the preview API route; in the front end it causes an "internal server error 500":

next-transpile-modules - DEPRECATED - fallbacking to previous module resolution system for module "@rasahq/react-tabula", you can now just pass the name of the package to transpile and it will detect its real path without you having to pass a sub-module.
2021-08-30T10:10:57.099Z    f51e15ae-a206-458c-8673-24dd421a1d55    ERROR   Error: next-transpile-modules - an unexpected error happened when trying to resolve "@rasahq/react-tabula". Are you sure the name module you are trying to transpile is correct, and it has a "main" or an "exports" field?
Error: Can't resolve '@rasahq/react-tabula' in '/var/task'
    at getPackageRootDirectory (/var/task/node_modules/next-transpile-modules/src/next-transpile-modules.js:173:17)
    at Array.map (<anonymous>)
    at withTM (/var/task/node_modules/next-transpile-modules/src/next-transpile-modules.js:185:34)
    at /var/task/.next/server/chunks/64.js:73:57
    at Array.reduce (<anonymous>)
    at composePlugins (/var/task/.next/server/chunks/64.js:71:18)
    at Object.3064 (/var/task/.next/server/chunks/64.js:33:18)
    at __webpack_require__ (/var/task/.next/server/webpack-runtime.js:25:42)
    at Object.797 (/var/task/.next/server/pages/api/preview.js:25:5)
    at __webpack_require__ (/var/task/.next/server/webpack-runtime.js:25:42)
RequestId: f51e15ae-a206-458c-8673-24dd421a1d55 Error: Runtime exited with error: exit status 1
Runtime.ExitError
lunelson commented 2 years ago

Closing this then, in favor of this one https://github.com/martpie/next-transpile-modules/issues/234