FredKSchott / snowpack

ESM-powered frontend build tool. Instant, lightweight, unbundled development. ✌️
https://www.snowpack.dev
MIT License
19.48k stars 921 forks source link

🐛 BUG: Cannot use ESM only modules as plugin dependencies #3838

Open waynevanson opened 2 years ago

waynevanson commented 2 years ago

Quick checklist

What package manager are you using?

pnpm

What operating system are you using?

Linux (Ubuntu)

Describe the bug

For context, the task I am undertaking is building snowpack-plugin-purescript, a plugin for using Purescript out of the box.

I am unable to use execa@^6 because it the module system built is esm and not cjs.

snowpack@plugin-sass works because it uses execa@^5. https://github.com/withastro/snowpack/blob/c44d86f73ac7b74507d4d5554ccb96e8b7dc5294/plugins/plugin-sass/package.json#L15-L16

execa@^5 contains cjs only, as visible on unpkg.

In the meantime, I can downgrade a major version and pray it works.

Possible Problem

The cli entrypoint loads cjs but never the esm version of the package. https://github.com/withastro/snowpack/blob/c44d86f73ac7b74507d4d5554ccb96e8b7dc5294/snowpack/index.bin.js#L12

I checked the file below and the package.json, and the only reference to the value is this. https://github.com/withastro/snowpack/blob/c44d86f73ac7b74507d4d5554ccb96e8b7dc5294/snowpack/package.json#L28-L35

I can confirm that the esm files are available to be read on my system in node_modules.

Possible Solution

I'm unsure of this, but I feel like we need to check if we're using ESM or CJS and import the CLI using the correct method.

-const cli = require('./lib/cjs/index.js'); 
// More pseudocode than real code as I don't know the exact solution.
+const cli = <is_cjs> ? require('./lib/cjs/index.js'): import('./lib/esm/index.js');

I haven't considered how to structure the package.json file with these changes. I assume to add "module": "./lib/esm/index.js" but there may be more.

Steps to reproduce

Posting before my browser turn off, back in a sec.

git clone https://github.com/waynevanson/snowpack-esm-dependency-plugin-error.git
cd snowpack-esm-dependency-plugin-error
pnpm install
cd example-application
pnpm snowpack build
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/waynevanson/Documents/code/snowpack-esm-dependency-plugin-error/example-plugin/index.js from /home/waynevanson/Documents/code/snowpack-esm-dependency-plugin-error/node_modules/.pnpm/snowpack@3.8.8/node_modules/snowpack/lib/cjs/util.js not supported.
Instead change the require of index.js in /home/waynevanson/Documents/code/snowpack-esm-dependency-plugin-error/node_modules/.pnpm/snowpack@3.8.8/node_modules/snowpack/lib/cjs/util.js to a dynamic import() which is available in all CommonJS modules.
    at loadPluginFromConfig (/home/waynevanson/Documents/code/snowpack-esm-dependency-plugin-error/node_modules/.pnpm/snowpack@3.8.8/node_modules/snowpack/lib/cjs/config.js:286:34)
    at /home/waynevanson/Documents/code/snowpack-esm-dependency-plugin-error/node_modules/.pnpm/snowpack@3.8.8/node_modules/snowpack/lib/cjs/config.js:316:24
    at Array.forEach (<anonymous>)
    at loadPlugins (/home/waynevanson/Documents/code/snowpack-esm-dependency-plugin-error/node_modules/.pnpm/snowpack@3.8.8/node_modules/snowpack/lib/cjs/config.js:313:20)
    at normalizeConfig (/home/waynevanson/Documents/code/snowpack-esm-dependency-plugin-error/node_modules/.pnpm/snowpack@3.8.8/node_modules/snowpack/lib/cjs/config.js:435:39)
    at createConfiguration (/home/waynevanson/Documents/code/snowpack-esm-dependency-plugin-error/node_modules/.pnpm/snowpack@3.8.8/node_modules/snowpack/lib/cjs/config.js:657:30)
    at Object.loadConfiguration (/home/waynevanson/Documents/code/snowpack-esm-dependency-plugin-error/node_modules/.pnpm/snowpack@3.8.8/node_modules/snowpack/lib/cjs/config.js:735:16)
    at async cli (/home/waynevanson/Documents/code/snowpack-esm-dependency-plugin-error/node_modules/.pnpm/snowpack@3.8.8/node_modules/snowpack/lib/cjs/index.js:145:20)

Link to minimal reproducible example (optional)

https://github.com/waynevanson/snowpack-esm-dependency-plugin-error

Additional Information

I just want to say that I really like how snowpack is unbundled first. It feels very good when mounting directories and it just works.

I have some questions regarding the architecture the plugin API for my use case, but I'll find somewhere else to put that. Thanks again!