diff --git a/cli/run/loadConfigFile.ts b/cli/run/loadConfigFile.ts index 31f9c77fbf0..986e1c0823e 100644 --- a/cli/run/loadConfigFile.ts +++ b/cli/run/loadConfigFile.ts @@ -55,7 +55,6 @@ async function getConfigFileExport(fileName: string, commandOptions: Record { - console.error(warning); if (warning.message.includes('To load an ES module')) { cannotLoadEsm = true; } diff --git a/src/utils/error.ts b/src/utils/error.ts index 204eb0fee96..b9d06647574 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -71,7 +71,7 @@ const ADDON_ERROR = 'ADDON_ERROR', ILLEGAL_REASSIGNMENT = 'ILLEGAL_REASSIGNMENT', INPUT_HOOK_IN_OUTPUT_PLUGIN = 'INPUT_HOOK_IN_OUTPUT_PLUGIN', INVALID_CHUNK = 'INVALID_CHUNK', - INVALID_CONFIG_MODULE_FORMAT = 'MISSING_CONFIGINVALID_CONFIG_MODULE_FORMAT', + INVALID_CONFIG_MODULE_FORMAT = 'INVALID_CONFIG_MODULE_FORMAT', INVALID_EXPORT_OPTION = 'INVALID_EXPORT_OPTION', INVALID_EXTERNAL_ID = 'INVALID_EXTERNAL_ID', INVALID_OPTION = 'INVALID_OPTION', diff --git a/test/cli/node_modules/rollup-plugin-esm-test/index.mjs b/test/cli/node_modules/rollup-plugin-esm-test/index.mjs new file mode 100644 index 00000000000..14daca5000a --- /dev/null +++ b/test/cli/node_modules/rollup-plugin-esm-test/index.mjs @@ -0,0 +1,9 @@ +export function esmTest() { + return { + transform(code, id) { + if (id.includes("\0")) return null; + const name = JSON.stringify(id.replace(/^.*\/advanced-esm\//, "esm-test: ")); + return `console.log(${name});\n${code}`; + } + }; +} diff --git a/test/cli/node_modules/rollup-plugin-esm-test/package.json b/test/cli/node_modules/rollup-plugin-esm-test/package.json new file mode 100644 index 00000000000..7084d6ebcc0 --- /dev/null +++ b/test/cli/node_modules/rollup-plugin-esm-test/package.json @@ -0,0 +1,4 @@ +{ + "type": "module", + "main": "index.mjs" +} diff --git a/test/cli/samples/config-cwd-case-insensitive-es6/rollup.config.js b/test/cli/samples/config-cwd-case-insensitive-es6/rollup.config.mjs similarity index 100% rename from test/cli/samples/config-cwd-case-insensitive-es6/rollup.config.js rename to test/cli/samples/config-cwd-case-insensitive-es6/rollup.config.mjs diff --git a/test/cli/samples/import-esm-package/_config.js b/test/cli/samples/import-esm-package/_config.js index a7f55262e3a..23d127d1d67 100644 --- a/test/cli/samples/import-esm-package/_config.js +++ b/test/cli/samples/import-esm-package/_config.js @@ -1,4 +1,5 @@ module.exports = { description: 'allows to import ESM dependencies from transpiled config files', - command: "rollup --config --configPlugin '{transform: c => c}'" + skipIfWindows: true, + command: "rollup --config --configPlugin '{transform:c => c}'" }; diff --git a/test/cli/samples/plugin/advanced-esm/_config.js b/test/cli/samples/plugin/advanced-esm/_config.js index 55146ba1cb5..ee7545ea184 100644 --- a/test/cli/samples/plugin/advanced-esm/_config.js +++ b/test/cli/samples/plugin/advanced-esm/_config.js @@ -1,4 +1,9 @@ module.exports = { description: 'load an ESM-only rollup plugin from node_modules as well as CJS plugins', - command: `rollup -c -p node-resolve,commonjs,esm-test -p "terser={mangle: false, output: {beautify: true, indent_level: 2}}"` + skipIfWindows: true, + + // The NodeJS resolution rules for ESM modules are more restrictive than CJS. + // Must copy the ESM plugin into the main node_modules in order to use and test it. + + command: `rm -rf ../../../../../node_modules/rollup-plugin-esm-test && cp -rp ../../../node_modules_rename_me/rollup-plugin-esm-test ../../../../../node_modules/ && rollup -c -p node-resolve,commonjs,esm-test -p "terser={mangle: false, output: {beautify: true, indent_level: 2}}"` }; diff --git a/test/load-config-file/index.js b/test/load-config-file/index.js index 9a19d53032f..b8937a7340f 100644 --- a/test/load-config-file/index.js +++ b/test/load-config-file/index.js @@ -70,13 +70,29 @@ describe('loadConfigFile', () => { cause: { message: "Unexpected token 'export'" }, - code: 'MISSING_CONFIGINVALID_CONFIG_MODULE_FORMAT', + code: 'INVALID_CONFIG_MODULE_FORMAT', message: 'Node tried to load your configuration file as CommonJS even though it is likely an ES module. To resolve this, change the extension of your configuration to ".mjs", set "type": "module" in your package.json file or pass the "--bundleConfigAsCjs" flag.\n\nOriginal error: Unexpected token \'export\'', url: 'https://rollupjs.org/guide/en/#--bundleconfigascjs' }); }); + it('just throws the error if it is not accompanied by the proper warning', async () => { + let caughtError; + try { + const promise = loadConfigFile( + path.resolve(__dirname, 'samples/esm-with-error/rollup.config.js') + ); + process.emit('warning', { message: 'Another warning.' }); + await promise; + } catch (err) { + caughtError = err; + } + compareError(caughtError, { + message: 'Config broken.' + }); + }); + it('throws a helpful error when loading a CommonJS module that should actually be ES', async () => { let caughtError; try { @@ -103,10 +119,24 @@ describe('loadConfigFile', () => { cause: { message: 'module is not defined in ES module scope' }, - code: 'MISSING_CONFIGINVALID_CONFIG_MODULE_FORMAT', + code: 'INVALID_CONFIG_MODULE_FORMAT', message: 'Rollup transpiled your configuration to an ES module even though it appears to contain CommonJS elements. To resolve this, you can pass the "--bundleConfigAsCjs" flag to Rollup or change your configuration to only contain valid ESM code.\n\nOriginal error: module is not defined in ES module scope', url: 'https://rollupjs.org/guide/en/#--bundleconfigascjs' }); }); + + it('just throws other errors while bundling', async () => { + let caughtError; + try { + await loadConfigFile(path.resolve(__dirname, 'samples/esm-with-error/rollup.config.js'), { + configPlugin: '{transform: c => c}' + }); + } catch (err) { + caughtError = err; + } + compareError(caughtError, { + message: 'Config broken.' + }); + }); }); diff --git a/test/load-config-file/samples/esm-with-error/package.json b/test/load-config-file/samples/esm-with-error/package.json new file mode 100644 index 00000000000..bedb411a912 --- /dev/null +++ b/test/load-config-file/samples/esm-with-error/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/test/load-config-file/samples/esm-with-error/rollup.config.js b/test/load-config-file/samples/esm-with-error/rollup.config.js new file mode 100644 index 00000000000..36a7414d777 --- /dev/null +++ b/test/load-config-file/samples/esm-with-error/rollup.config.js @@ -0,0 +1 @@ +throw new Error('Config broken.');