jaredpalmer/razzle

Support ES module razzle.config.js

gregmartyn opened this issue ยท 0 comments

๐Ÿ› Bug report

Current Behavior

razzle.config.js is imported as a commonjs module regardless of the package.json type setting. This blocks adoption of native ES modules on the server-side.

Expected behavior

The module format used when loading razzle.config.js should be influenced by the package.json "type" field. If it isn't specified, or set to "commonjs", the behavior is unchanged from the present. If it is set to "module", it should be loaded as an ES module.

Reproducible example

Set "type": "module" in package.json then run yarn start

Error [ERR_REQUIRE_ESM]: require() of ES Module /var/www/html/razzle.config.js from /var/www/html/node_modules/razzle/config/loadRazzleConfig.js not supported.
Instead change the require of razzle.config.js in /var/www/html/node_modules/razzle/config/loadRazzleConfig.js to a dynamic import() which is available in all CommonJS modules.

Suggested solution(s)

If we see "type": "module" in package.json, use await import() instead of require() when loading razzle.config.js.

PR Here: #1848

Additional context

There is further work to do to support ES modules. This only addresses loading the config file. There's still remains work to configure webpack to output ES modules. (more info here: webpack/webpack#2933 (comment)) I've implemented the above PR to see how it goes.

I'm doing this work in a backward-compatible way in hopes that can be merged without being blocked by the next major release.

Your environment

Software Version(s)
Razzle 4.2.16
Razzle Plugins
Node v16
Browser
npm/Yarn yarn
Operating System linux
TypeScript
React 17