ericcornelissen/webmangler

Support ESM-based configuration files

Opened this issue · 0 comments

Package(s)

webmangler-cli (v0.1.8)

Description

Currently it is only possible to have WebMangler (CLI) configuration files with CommonJS (CJS) syntax. This should be extended to support ESModule (ESM) syntax as well.

When attempting to use a ESM-based configuration file you'll encounter the following error message:

$ webmangler ./dist --write --stats

.../node_modules/cosmiconfig/dist/loaders.js:32
    throw error;
                ^

Error [ERR_REQUIRE_ESM]: require() of ES Module .../.webmanglerrc.js from .../node_modules/cosmiconfig/dist/loaders.js not supported.
Instead change the require of .webmanglerrc.js in .../node_modules/cosmiconfig/dist/loaders.js to a dynamic import() which is available in all CommonJS modules.
    at module.exports (.../node_modules/import-fresh/index.js:32:59)
    at loadJs (.../node_modules/cosmiconfig/dist/loaders.js:16:18)
    at ExplorerSync.loadFileContentSync (.../node_modules/cosmiconfig/dist/ExplorerSync.js:84:26)
    at ExplorerSync.createCosmiconfigResultSync (.../node_modules/cosmiconfig/dist/ExplorerSync.js:89:30)
    at ExplorerSync.loadSearchPlaceSync (.../node_modules/cosmiconfig/dist/ExplorerSync.js:70:25)
    at ExplorerSync.searchDirectorySync (.../node_modules/cosmiconfig/dist/ExplorerSync.js:55:32)
    at run (.../node_modules/cosmiconfig/dist/ExplorerSync.js:35:27)
    at cacheWrapperSync (.../node_modules/cosmiconfig/dist/cacheWrapper.js:28:18)
    at ExplorerSync.searchFromDirectorySync (.../node_modules/cosmiconfig/dist/ExplorerSync.js:47:49)
    at ExplorerSync.searchSync (.../node_modules/cosmiconfig/dist/ExplorerSync.js:27:25)
    at searchDefaultPaths (.../node_modules/webmangler-cli/lib/config/loader.js:33:29)
    at .../node_modules/webmangler-cli/lib/config/loader.js:63:13
    at run (.../node_modules/webmangler-cli/lib/main.js:34:41)
    at Object.<anonymous> (.../node_modules/webmangler-cli/lib/index.js:49:20) {
  code: 'ERR_REQUIRE_ESM'
}

Motivation

Node.js is moving towards having ESM be the default syntax for JavaScript files, hence it makes sense to support this for WebMangler configuration files.

Proposal

The configuration file should load correctly at least in the following scenarios:

  • package.json::type = "module" and config file is in ESM with .js extension.
  • package.json::type = "commonjs" and config file is in CJS with .js extension.
  • Config file is in ESM with .mjs extension (regardless of package.json).
  • Config file is in CJS with .cjs extension (regardless of package.json).

Alternatives

Alternatively, only ESM configuration files could be supported. I don't think this makes sense as long as Node.js still supports the CJS syntax.

Notes

No response