diff --git a/packages/commonjs/src/index.js b/packages/commonjs/src/index.js index 43c5ad1be..892a4eaf3 100644 --- a/packages/commonjs/src/index.js +++ b/packages/commonjs/src/index.js @@ -146,6 +146,20 @@ export default function commonjs(options = {}) { return { name: 'commonjs', + options(options) { + // Always sort the node-resolve plugin after the commonjs plugin as otherwise CommonJS entries + // will not work with strictRequires: true + const { plugins } = options; + if (Array.isArray(plugins)) { + const cjsIndex = plugins.findIndex((plugin) => plugin.name === 'commonjs'); + const nodeResolveIndex = plugins.findIndex((plugin) => plugin.name === 'node-resolve'); + if (nodeResolveIndex >= 0 && nodeResolveIndex < cjsIndex) { + plugins.splice(cjsIndex + 1, 0, plugins[nodeResolveIndex]); + plugins.splice(nodeResolveIndex, 1); + } + } + }, + buildStart() { validateRollupVersion(this.meta.rollupVersion, peerDependencies.rollup); if (options.namedExports != null) { diff --git a/packages/commonjs/test/fixtures/function/strict-requires-entry-node-resolve/_config.js b/packages/commonjs/test/fixtures/function/strict-requires-entry-node-resolve/_config.js new file mode 100644 index 000000000..d5ae8bbb1 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/strict-requires-entry-node-resolve/_config.js @@ -0,0 +1,31 @@ +const assert = require('assert'); + +const { nodeResolve } = require('@rollup/plugin-node-resolve'); + +module.exports = { + description: + 'strict require semantic modules can be entry points when the node-resolve plugin is used', + pluginOptions: { + strictRequires: true + }, + options: { + plugins: [ + { + name: 'before-node', + buildStart({ plugins }) { + assert.deepStrictEqual( + plugins.map((plugin) => plugin.name), + ['before-node', 'after-node', 'commonjs', 'node-resolve'] + ); + } + }, + nodeResolve(), + { + name: 'after-node' + } + ] + }, + exports(exports) { + assert.deepStrictEqual(exports, { foo: 'foo' }); + } +}; diff --git a/packages/commonjs/test/fixtures/function/strict-requires-entry/main.js b/packages/commonjs/test/fixtures/function/strict-requires-entry-node-resolve/main.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-requires-entry/main.js rename to packages/commonjs/test/fixtures/function/strict-requires-entry-node-resolve/main.js diff --git a/packages/commonjs/test/fixtures/function/strict-requires-entry/_config.js b/packages/commonjs/test/fixtures/function/strict-requires-multiple-entry/_config.js similarity index 60% rename from packages/commonjs/test/fixtures/function/strict-requires-entry/_config.js rename to packages/commonjs/test/fixtures/function/strict-requires-multiple-entry/_config.js index b9807be5c..6e3b65ee2 100644 --- a/packages/commonjs/test/fixtures/function/strict-requires-entry/_config.js +++ b/packages/commonjs/test/fixtures/function/strict-requires-multiple-entry/_config.js @@ -4,15 +4,15 @@ module.exports = { description: 'strict require semantic modules can be entry points', options: { input: [ - 'fixtures/function/strict-requires-entry/main.js', - 'fixtures/function/strict-requires-entry/other.js' + 'fixtures/function/strict-requires-multiple-entry/main.js', + 'fixtures/function/strict-requires-multiple-entry/other.js' ], output: { chunkFileNames: 'generated-[name].js' } }, pluginOptions: { - strictRequires: ['fixtures/function/strict-requires-entry/main.js'] + strictRequires: ['fixtures/function/strict-requires-multiple-entry/main.js'] }, exports(exports) { assert.deepStrictEqual(exports, { foo: 'foo' }); diff --git a/packages/commonjs/test/fixtures/function/strict-requires-multiple-entry/main.js b/packages/commonjs/test/fixtures/function/strict-requires-multiple-entry/main.js new file mode 100644 index 000000000..94ecacb72 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/strict-requires-multiple-entry/main.js @@ -0,0 +1 @@ +exports.foo = 'foo'; diff --git a/packages/commonjs/test/fixtures/function/strict-requires-entry/other.js b/packages/commonjs/test/fixtures/function/strict-requires-multiple-entry/other.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-requires-entry/other.js rename to packages/commonjs/test/fixtures/function/strict-requires-multiple-entry/other.js diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index a004bbea7..260d58e48 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -5629,7 +5629,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 { - 'generated-main.js': `'use strict';␊ + 'main.js': `'use strict';␊ ␊ var main = {};␊ ␊ @@ -5642,26 +5642,10 @@ Generated by [AVA](https://avajs.dev). return main;␊ }␊ ␊ - exports.requireMain = requireMain;␊ - `, - 'main.js': `'use strict';␊ - ␊ - var main = require('./generated-main.js');␊ - ␊ - var mainExports = main.requireMain();␊ + var mainExports = requireMain();␊ ␊ module.exports = mainExports;␊ `, - 'other.js': `'use strict';␊ - ␊ - var main = require('./generated-main.js');␊ - ␊ - var other = {};␊ - ␊ - t.is(main.requireMain().foo, 'foo');␊ - ␊ - module.exports = other;␊ - `, } ## strict-requires-exportmode-exports @@ -5899,6 +5883,80 @@ Generated by [AVA](https://avajs.dev). `, } +## strict-requires-magic-string + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + Object.defineProperty(exports, '__esModule', { value: true });␊ + ␊ + var main = {};␊ + ␊ + var hasRequiredMain;␊ + ␊ + function requireMain () {␊ + if (hasRequiredMain) return main;␊ + hasRequiredMain = 1;␊ + console.log('hey');␊ + // magic-string@0.25.7␊ + const m = new MagicString('0123456789');␊ + console.log(␊ + m.prependRight(0, 'W').prependLeft(3, 'AB').appendRight(9, 'XY').remove(6, 8).toString()␊ + );␊ + const bundle = new MagicString.Bundle();␊ + bundle.addSource({ filename: 'foo.txt', content: m });␊ + const map = bundle.generateMap({ file: 'bundle.txt', includeContent: true, hires: true });␊ + console.log(JSON.stringify(map));␊ + main.foo = 'foo';␊ + return main;␊ + }␊ + ␊ + exports.__require = requireMain;␊ + `, + } + +## strict-requires-multiple-entry + +> Snapshot 1 + + { + 'generated-main.js': `'use strict';␊ + ␊ + var main = {};␊ + ␊ + var hasRequiredMain;␊ + ␊ + function requireMain () {␊ + if (hasRequiredMain) return main;␊ + hasRequiredMain = 1;␊ + main.foo = 'foo';␊ + return main;␊ + }␊ + ␊ + exports.requireMain = requireMain;␊ + `, + 'main.js': `'use strict';␊ + ␊ + var main = require('./generated-main.js');␊ + ␊ + var mainExports = main.requireMain();␊ + ␊ + module.exports = mainExports;␊ + `, + 'other.js': `'use strict';␊ + ␊ + var main = require('./generated-main.js');␊ + ␊ + var other = {};␊ + ␊ + t.is(main.requireMain().foo, 'foo');␊ + ␊ + module.exports = other;␊ + `, + } + ## this > Snapshot 1 @@ -6749,3 +6807,27 @@ Generated by [AVA](https://avajs.dev). module.exports = main;␊ `, } + +## strict-requires-entry-node-resolve + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var main = {};␊ + ␊ + var hasRequiredMain;␊ + ␊ + function requireMain () {␊ + if (hasRequiredMain) return main;␊ + hasRequiredMain = 1;␊ + main.foo = 'foo';␊ + return main;␊ + }␊ + ␊ + var mainExports = requireMain();␊ + ␊ + module.exports = mainExports;␊ + `, + } diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index 6e4f49fa6..4ec48d232 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ