Skip to content

Commit

Permalink
fix(commonjs): Always sort node-resolve plugin after commonjs if it i…
Browse files Browse the repository at this point in the history
…s not the case
  • Loading branch information
lukastaegert committed Nov 12, 2021
1 parent f306907 commit 8d56097
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 21 deletions.
14 changes: 14 additions & 0 deletions packages/commonjs/src/index.js
Expand Up @@ -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) {
Expand Down
@@ -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' });
}
};
Expand Up @@ -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' });
Expand Down
@@ -0,0 +1 @@
exports.foo = 'foo';
118 changes: 100 additions & 18 deletions packages/commonjs/test/snapshots/function.js.md
Expand Up @@ -5629,7 +5629,7 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
{
'generated-main.js': `'use strict';␊
'main.js': `'use strict';␊
␊
var main = {};␊
␊
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;␊
`,
}
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.

0 comments on commit 8d56097

Please sign in to comment.