diff --git a/packages/commonjs/src/dynamic-modules.js b/packages/commonjs/src/dynamic-modules.js index 82f34c78e..c00c21511 100644 --- a/packages/commonjs/src/dynamic-modules.js +++ b/packages/commonjs/src/dynamic-modules.js @@ -63,6 +63,9 @@ export function getDynamicRequireModules(patterns, dynamicRequireRoot) { const FAILED_REQUIRE_ERROR = `throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');`; +export const COMMONJS_REQUIRE_EXPORT = 'commonjsRequire'; +export const CREATE_COMMONJS_REQUIRE_EXPORT = 'createCommonjsRequire'; + export function getDynamicModuleRegistry( isDynamicRequireModulesEnabled, dynamicRequireModules, @@ -70,7 +73,7 @@ export function getDynamicModuleRegistry( ignoreDynamicRequires ) { if (!isDynamicRequireModulesEnabled) { - return `export function commonjsRequire(path) { + return `export function ${COMMONJS_REQUIRE_EXPORT}(path) { ${FAILED_REQUIRE_ERROR} }`; } @@ -100,25 +103,25 @@ ${dynamicModuleProps} }); } -export function commonjsRequire(path, originalModuleDir) { - var resolvedPath = commonjsResolveImpl(path, originalModuleDir); - if (resolvedPath !== null) { - return getDynamicModules()[resolvedPath](); +export function ${CREATE_COMMONJS_REQUIRE_EXPORT}(originalModuleDir) { + function handleRequire(path) { + var resolvedPath = commonjsResolve(path, originalModuleDir); + if (resolvedPath !== null) { + return getDynamicModules()[resolvedPath](); + } + ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR} } - ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR} -} - -function commonjsResolve (path, originalModuleDir) { - const resolvedPath = commonjsResolveImpl(path, originalModuleDir); - if (resolvedPath !== null) { - return resolvedPath; + handleRequire.resolve = function (path) { + var resolvedPath = commonjsResolve(path, originalModuleDir); + if (resolvedPath !== null) { + return resolvedPath; + } + return require.resolve(path); } - return require.resolve(path); + return handleRequire; } -commonjsRequire.resolve = commonjsResolve; - -function commonjsResolveImpl (path, originalModuleDir) { +function commonjsResolve (path, originalModuleDir) { var shouldTryNodeModules = isPossibleNodeModulesPath(path); path = normalize(path); var relPath; diff --git a/packages/commonjs/src/generate-imports.js b/packages/commonjs/src/generate-imports.js index 7fb85393d..9c3aaa02e 100644 --- a/packages/commonjs/src/generate-imports.js +++ b/packages/commonjs/src/generate-imports.js @@ -1,3 +1,4 @@ +import { COMMONJS_REQUIRE_EXPORT, CREATE_COMMONJS_REQUIRE_EXPORT } from './dynamic-modules'; import { DYNAMIC_MODULES_ID, EXPORTS_SUFFIX, @@ -92,14 +93,16 @@ export function getRequireHandlers() { resolveRequireSourcesAndGetMeta, needsRequireWrapper, isEsModule, - usesRequire, + isDynamicRequireModulesEnabled, getIgnoreTryCatchRequireStatementMode ) { const imports = []; imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`); - if (usesRequire) { + if (dynamicRequireName) { imports.push( - `import { commonjsRequire as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";` + `import { ${ + isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT + } as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";` ); } if (exportMode === 'module') { diff --git a/packages/commonjs/src/transform-commonjs.js b/packages/commonjs/src/transform-commonjs.js index 3fb8602cd..851115b47 100644 --- a/packages/commonjs/src/transform-commonjs.js +++ b/packages/commonjs/src/transform-commonjs.js @@ -16,6 +16,7 @@ import { isTruthy, KEY_COMPILED_ESM } from './ast-utils'; +import { COMMONJS_REQUIRE_EXPORT, CREATE_COMMONJS_REQUIRE_EXPORT } from './dynamic-modules'; import { rewriteExportsAndGetExportsBlock, wrapCode } from './generate-exports'; import { getRequireHandlers, @@ -201,12 +202,6 @@ export default async function transformCommonjs( checkDynamicRequire(node.start); uses.require = true; const requireNode = node.callee.object; - magicString.appendLeft( - node.end - 1, - `,${JSON.stringify( - dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath - )}` - ); replacedDynamicRequires.push(requireNode); return; } @@ -221,12 +216,6 @@ export default async function transformCommonjs( if (hasDynamicArguments(node)) { if (isDynamicRequireModulesEnabled) { checkDynamicRequire(node.start); - magicString.appendLeft( - node.end - 1, - `, ${JSON.stringify( - dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath - )}` - ); } if (!ignoreDynamicRequires) { replacedDynamicRequires.push(node.callee); @@ -400,7 +389,13 @@ export default async function transformCommonjs( const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`); const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`); const helpersName = deconflict([scope], globals, 'commonjsHelpers'); - const dynamicRequireName = deconflict([scope], globals, 'commonjsRequire'); + const dynamicRequireName = + replacedDynamicRequires.length > 0 && + deconflict( + [scope], + globals, + isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT + ); const deconflictedExportNames = Object.create(null); for (const [exportName, { scopes }] of exportsAssignmentsByName) { deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName); @@ -412,10 +407,17 @@ export default async function transformCommonjs( }); } for (const node of replacedDynamicRequires) { - magicString.overwrite(node.start, node.end, dynamicRequireName, { - contentOnly: true, - storeName: true - }); + magicString.overwrite( + node.start, + node.end, + isDynamicRequireModulesEnabled + ? `${dynamicRequireName}(${JSON.stringify(virtualDynamicRequirePath)})` + : dynamicRequireName, + { + contentOnly: true, + storeName: true + } + ); } // We cannot wrap ES/mixed modules @@ -470,7 +472,7 @@ export default async function transformCommonjs( resolveRequireSourcesAndGetMeta, needsRequireWrapper, isEsModule, - uses.require, + isDynamicRequireModulesEnabled, getIgnoreTryCatchRequireStatementMode ); const exportBlock = isEsModule diff --git a/packages/commonjs/test/fixtures/form/constant-template-literal/output.js b/packages/commonjs/test/fixtures/form/constant-template-literal/output.js index 08f8bd02d..ed766a8a3 100644 --- a/packages/commonjs/test/fixtures/form/constant-template-literal/output.js +++ b/packages/commonjs/test/fixtures/form/constant-template-literal/output.js @@ -1,5 +1,4 @@ import * as commonjsHelpers from "_commonjsHelpers.js"; -import { commonjsRequire as commonjsRequire } from "_commonjs-dynamic-modules"; import { __exports as input } from "\u0000fixtures/form/constant-template-literal/input.js?commonjs-exports" import require$$0 from "\u0000CWD/fixtures/form/constant-template-literal/tape.js?commonjs-proxy"; diff --git a/packages/commonjs/test/fixtures/form/ignore-ids-function/output.js b/packages/commonjs/test/fixtures/form/ignore-ids-function/output.js index de90c6826..9bf413331 100644 --- a/packages/commonjs/test/fixtures/form/ignore-ids-function/output.js +++ b/packages/commonjs/test/fixtures/form/ignore-ids-function/output.js @@ -1,5 +1,4 @@ import * as commonjsHelpers from "_commonjsHelpers.js"; -import { commonjsRequire as commonjsRequire } from "_commonjs-dynamic-modules"; import { __exports as input } from "\u0000fixtures/form/ignore-ids-function/input.js?commonjs-exports" import require$$0 from "\u0000CWD/fixtures/form/ignore-ids-function/bar.js?commonjs-proxy"; diff --git a/packages/commonjs/test/fixtures/form/ignore-ids/output.js b/packages/commonjs/test/fixtures/form/ignore-ids/output.js index e37256ccb..7bf69819c 100644 --- a/packages/commonjs/test/fixtures/form/ignore-ids/output.js +++ b/packages/commonjs/test/fixtures/form/ignore-ids/output.js @@ -1,5 +1,4 @@ import * as commonjsHelpers from "_commonjsHelpers.js"; -import { commonjsRequire as commonjsRequire } from "_commonjs-dynamic-modules"; import { __exports as input } from "\u0000fixtures/form/ignore-ids/input.js?commonjs-exports" import require$$0 from "\u0000CWD/fixtures/form/ignore-ids/bar.js?commonjs-proxy"; diff --git a/packages/commonjs/test/fixtures/form/multi-entry-module-exports/output1.js b/packages/commonjs/test/fixtures/form/multi-entry-module-exports/output1.js index 5bf32420e..214e817e3 100644 --- a/packages/commonjs/test/fixtures/form/multi-entry-module-exports/output1.js +++ b/packages/commonjs/test/fixtures/form/multi-entry-module-exports/output1.js @@ -1,5 +1,4 @@ import * as commonjsHelpers from "_commonjsHelpers.js"; -import { commonjsRequire as commonjsRequire } from "_commonjs-dynamic-modules"; import require$$0 from "\u0000CWD/fixtures/form/multi-entry-module-exports/input2.js?commonjs-proxy"; const t2 = require$$0; diff --git a/packages/commonjs/test/fixtures/form/multiple-var-declarations-b/output.js b/packages/commonjs/test/fixtures/form/multiple-var-declarations-b/output.js index 05bb39f86..93cf331d5 100644 --- a/packages/commonjs/test/fixtures/form/multiple-var-declarations-b/output.js +++ b/packages/commonjs/test/fixtures/form/multiple-var-declarations-b/output.js @@ -1,5 +1,4 @@ import * as commonjsHelpers from "_commonjsHelpers.js"; -import { commonjsRequire as commonjsRequire } from "_commonjs-dynamic-modules"; import { __exports as input } from "\u0000fixtures/form/multiple-var-declarations-b/input.js?commonjs-exports" import require$$0 from "\u0000CWD/fixtures/form/multiple-var-declarations-b/a.js?commonjs-proxy"; diff --git a/packages/commonjs/test/fixtures/form/multiple-var-declarations-c/output.js b/packages/commonjs/test/fixtures/form/multiple-var-declarations-c/output.js index 74dc40f2e..d5d652e50 100644 --- a/packages/commonjs/test/fixtures/form/multiple-var-declarations-c/output.js +++ b/packages/commonjs/test/fixtures/form/multiple-var-declarations-c/output.js @@ -1,5 +1,4 @@ import * as commonjsHelpers from "_commonjsHelpers.js"; -import { commonjsRequire as commonjsRequire } from "_commonjs-dynamic-modules"; import { __exports as input } from "\u0000fixtures/form/multiple-var-declarations-c/input.js?commonjs-exports" import require$$0 from "\u0000CWD/fixtures/form/multiple-var-declarations-c/b.js?commonjs-proxy"; diff --git a/packages/commonjs/test/fixtures/form/multiple-var-declarations/output.js b/packages/commonjs/test/fixtures/form/multiple-var-declarations/output.js index 38a4a54aa..3e05dfdb0 100644 --- a/packages/commonjs/test/fixtures/form/multiple-var-declarations/output.js +++ b/packages/commonjs/test/fixtures/form/multiple-var-declarations/output.js @@ -1,5 +1,4 @@ import * as commonjsHelpers from "_commonjsHelpers.js"; -import { commonjsRequire as commonjsRequire } from "_commonjs-dynamic-modules"; import { __exports as input } from "\u0000fixtures/form/multiple-var-declarations/input.js?commonjs-exports" import require$$0 from "\u0000CWD/fixtures/form/multiple-var-declarations/a.js?commonjs-proxy"; import require$$1 from "\u0000CWD/fixtures/form/multiple-var-declarations/b.js?commonjs-proxy"; diff --git a/packages/commonjs/test/fixtures/form/no-exports-entry/output.js b/packages/commonjs/test/fixtures/form/no-exports-entry/output.js index b10040f7b..252fd167f 100644 --- a/packages/commonjs/test/fixtures/form/no-exports-entry/output.js +++ b/packages/commonjs/test/fixtures/form/no-exports-entry/output.js @@ -1,5 +1,4 @@ import * as commonjsHelpers from "_commonjsHelpers.js"; -import { commonjsRequire as commonjsRequire } from "_commonjs-dynamic-modules"; import { __exports as input_1 } from "\u0000fixtures/form/no-exports-entry/input.js?commonjs-exports" import require$$0 from "\u0000CWD/fixtures/form/no-exports-entry/dummy.js?commonjs-proxy"; diff --git a/packages/commonjs/test/fixtures/form/node-require-methods/output.js b/packages/commonjs/test/fixtures/form/node-require-methods/output.js index b1ceb2c2a..6aff4d3e1 100644 --- a/packages/commonjs/test/fixtures/form/node-require-methods/output.js +++ b/packages/commonjs/test/fixtures/form/node-require-methods/output.js @@ -1,5 +1,4 @@ import * as commonjsHelpers from "_commonjsHelpers.js"; -import { commonjsRequire as commonjsRequire } from "_commonjs-dynamic-modules"; import { __exports as input } from "\u0000fixtures/form/node-require-methods/input.js?commonjs-exports" var getFilePath = input.getFilePath = function getFilePath(someFile) { diff --git a/packages/commonjs/test/fixtures/form/require-collision/output.js b/packages/commonjs/test/fixtures/form/require-collision/output.js index d760dbf85..44fd7b388 100644 --- a/packages/commonjs/test/fixtures/form/require-collision/output.js +++ b/packages/commonjs/test/fixtures/form/require-collision/output.js @@ -1,5 +1,4 @@ import * as commonjsHelpers from "_commonjsHelpers.js"; -import { commonjsRequire as commonjsRequire } from "_commonjs-dynamic-modules"; import { __exports as input } from "\u0000fixtures/form/require-collision/input.js?commonjs-exports" import require$$1 from "\u0000CWD/fixtures/form/require-collision/foo.js?commonjs-proxy"; diff --git a/packages/commonjs/test/fixtures/form/unambiguous-with-default-export/output.js b/packages/commonjs/test/fixtures/form/unambiguous-with-default-export/output.js index 51bfef8ef..b2a41f38e 100644 --- a/packages/commonjs/test/fixtures/form/unambiguous-with-default-export/output.js +++ b/packages/commonjs/test/fixtures/form/unambiguous-with-default-export/output.js @@ -1,5 +1,4 @@ import * as commonjsHelpers from "_commonjsHelpers.js"; -import { commonjsRequire as commonjsRequire } from "_commonjs-dynamic-modules"; import { __exports as input } from "\u0000fixtures/form/unambiguous-with-default-export/input.js?commonjs-exports" import "\u0000CWD/fixtures/form/unambiguous-with-default-export/foo.js?commonjs-proxy"; diff --git a/packages/commonjs/test/fixtures/form/unambiguous-with-import/output.js b/packages/commonjs/test/fixtures/form/unambiguous-with-import/output.js index 8b277a597..76b7883b0 100644 --- a/packages/commonjs/test/fixtures/form/unambiguous-with-import/output.js +++ b/packages/commonjs/test/fixtures/form/unambiguous-with-import/output.js @@ -1,5 +1,4 @@ import * as commonjsHelpers from "_commonjsHelpers.js"; -import { commonjsRequire as commonjsRequire } from "_commonjs-dynamic-modules"; import { __exports as input } from "\u0000fixtures/form/unambiguous-with-import/input.js?commonjs-exports" import "\u0000CWD/fixtures/form/unambiguous-with-import/foo.js?commonjs-proxy"; diff --git a/packages/commonjs/test/fixtures/form/unambiguous-with-named-export/output.js b/packages/commonjs/test/fixtures/form/unambiguous-with-named-export/output.js index c84e44011..a31cc3202 100644 --- a/packages/commonjs/test/fixtures/form/unambiguous-with-named-export/output.js +++ b/packages/commonjs/test/fixtures/form/unambiguous-with-named-export/output.js @@ -1,5 +1,4 @@ import * as commonjsHelpers from "_commonjsHelpers.js"; -import { commonjsRequire as commonjsRequire } from "_commonjs-dynamic-modules"; import { __exports as input } from "\u0000fixtures/form/unambiguous-with-named-export/input.js?commonjs-exports" import "\u0000CWD/fixtures/form/unambiguous-with-named-export/foo.js?commonjs-proxy"; diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-alias-hack/_config.js b/packages/commonjs/test/fixtures/function/dynamic-require-alias-hack/_config.js new file mode 100755 index 000000000..f52126137 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-alias-hack/_config.js @@ -0,0 +1,7 @@ +module.exports = { + description: 'resolves both windows and posix paths', + pluginOptions: { + dynamicRequireTargets: ['fixtures/function/dynamic-require-alias-hack/stub.js'], + ignoreDynamicRequires: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-alias-hack/main.js b/packages/commonjs/test/fixtures/function/dynamic-require-alias-hack/main.js new file mode 100755 index 000000000..636d42e03 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-alias-hack/main.js @@ -0,0 +1,9 @@ +/* eslint-disable global-require */ +// noinspection UnnecessaryLocalVariableJS + +// A hack used in many old libraries, saying "workaround to exclude package from browserify list." +// Will bypass rollup-commonjs finding out that this is a require that should not go through the plugin, and will do an infinite search. +const _require = require; +const buffer = _require('buffer'); + +t.is(buffer, require('buffer')); diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-alias-hack/stub.js b/packages/commonjs/test/fixtures/function/dynamic-require-alias-hack/stub.js new file mode 100755 index 000000000..28a021e39 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-alias-hack/stub.js @@ -0,0 +1,3 @@ +module.exports = function () { + return 'Hello there'; +}; diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index 70584b812..1e80780c8 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -417,25 +417,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -508,7 +508,7 @@ Generated by [AVA](https://avajs.dev). let message;␊ ␊ function takeModule(withName) {␊ - return commonjsRequire(`./${withName}`, "/fixtures/function/dynamic-module-require");␊ + return createCommonjsRequire("/fixtures/function/dynamic-module-require")(`./${withName}`);␊ }␊ ␊ try {␊ @@ -551,25 +551,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -642,7 +642,7 @@ Generated by [AVA](https://avajs.dev). let message;␊ ␊ function takeModule(withName) {␊ - return commonjsRequire(`./${withName}`, "/fixtures/function/dynamic-require");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require")(`./${withName}`);␊ }␊ ␊ try {␊ @@ -665,6 +665,8 @@ Generated by [AVA](https://avajs.dev). { 'main.js': `'use strict';␊ ␊ + var main = {};␊ + ␊ var direct;␊ var hasRequiredDirect;␊ ␊ @@ -705,25 +707,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -789,14 +791,12 @@ Generated by [AVA](https://avajs.dev). return path;␊ }␊ ␊ - var main = {};␊ - ␊ var submodule = {};␊ ␊ /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule(name) {␊ - return commonjsRequire(name, "/fixtures/function/dynamic-require-absolute-import/sub");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-absolute-import/sub")(name);␊ }␊ ␊ submodule.moduleDirect = takeModule('module/direct');␊ @@ -815,6 +815,140 @@ Generated by [AVA](https://avajs.dev). `, } +## dynamic-require-alias-hack + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var require$$0 = require('buffer');␊ + ␊ + function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }␊ + ␊ + var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0);␊ + ␊ + var stub;␊ + var hasRequiredStub;␊ + ␊ + function requireStub () {␊ + if (hasRequiredStub) return stub;␊ + hasRequiredStub = 1;␊ + stub = function () {␊ + return 'Hello there';␊ + };␊ + return stub;␊ + }␊ + ␊ + var dynamicModules;␊ + ␊ + function getDynamicModules() {␊ + return dynamicModules || (dynamicModules = {␊ + "/fixtures/function/dynamic-require-alias-hack/stub.js": requireStub␊ + });␊ + }␊ + ␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + return require(path);␊ + }␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ + }␊ + ␊ + function commonjsResolve (path, originalModuleDir) {␊ + var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ + path = normalize(path);␊ + var relPath;␊ + if (path[0] === '/') {␊ + originalModuleDir = '';␊ + }␊ + var modules = getDynamicModules();␊ + var checkedExtensions = ['', '.js', '.json'];␊ + while (true) {␊ + if (!shouldTryNodeModules) {␊ + relPath = normalize(originalModuleDir + '/' + path);␊ + } else {␊ + relPath = normalize(originalModuleDir + '/node_modules/' + path);␊ + }␊ + ␊ + if (relPath.endsWith('/..')) {␊ + break; // Travelled too far up, avoid infinite loop␊ + }␊ + ␊ + for (var extensionIndex = 0; extensionIndex < checkedExtensions.length; extensionIndex++) {␊ + var resolvedPath = relPath + checkedExtensions[extensionIndex];␊ + if (modules[resolvedPath]) {␊ + return resolvedPath;␊ + }␊ + }␊ + if (!shouldTryNodeModules) break;␊ + var nextDir = normalize(originalModuleDir + '/..');␊ + if (nextDir === originalModuleDir) break;␊ + originalModuleDir = nextDir;␊ + }␊ + return null;␊ + }␊ + ␊ + function isPossibleNodeModulesPath (modulePath) {␊ + var c0 = modulePath[0];␊ + if (c0 === '/' || c0 === '\\\\') return false;␊ + var c1 = modulePath[1], c2 = modulePath[2];␊ + if ((c0 === '.' && (!c1 || c1 === '/' || c1 === '\\\\')) ||␊ + (c0 === '.' && c1 === '.' && (!c2 || c2 === '/' || c2 === '\\\\'))) return false;␊ + if (c1 === ':' && (c2 === '/' || c2 === '\\\\')) return false;␊ + return true;␊ + }␊ + ␊ + function normalize (path) {␊ + path = path.replace(/\\\\/g, '/');␊ + var parts = path.split('/');␊ + var slashed = parts[0] === '';␊ + for (var i = 1; i < parts.length; i++) {␊ + if (parts[i] === '.' || parts[i] === '') {␊ + parts.splice(i--, 1);␊ + }␊ + }␊ + for (var i = 1; i < parts.length; i++) {␊ + if (parts[i] !== '..') continue;␊ + if (i > 0 && parts[i - 1] !== '..' && parts[i - 1] !== '.') {␊ + parts.splice(--i, 2);␊ + i--;␊ + }␊ + }␊ + path = parts.join('/');␊ + if (slashed && path[0] !== '/') path = '/' + path;␊ + else if (path.length === 0) path = '.';␊ + return path;␊ + }␊ + ␊ + var main = {};␊ + ␊ + /* eslint-disable global-require */␊ + ␊ + // noinspection UnnecessaryLocalVariableJS␊ + ␊ + // A hack used in many old libraries, saying "workaround to exclude package from browserify list."␊ + // Will bypass rollup-commonjs finding out that this is a require that should not go through the plugin, and will do an infinite search.␊ + const _require = createCommonjsRequire("/fixtures/function/dynamic-require-alias-hack");␊ + const buffer = _require('buffer');␊ + ␊ + t.is(buffer, require$$0__default["default"]);␊ + ␊ + module.exports = main;␊ + `, + } + ## dynamic-require-code-splitting > Snapshot 1 @@ -851,25 +985,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -941,14 +1075,14 @@ Generated by [AVA](https://avajs.dev). ␊ for (const index of [1, 2]) {␊ try {␊ - message = commonjsRequire(`./target${index}.js`, "/fixtures/function/dynamic-require-code-splitting");␊ + message = createCommonjsRequire("/fixtures/function/dynamic-require-code-splitting")(`./target${index}.js`);␊ } catch (err) {␊ ({ message } = err);␊ }␊ t.is(message, index.toString());␊ }␊ ␊ - exports.commonjsRequire = commonjsRequire;␊ + exports.createCommonjsRequire = createCommonjsRequire;␊ `, 'main.js': `'use strict';␊ ␊ @@ -960,7 +1094,7 @@ Generated by [AVA](https://avajs.dev). ␊ for (const index of [1, 2]) {␊ try {␊ - message = lib2.commonjsRequire(`./target${index}.js`, "/fixtures/function/dynamic-require-code-splitting");␊ + message = lib2.createCommonjsRequire("/fixtures/function/dynamic-require-code-splitting")(`./target${index}.js`);␊ } catch (err) {␊ ({ message } = err);␊ }␊ @@ -1026,25 +1160,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -1113,7 +1247,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule(withName) {␊ - return commonjsRequire(`./${withName}`, "/fixtures/function/dynamic-require-es-entry");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-es-entry")(`./${withName}`);␊ }␊ ␊ var importer = takeModule('submodule.js');␊ @@ -1181,25 +1315,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -1270,7 +1404,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule(withName) {␊ - return commonjsRequire(`./${withName}`, "/fixtures/function/dynamic-require-extensions");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-extensions")(`./${withName}`);␊ }␊ ␊ const withExtension = takeModule('submodule.js');␊ @@ -1379,25 +1513,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -1468,7 +1602,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule(withName) {␊ - return commonjsRequire(`./${withName}`, "/fixtures/function/dynamic-require-globs");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-globs")(`./${withName}`);␊ }␊ ␊ t.is(takeModule('submodule1.js'), 'submodule1');␊ @@ -1521,25 +1655,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -1610,7 +1744,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule(withName) {␊ - return commonjsRequire(withName, "/fixtures/function/dynamic-require-instances");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-instances")(withName);␊ }␊ ␊ takeModule('./direct').value = 'direct-instance';␊ @@ -1645,25 +1779,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -1734,7 +1868,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule(withName) {␊ - return commonjsRequire(`./${withName}`, "/fixtures/function/dynamic-require-json");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-json")(`./${withName}`);␊ }␊ ␊ t.deepEqual(takeModule('dynamic.json'), { value: 'present' });␊ @@ -1822,25 +1956,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -1911,7 +2045,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule$1(name) {␊ - return commonjsRequire(name, "/fixtures/function/dynamic-require-package/sub");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-package/sub")(name);␊ }␊ ␊ var sub = {␊ @@ -1922,7 +2056,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule(name) {␊ - return commonjsRequire(name, "/fixtures/function/dynamic-require-package");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-package")(name);␊ }␊ ␊ t.is(takeModule('.'), 'same-directory');␊ @@ -1945,21 +2079,21 @@ Generated by [AVA](https://avajs.dev). { 'entry.js': `'use strict';␊ ␊ - var entry$1;␊ + var entry$1 = {};␊ + ␊ + var entry;␊ var hasRequiredEntry;␊ ␊ function requireEntry () {␊ - if (hasRequiredEntry) return entry$1;␊ + if (hasRequiredEntry) return entry;␊ hasRequiredEntry = 1;␊ - entry$1 = 'custom-module';␊ - return entry$1;␊ + entry = 'custom-module';␊ + return entry;␊ }␊ ␊ - var entry = {};␊ - ␊ t.is(requireEntry(), 'custom-module');␊ ␊ - module.exports = entry;␊ + module.exports = entry$1;␊ `, } @@ -1999,25 +2133,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -2088,7 +2222,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModuleWithDelimiter(name, delimiter) {␊ - return commonjsRequire(`.${delimiter}${name.replace(/=/g, delimiter)}`, "/fixtures/function/dynamic-require-relative-paths");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-relative-paths")(`.${delimiter}${name.replace(/=/g, delimiter)}`);␊ }␊ ␊ t.is(takeModuleWithDelimiter('sub=submodule.js', '/'), 'submodule');␊ @@ -2150,25 +2284,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -2239,7 +2373,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule$1(name) {␊ - return commonjsRequire(name, "/fixtures/function/dynamic-require-resolve-index/sub");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-resolve-index/sub")(name);␊ }␊ ␊ var sub = {␊ @@ -2250,7 +2384,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule(name) {␊ - return commonjsRequire(name, "/fixtures/function/dynamic-require-resolve-index");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-resolve-index")(name);␊ }␊ ␊ t.is(takeModule('.'), 'same-directory');␊ @@ -2273,18 +2407,7 @@ Generated by [AVA](https://avajs.dev). { 'main.js': `'use strict';␊ ␊ - var customModule = {exports: {}};␊ - ␊ - var hasRequiredCustomModule;␊ - ␊ - function requireCustomModule () {␊ - if (hasRequiredCustomModule) return customModule.exports;␊ - hasRequiredCustomModule = 1;␊ - (function (module) {␊ - module.exports = () => commonjsRequire.resolve('custom-module2',"/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module");␊ - } (customModule));␊ - return customModule.exports;␊ - }␊ + var main = {};␊ ␊ var customModule2;␊ var hasRequiredCustomModule2;␊ @@ -2292,7 +2415,7 @@ Generated by [AVA](https://avajs.dev). function requireCustomModule2 () {␊ if (hasRequiredCustomModule2) return customModule2;␊ hasRequiredCustomModule2 = 1;␊ - customModule2 = () => commonjsRequire.resolve('custom-module',"/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module2");␊ + customModule2 = () => createCommonjsRequire("/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module2").resolve('custom-module');␊ return customModule2;␊ }␊ ␊ @@ -2307,25 +2430,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -2391,7 +2514,18 @@ Generated by [AVA](https://avajs.dev). return path;␊ }␊ ␊ - var main = {};␊ + var customModule = {exports: {}};␊ + ␊ + var hasRequiredCustomModule;␊ + ␊ + function requireCustomModule () {␊ + if (hasRequiredCustomModule) return customModule.exports;␊ + hasRequiredCustomModule = 1;␊ + (function (module) {␊ + module.exports = () => createCommonjsRequire("/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module").resolve('custom-module2');␊ + } (customModule));␊ + return customModule.exports;␊ + }␊ ␊ t.is(␊ requireCustomModule()(),␊ @@ -2433,25 +2567,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -2524,7 +2658,7 @@ Generated by [AVA](https://avajs.dev). let message;␊ ␊ function takeModule(withName) {␊ - return commonjsRequire(`./${withName}`, "/");␊ + return createCommonjsRequire("/")(`./${withName}`);␊ }␊ ␊ try {␊ @@ -2547,6 +2681,8 @@ Generated by [AVA](https://avajs.dev). { 'main.js': `'use strict';␊ ␊ + var main = {};␊ + ␊ var customModule = {exports: {}};␊ ␊ var circular = {};␊ @@ -2589,8 +2725,6 @@ Generated by [AVA](https://avajs.dev). return customModule.exports;␊ }␊ ␊ - var main = {};␊ - ␊ /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ const custom = requireCustomModule();␊ @@ -2664,25 +2798,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -2753,7 +2887,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule$1(name) {␊ - return commonjsRequire(name, "/fixtures/function/dynamic-require-slash-access/sub");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-slash-access/sub")(name);␊ }␊ ␊ var sub = {␊ @@ -2764,7 +2898,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule(name) {␊ - return commonjsRequire(name, "/fixtures/function/dynamic-require-slash-access");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-slash-access")(name);␊ }␊ ␊ t.is(takeModule('.'), 'same-directory', '.');␊ @@ -2808,25 +2942,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + return require(path);␊ }␊ - return require(path);␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -2897,7 +3031,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule(withName) {␊ - return commonjsRequire(withName, "/fixtures/function/dynamic-require-targets-fallback");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-targets-fallback")(withName);␊ }␊ ␊ t.is(takeModule('./dep1.js'), 'dep');␊ @@ -2933,25 +3067,25 @@ Generated by [AVA](https://avajs.dev). });␊ }␊ ␊ - function commonjsRequire(path, originalModuleDir) {␊ - var resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return getDynamicModules()[resolvedPath]();␊ + function createCommonjsRequire(originalModuleDir) {␊ + function handleRequire(path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return getDynamicModules()[resolvedPath]();␊ + }␊ + throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ }␊ - throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊ + handleRequire.resolve = function (path) {␊ + var resolvedPath = commonjsResolve(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + };␊ + return handleRequire;␊ }␊ ␊ function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir) {␊ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ path = normalize(path);␊ var relPath;␊ @@ -3022,7 +3156,7 @@ Generated by [AVA](https://avajs.dev). /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ function takeModule(withName) {␊ - return commonjsRequire(withName, "/fixtures/function/dynamic-require-targets-no-fallback");␊ + return createCommonjsRequire("/fixtures/function/dynamic-require-targets-no-fallback")(withName);␊ }␊ ␊ t.is(takeModule('./dep1.js'), 'dep');␊ diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index 9a637b0f7..56d991917 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ