diff --git a/packages/commonjs/src/index.js b/packages/commonjs/src/index.js index b3e277e0e..53be16678 100644 --- a/packages/commonjs/src/index.js +++ b/packages/commonjs/src/index.js @@ -60,10 +60,11 @@ export default function commonjs(options = {}) { : () => typeof defaultIsModuleExportsOption === 'boolean' ? defaultIsModuleExportsOption : 'auto'; - const { resolveRequireSourcesAndGetMeta, getWrappedIds } = getResolveRequireSourcesAndGetMeta( - extensions, - detectCycles - ); + const { + resolveRequireSourcesAndGetMeta, + getWrappedIds, + isRequiredId + } = getResolveRequireSourcesAndGetMeta(extensions, detectCycles); const dynamicRequireModuleSet = getDynamicRequireModuleSet(options.dynamicRequireTargets); const isDynamicRequireModulesEnabled = dynamicRequireModuleSet.size > 0; const commonDir = isDynamicRequireModulesEnabled @@ -115,7 +116,8 @@ export default function commonjs(options = {}) { if ( !dynamicRequireModuleSet.has(normalizePathSlashes(id)) && - (!hasCjsKeywords(code, ignoreGlobal) || (isEsModule && !options.transformMixedEsModules)) + (!(hasCjsKeywords(code, ignoreGlobal) || isRequiredId(id)) || + (isEsModule && !options.transformMixedEsModules)) ) { return { meta: { commonjs: { isCommonJS: false } } }; } @@ -140,13 +142,28 @@ export default function commonjs(options = {}) { ast, getDefaultIsModuleExports(id), needsRequireWrapper, - resolveRequireSourcesAndGetMeta(this) + resolveRequireSourcesAndGetMeta(this), + isRequiredId(id) ); } 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/src/resolve-require-sources.js b/packages/commonjs/src/resolve-require-sources.js index ce3727288..08b0de297 100644 --- a/packages/commonjs/src/resolve-require-sources.js +++ b/packages/commonjs/src/resolve-require-sources.js @@ -3,6 +3,7 @@ import { resolveExtensions } from './resolve-id'; export function getResolveRequireSourcesAndGetMeta(extensions, detectCycles) { const knownCjsModuleTypes = Object.create(null); + const requiredIds = Object.create(null); const dependentModules = Object.create(null); const getDependentModules = (id) => dependentModules[id] || (dependentModules[id] = Object.create(null)); @@ -12,6 +13,7 @@ export function getResolveRequireSourcesAndGetMeta(extensions, detectCycles) { Object.keys(knownCjsModuleTypes).filter( (id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS ), + isRequiredId: (id) => requiredIds[id], resolveRequireSourcesAndGetMeta: (rollupContext) => async (id, isParentCommonJS, sources) => { knownCjsModuleTypes[id] = isParentCommonJS; const requireTargets = await Promise.all( @@ -34,6 +36,7 @@ export function getResolveRequireSourcesAndGetMeta(extensions, detectCycles) { if (resolved.external) { return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false }; } + requiredIds[childId] = true; const parentDependentModules = getDependentModules(id); const childDependentModules = getDependentModules(childId); childDependentModules[id] = true; diff --git a/packages/commonjs/src/transform-commonjs.js b/packages/commonjs/src/transform-commonjs.js index fed2690d6..c6cfbdc1a 100644 --- a/packages/commonjs/src/transform-commonjs.js +++ b/packages/commonjs/src/transform-commonjs.js @@ -51,7 +51,8 @@ export default async function transformCommonjs( astCache, defaultIsModuleExports, needsRequireWrapper, - resolveRequireSourcesAndGetMeta + resolveRequireSourcesAndGetMeta, + isRequired ) { const ast = astCache || tryParse(parse, code, id); const magicString = new MagicString(code); @@ -418,6 +419,7 @@ export default async function transformCommonjs( if ( !( shouldWrap || + isRequired || uses.module || uses.exports || uses.require || diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-auto/_config.js b/packages/commonjs/test/fixtures/function/strict-requires-auto/_config.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-auto/_config.js rename to packages/commonjs/test/fixtures/function/strict-requires-auto/_config.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-auto/a-imports-b.js b/packages/commonjs/test/fixtures/function/strict-requires-auto/a-imports-b.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-auto/a-imports-b.js rename to packages/commonjs/test/fixtures/function/strict-requires-auto/a-imports-b.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-auto/b-imports-c.js b/packages/commonjs/test/fixtures/function/strict-requires-auto/b-imports-c.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-auto/b-imports-c.js rename to packages/commonjs/test/fixtures/function/strict-requires-auto/b-imports-c.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-auto/c-imports-a.js b/packages/commonjs/test/fixtures/function/strict-requires-auto/c-imports-a.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-auto/c-imports-a.js rename to packages/commonjs/test/fixtures/function/strict-requires-auto/c-imports-a.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-auto/main.js b/packages/commonjs/test/fixtures/function/strict-requires-auto/main.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-auto/main.js rename to packages/commonjs/test/fixtures/function/strict-requires-auto/main.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-circular/_config.js b/packages/commonjs/test/fixtures/function/strict-requires-circular/_config.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-circular/_config.js rename to packages/commonjs/test/fixtures/function/strict-requires-circular/_config.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-circular/main.js b/packages/commonjs/test/fixtures/function/strict-requires-circular/main.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-circular/main.js rename to packages/commonjs/test/fixtures/function/strict-requires-circular/main.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-circular/other.js b/packages/commonjs/test/fixtures/function/strict-requires-circular/other.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-circular/other.js rename to packages/commonjs/test/fixtures/function/strict-requires-circular/other.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-cycle-detection/_config.js b/packages/commonjs/test/fixtures/function/strict-requires-cycle-detection/_config.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-cycle-detection/_config.js rename to packages/commonjs/test/fixtures/function/strict-requires-cycle-detection/_config.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-cycle-detection/a-imports-b.js b/packages/commonjs/test/fixtures/function/strict-requires-cycle-detection/a-imports-b.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-cycle-detection/a-imports-b.js rename to packages/commonjs/test/fixtures/function/strict-requires-cycle-detection/a-imports-b.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-cycle-detection/b-imports-c.js b/packages/commonjs/test/fixtures/function/strict-requires-cycle-detection/b-imports-c.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-cycle-detection/b-imports-c.js rename to packages/commonjs/test/fixtures/function/strict-requires-cycle-detection/b-imports-c.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-cycle-detection/c-imports-a.js b/packages/commonjs/test/fixtures/function/strict-requires-cycle-detection/c-imports-a.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-cycle-detection/c-imports-a.js rename to packages/commonjs/test/fixtures/function/strict-requires-cycle-detection/c-imports-a.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-cycle-detection/main.js b/packages/commonjs/test/fixtures/function/strict-requires-cycle-detection/main.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-cycle-detection/main.js rename to packages/commonjs/test/fixtures/function/strict-requires-cycle-detection/main.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-debug-none/_config.js b/packages/commonjs/test/fixtures/function/strict-requires-debug-none/_config.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-debug-none/_config.js rename to packages/commonjs/test/fixtures/function/strict-requires-debug-none/_config.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-debug-none/main.js b/packages/commonjs/test/fixtures/function/strict-requires-debug-none/main.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-debug-none/main.js rename to packages/commonjs/test/fixtures/function/strict-requires-debug-none/main.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-debug/_config.js b/packages/commonjs/test/fixtures/function/strict-requires-debug/_config.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-debug/_config.js rename to packages/commonjs/test/fixtures/function/strict-requires-debug/_config.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-debug/a-imports-b.js b/packages/commonjs/test/fixtures/function/strict-requires-debug/a-imports-b.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-debug/a-imports-b.js rename to packages/commonjs/test/fixtures/function/strict-requires-debug/a-imports-b.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-debug/b-imports-c.js b/packages/commonjs/test/fixtures/function/strict-requires-debug/b-imports-c.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-debug/b-imports-c.js rename to packages/commonjs/test/fixtures/function/strict-requires-debug/b-imports-c.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-debug/c-imports-a.js b/packages/commonjs/test/fixtures/function/strict-requires-debug/c-imports-a.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-debug/c-imports-a.js rename to packages/commonjs/test/fixtures/function/strict-requires-debug/c-imports-a.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-debug/main.js b/packages/commonjs/test/fixtures/function/strict-requires-debug/main.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-debug/main.js rename to packages/commonjs/test/fixtures/function/strict-requires-debug/main.js 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-require-semantic-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-require-semantic-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-require-semantic-exportmode-exports/_config.js b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-exports/_config.js similarity index 59% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-exports/_config.js rename to packages/commonjs/test/fixtures/function/strict-requires-exportmode-exports/_config.js index 683e0b747..350658699 100644 --- a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-exports/_config.js +++ b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-exports/_config.js @@ -1,6 +1,6 @@ module.exports = { description: 'supports using function wrappers for modules for export mode "exports"', pluginOptions: { - strictRequires: ['fixtures/function/strict-require-semantic-exportmode-exports/*E*.js'] + strictRequires: ['fixtures/function/strict-requires-exportmode-exports/*E*.js'] } }; diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-exports/assignExports.js b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-exports/assignExports.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-exports/assignExports.js rename to packages/commonjs/test/fixtures/function/strict-requires-exportmode-exports/assignExports.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-exports/compiledEsm.js b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-exports/compiledEsm.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-exports/compiledEsm.js rename to packages/commonjs/test/fixtures/function/strict-requires-exportmode-exports/compiledEsm.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-exports/main.js b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-exports/main.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-exports/main.js rename to packages/commonjs/test/fixtures/function/strict-requires-exportmode-exports/main.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-exports/wrappedExports.js b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-exports/wrappedExports.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-exports/wrappedExports.js rename to packages/commonjs/test/fixtures/function/strict-requires-exportmode-exports/wrappedExports.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-module/_config.js b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-module/_config.js similarity index 59% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-module/_config.js rename to packages/commonjs/test/fixtures/function/strict-requires-exportmode-module/_config.js index dda61ff86..6ca355ff8 100644 --- a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-module/_config.js +++ b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-module/_config.js @@ -1,6 +1,6 @@ module.exports = { description: 'supports using function wrappers for modules for export mode "module"', pluginOptions: { - strictRequires: ['fixtures/function/strict-require-semantic-exportmode-module/*E*.js'] + strictRequires: ['fixtures/function/strict-requires-exportmode-module/*E*.js'] } }; diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-module/assignModuleAndExports.js b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-module/assignModuleAndExports.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-module/assignModuleAndExports.js rename to packages/commonjs/test/fixtures/function/strict-requires-exportmode-module/assignModuleAndExports.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-module/assignModuleExports.js b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-module/assignModuleExports.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-module/assignModuleExports.js rename to packages/commonjs/test/fixtures/function/strict-requires-exportmode-module/assignModuleExports.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-module/main.js b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-module/main.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-module/main.js rename to packages/commonjs/test/fixtures/function/strict-requires-exportmode-module/main.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-module/wrappedModuleExports.js b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-module/wrappedModuleExports.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-module/wrappedModuleExports.js rename to packages/commonjs/test/fixtures/function/strict-requires-exportmode-module/wrappedModuleExports.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-replace/_config.js b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-replace/_config.js similarity index 59% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-replace/_config.js rename to packages/commonjs/test/fixtures/function/strict-requires-exportmode-replace/_config.js index 4f1cd0f78..1766448d1 100644 --- a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-replace/_config.js +++ b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-replace/_config.js @@ -1,6 +1,6 @@ module.exports = { description: 'supports using function wrappers for modules for export mode "replace"', pluginOptions: { - strictRequires: ['fixtures/function/strict-require-semantic-exportmode-replace/*E*.js'] + strictRequires: ['fixtures/function/strict-requires-exportmode-replace/*E*.js'] } }; diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-replace/main.js b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-replace/main.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-replace/main.js rename to packages/commonjs/test/fixtures/function/strict-requires-exportmode-replace/main.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-replace/replaceModuleExports.js b/packages/commonjs/test/fixtures/function/strict-requires-exportmode-replace/replaceModuleExports.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-exportmode-replace/replaceModuleExports.js rename to packages/commonjs/test/fixtures/function/strict-requires-exportmode-replace/replaceModuleExports.js diff --git a/packages/commonjs/test/fixtures/function/strict-requires-file-without-module-type/_config.js b/packages/commonjs/test/fixtures/function/strict-requires-file-without-module-type/_config.js new file mode 100644 index 000000000..aa3d115c7 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/strict-requires-file-without-module-type/_config.js @@ -0,0 +1,7 @@ +module.exports = { + description: + 'identifies files without module features as commonjs if they are required by another file', + pluginOptions: { + strictRequires: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/strict-requires-file-without-module-type/error.js b/packages/commonjs/test/fixtures/function/strict-requires-file-without-module-type/error.js new file mode 100644 index 000000000..85dbb8dd0 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/strict-requires-file-without-module-type/error.js @@ -0,0 +1 @@ +throw new Error('FAIL'); diff --git a/packages/commonjs/test/fixtures/function/strict-requires-file-without-module-type/main.js b/packages/commonjs/test/fixtures/function/strict-requires-file-without-module-type/main.js new file mode 100644 index 000000000..c10a2ca10 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/strict-requires-file-without-module-type/main.js @@ -0,0 +1,2 @@ +// eslint-disable-next-line global-require +t.is(0 && require('./error.js'), 0); diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-from-esm/_config.js b/packages/commonjs/test/fixtures/function/strict-requires-from-esm/_config.js similarity index 55% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-from-esm/_config.js rename to packages/commonjs/test/fixtures/function/strict-requires-from-esm/_config.js index 9ba78e6a5..f445b5c88 100644 --- a/packages/commonjs/test/fixtures/function/strict-require-semantic-from-esm/_config.js +++ b/packages/commonjs/test/fixtures/function/strict-requires-from-esm/_config.js @@ -1,6 +1,6 @@ module.exports = { description: 'handles importing wrapped modules from ESM', pluginOptions: { - strictRequires: ['fixtures/function/strict-require-semantic-from-esm/strict.js'] + strictRequires: ['fixtures/function/strict-requires-from-esm/strict.js'] } }; diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-from-esm/dep.js b/packages/commonjs/test/fixtures/function/strict-requires-from-esm/dep.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-from-esm/dep.js rename to packages/commonjs/test/fixtures/function/strict-requires-from-esm/dep.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-from-esm/main.js b/packages/commonjs/test/fixtures/function/strict-requires-from-esm/main.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-from-esm/main.js rename to packages/commonjs/test/fixtures/function/strict-requires-from-esm/main.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-from-esm/strict.js b/packages/commonjs/test/fixtures/function/strict-requires-from-esm/strict.js similarity index 100% rename from packages/commonjs/test/fixtures/function/strict-require-semantic-from-esm/strict.js rename to packages/commonjs/test/fixtures/function/strict-requires-from-esm/strict.js diff --git a/packages/commonjs/test/fixtures/function/strict-require-semantic-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-require-semantic-entry/_config.js rename to packages/commonjs/test/fixtures/function/strict-requires-multiple-entry/_config.js index 757ac2cab..6e3b65ee2 100644 --- a/packages/commonjs/test/fixtures/function/strict-require-semantic-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-require-semantic-entry/main.js', - 'fixtures/function/strict-require-semantic-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-require-semantic-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-require-semantic-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-require-semantic-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 c9311e099..260d58e48 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -3588,9 +3588,7 @@ Generated by [AVA](https://avajs.dev). ␊ var require$$5 = 'bar';␊ ␊ - var none = /*#__PURE__*/Object.freeze({␊ - __proto__: null␊ - });␊ + var none = {};␊ ␊ const externalNamed = require$$0;␊ const externalMixed = require$$1;␊ @@ -3698,11 +3696,7 @@ Generated by [AVA](https://avajs.dev). ␊ var require$$5 = /*@__PURE__*/getAugmentedNamespace(_default$1);␊ ␊ - var none = /*#__PURE__*/Object.freeze({␊ - __proto__: null␊ - });␊ - ␊ - var require$$6 = /*@__PURE__*/getAugmentedNamespace(none);␊ + var none = {};␊ ␊ const externalNamed = require$$0;␊ const externalMixed = require$$1;␊ @@ -3711,7 +3705,7 @@ Generated by [AVA](https://avajs.dev). const namedExports = require$$3;␊ const mixedExports = require$$4;␊ const defaultExport = require$$5;␊ - const noExports = require$$6;␊ + const noExports = none;␊ ␊ t.deepEqual(namedExports, { foo: 'foo' }, 'named exports');␊ t.deepEqual(mixedExports, { foo: 'foo', default: 'bar' }, 'mixed exports');␊ @@ -4004,9 +3998,7 @@ Generated by [AVA](https://avajs.dev). 'default': _default␊ });␊ ␊ - var none = /*#__PURE__*/Object.freeze({␊ - __proto__: null␊ - });␊ + var none = {};␊ ␊ const externalNamed = externalEsmNamed__namespace;␊ const externalMixed = externalEsmMixed__namespace;␊ @@ -4085,9 +4077,7 @@ Generated by [AVA](https://avajs.dev). ␊ var require$$5 = 'bar';␊ ␊ - var none = /*#__PURE__*/Object.freeze({␊ - __proto__: null␊ - });␊ + var none = {};␊ ␊ const externalNamed = require$$0;␊ const externalMixed = require$$1;␊ @@ -4235,11 +4225,7 @@ Generated by [AVA](https://avajs.dev). ␊ var require$$5 = /*@__PURE__*/getAugmentedNamespace(_default$1);␊ ␊ - var none = /*#__PURE__*/Object.freeze({␊ - __proto__: null␊ - });␊ - ␊ - var require$$6 = /*@__PURE__*/getAugmentedNamespace(none);␊ + var none = {};␊ ␊ const externalNamed = require$$0;␊ const externalMixed = require$$1;␊ @@ -4248,7 +4234,7 @@ Generated by [AVA](https://avajs.dev). const namedExports = require$$3;␊ const mixedExports = require$$4;␊ const defaultExport = require$$5;␊ - const noExports = require$$6;␊ + const noExports = none;␊ ␊ t.deepEqual(namedExports, { foo: 'foo' }, 'named exports');␊ t.deepEqual(mixedExports, { foo: 'foo', default: 'bar' }, 'mixed exports');␊ @@ -5442,7 +5428,7 @@ Generated by [AVA](https://avajs.dev). `, } -## strict-require-semantic-auto +## strict-requires-auto > Snapshot 1 @@ -5491,7 +5477,7 @@ Generated by [AVA](https://avajs.dev). `, } -## strict-require-semantic-circular +## strict-requires-circular > Snapshot 1 @@ -5527,7 +5513,7 @@ Generated by [AVA](https://avajs.dev). `, } -## strict-require-semantic-cycle-detection +## strict-requires-cycle-detection > Snapshot 1 @@ -5576,7 +5562,7 @@ Generated by [AVA](https://avajs.dev). `, } -## strict-require-semantic-debug +## strict-requires-debug > Snapshot 1 @@ -5625,7 +5611,7 @@ Generated by [AVA](https://avajs.dev). `, } -## strict-require-semantic-debug-none +## strict-requires-debug-none > Snapshot 1 @@ -5638,12 +5624,12 @@ Generated by [AVA](https://avajs.dev). `, } -## strict-require-semantic-entry +## strict-requires-entry > Snapshot 1 { - 'generated-main.js': `'use strict';␊ + 'main.js': `'use strict';␊ ␊ var main = {};␊ ␊ @@ -5656,29 +5642,13 @@ 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-require-semantic-exportmode-exports +## strict-requires-exportmode-exports > Snapshot 1 @@ -5754,7 +5724,7 @@ Generated by [AVA](https://avajs.dev). `, } -## strict-require-semantic-exportmode-module +## strict-requires-exportmode-module > Snapshot 1 @@ -5830,7 +5800,7 @@ Generated by [AVA](https://avajs.dev). `, } -## strict-require-semantic-exportmode-replace +## strict-requires-exportmode-replace > Snapshot 1 @@ -5862,7 +5832,32 @@ Generated by [AVA](https://avajs.dev). `, } -## strict-require-semantic-from-esm +## strict-requires-file-without-module-type + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var main = {};␊ + ␊ + var hasRequiredMain;␊ + ␊ + function requireMain () {␊ + if (hasRequiredMain) return main;␊ + hasRequiredMain = 1;␊ + // eslint-disable-next-line global-require␊ + t.is(0 , 0);␊ + return main;␊ + }␊ + ␊ + var mainExports = requireMain();␊ + ␊ + module.exports = mainExports;␊ + `, + } + +## strict-requires-from-esm > Snapshot 1 @@ -5888,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 @@ -6738,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 71ddc9153..4ec48d232 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ