nystudio107 / rollup-plugin-critical

Vite.js & Rollup plugin for generating critical CSS
MIT License
91 stars 11 forks source link

Require error #9

Closed ishetnogferre closed 1 year ago

ishetnogferre commented 1 year ago

Describe the bug

In a Craft & Vite project after following the docs and including the standard setup in the vite.config.js plugins section, following error keeps showing after running the commands for serving or building:

Error [ERR_REQUIRE_ESM]: require() of ES Module /XXX/XXX/node_modules/critical/index.js from /XXX/XXX/node_modules/rollup-plugin-critical/dist/index.js not supported.
Instead change the require of /XXX/XXX/node_modules/critical/index.js in /XXX/XXX/node_modules/rollup-plugin-critical/dist/index.js to a dynamic import() which is available in all CommonJS modules.

To reproduce

Steps to reproduce the behaviour:

  1. A Craft CMS setup with vite plugin installed
  2. Add this plugin
  3. Run "vite" or "vite build"
  4. Error in console

Expected behaviour

A clear and concise description of what you expected to happen.

Screenshots

If applicable, add screenshots to help explain your problem.

Versions

jmartsch commented 1 year ago

Same here but with ProcessWire as a CMS.

khalwat commented 1 year ago

Yep, I'm fixing it right now... it should be bundled as CommonJS by default, not ESM.

khalwat commented 1 year ago

Well, that was fun. The actual issue was that the critical package moved to be ESM-only, and rollup-plugin-critical is shipped with both CJS and ESM versions.

So I had to dynamically import the critical ESM into rollup-plugin-critical to allow it to work for both CJS and ESM builds.

The current tests all passed, because they operate on the ESM build; I'll have to investigate that further at some point, to see if I can add a real-world test trying to import and use rollup-plugin-critical for both ESM and CJS.

Fixed in 1.0.12 -> https://github.com/nystudio107/rollup-plugin-critical/releases/tag/1.0.12

jmartsch commented 1 year ago

I updated to latest version, but the error still occurs to me. Deleted node_modules and reinstalled everything.

image

khalwat commented 1 year ago

@jmartsch please verify that the new version is actually installed?

I've tested this on an external project that demonstrated the issue, and it worked fine. You might try nuking your node_modules directory and package-lock.json and installing from scratch.

jmartsch commented 1 year ago

Deleted package-lock.json and reinstalled. Throws no error now, but also seems to not generate the crititcal-css. But might be another problem on my side. I am checking tomorrow.

khalwat commented 1 year ago

FWIW, the test project I mentioned generated the critical CSS properly