diff --git a/packages/commonjs/README.md b/packages/commonjs/README.md index dd3bddd7e..6aff1ee2e 100644 --- a/packages/commonjs/README.md +++ b/packages/commonjs/README.md @@ -134,6 +134,25 @@ Due to the conversion of `require` to a static `import` - the call is hoisted to - `string[]`: Pass an array containing the IDs to left unconverted. - `((id: string) => boolean|'remove')`: Pass a function that control individual IDs. +### `ignoreDynamicRequires` + +Type: `boolean` +Default: false + +Some `require` calls cannot be resolved statically to be translated to imports, e.g. + +```js +function wrappedRequire(target) { + return require(target); +} +wrappedRequire('foo'); +wrappedRequire('bar'); +``` + +When this option is set to `false`, the generated code will either directly throw an error when such a call is encountered or, when `dynamicRequireTargets` is used, when such a call cannot be resolved with a configured dynamic require target. + +Setting this option to `true` will instead leave the `require` call in the code or use it as a fallback for `dynamicRequireTargets`. + ### `esmExternals` Type: `boolean | string[] | ((id: string) => boolean)` diff --git a/packages/commonjs/src/helpers.js b/packages/commonjs/src/helpers.js index c57f142ee..b4c17ccd2 100644 --- a/packages/commonjs/src/helpers.js +++ b/packages/commonjs/src/helpers.js @@ -48,18 +48,20 @@ export function getAugmentedNamespace(n) { } `; +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.');`; + const HELPER_NON_DYNAMIC = ` export function createCommonjsModule(fn) { var module = { exports: {} } return fn(module, module.exports), module.exports; } -export function commonjsRequire (target) { - throw new Error('Could not dynamically require "' + target + '". Please configure the dynamicRequireTargets option of @rollup/plugin-commonjs appropriately for this require call to behave properly.'); +export function commonjsRequire (path) { + ${FAILED_REQUIRE_ERROR} } `; -const HELPERS_DYNAMIC = ` +const getDynamicHelpers = (ignoreDynamicRequires) => ` export function createCommonjsModule(fn, basedir, module) { return module = { path: basedir, @@ -231,13 +233,15 @@ export function commonjsRequire (path, originalModuleDir) { return cachedModule.exports; }; } - return require(path); + ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR} } commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE; commonjsRequire.resolve = commonjsResolve; `; -export function getHelpersModule(isDynamicRequireModulesEnabled) { - return `${HELPERS}${isDynamicRequireModulesEnabled ? HELPERS_DYNAMIC : HELPER_NON_DYNAMIC}`; +export function getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires) { + return `${HELPERS}${ + isDynamicRequireModulesEnabled ? getDynamicHelpers(ignoreDynamicRequires) : HELPER_NON_DYNAMIC + }`; } diff --git a/packages/commonjs/src/index.js b/packages/commonjs/src/index.js index ed307c90f..79015294b 100644 --- a/packages/commonjs/src/index.js +++ b/packages/commonjs/src/index.js @@ -44,6 +44,7 @@ export default function commonjs(options = {}) { const filter = createFilter(options.include, options.exclude); const { ignoreGlobal, + ignoreDynamicRequires, requireReturnsDefault: requireReturnsDefaultOption, esmExternals } = options; @@ -97,6 +98,7 @@ export default function commonjs(options = {}) { function transformAndCheckExports(code, id) { if (isDynamicRequireModulesEnabled && this.getModuleInfo(id).isEntry) { + // eslint-disable-next-line no-param-reassign code = getDynamicPackagesEntryIntro(dynamicRequireModuleDirPaths, dynamicRequireModuleSet) + code; } @@ -125,6 +127,7 @@ export default function commonjs(options = {}) { // avoid wrapping in createCommonjsModule, as this is a commonjsRegister call if (isModuleRegisterProxy(id)) { disableWrap = true; + // eslint-disable-next-line no-param-reassign id = unwrapModuleRegisterProxy(id); } @@ -135,6 +138,7 @@ export default function commonjs(options = {}) { isEsModule, ignoreGlobal || isEsModule, ignoreRequire, + ignoreDynamicRequires && !isDynamicRequireModulesEnabled, getIgnoreTryCatchRequireStatementMode, sourceMap, isDynamicRequireModulesEnabled, @@ -161,7 +165,7 @@ export default function commonjs(options = {}) { load(id) { if (id === HELPERS_ID) { - return getHelpersModule(isDynamicRequireModulesEnabled); + return getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires); } if (id.startsWith(HELPERS_ID)) { @@ -232,6 +236,7 @@ export default function commonjs(options = {}) { } }, + // eslint-disable-next-line no-shadow moduleParsed({ id, meta: { commonjs } }) { if (commonjs) { const isCjs = commonjs.isCommonJS; diff --git a/packages/commonjs/src/transform-commonjs.js b/packages/commonjs/src/transform-commonjs.js index 0f6b54060..ccb32a565 100644 --- a/packages/commonjs/src/transform-commonjs.js +++ b/packages/commonjs/src/transform-commonjs.js @@ -46,6 +46,7 @@ export default function transformCommonjs( isEsModule, ignoreGlobal, ignoreRequire, + ignoreDynamicRequires, getIgnoreTryCatchRequireStatementMode, sourceMap, isDynamicRequireModulesEnabled, @@ -320,12 +321,14 @@ export default function transformCommonjs( )}` ); } - if (isShorthandProperty(parent)) { - magicString.appendRight(node.end, `: ${HELPERS_NAME}.commonjsRequire`); - } else { - magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, { - storeName: true - }); + if (!ignoreDynamicRequires) { + if (isShorthandProperty(parent)) { + magicString.appendRight(node.end, `: ${HELPERS_NAME}.commonjsRequire`); + } else { + magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, { + storeName: true + }); + } } uses.commonjsHelpers = true; diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-absolute-paths/_config.js b/packages/commonjs/test/fixtures/function/dynamic-require-absolute-paths/_config.js deleted file mode 100755 index c0a5a9fc2..000000000 --- a/packages/commonjs/test/fixtures/function/dynamic-require-absolute-paths/_config.js +++ /dev/null @@ -1,9 +0,0 @@ -module.exports = { - description: 'resolves both windows and posix absolute paths', - pluginOptions: { - dynamicRequireTargets: ['fixtures/function/dynamic-require-absolute-paths/submodule.js'] - }, - options: { - external: ['path'] - } -}; diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-absolute-paths/main.js b/packages/commonjs/test/fixtures/function/dynamic-require-absolute-paths/main.js deleted file mode 100755 index b5653b870..000000000 --- a/packages/commonjs/test/fixtures/function/dynamic-require-absolute-paths/main.js +++ /dev/null @@ -1,7 +0,0 @@ -/* eslint-disable import/no-dynamic-require, global-require */ - -const Path = require('path'); - -const basePath = `${process.cwd()}/fixtures/function/dynamic-require-absolute-paths`; - -t.is(require(Path.resolve(`${basePath}/submodule.js`)), 'submodule'); diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-absolute-paths/submodule.js b/packages/commonjs/test/fixtures/function/dynamic-require-absolute-paths/submodule.js deleted file mode 100755 index c285d34bc..000000000 --- a/packages/commonjs/test/fixtures/function/dynamic-require-absolute-paths/submodule.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = 'submodule'; diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-fallback/_config.js b/packages/commonjs/test/fixtures/function/dynamic-require-fallback/_config.js new file mode 100755 index 000000000..2e93c732b --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-fallback/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'keeps a require call as fallback when configured', + pluginOptions: { + ignoreDynamicRequires: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-fallback/dep.js b/packages/commonjs/test/fixtures/function/dynamic-require-fallback/dep.js new file mode 100755 index 000000000..d0afb22c4 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-fallback/dep.js @@ -0,0 +1 @@ +module.exports = 'dep'; diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-fallback/main.js b/packages/commonjs/test/fixtures/function/dynamic-require-fallback/main.js new file mode 100755 index 000000000..add49eec6 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-fallback/main.js @@ -0,0 +1,8 @@ +/* eslint-disable import/no-dynamic-require, global-require */ + +function takeModule(withName) { + return require(withName); +} + +// The bundled code will run from test/helpers/util.js +t.is(takeModule('../fixtures/function/dynamic-require-fallback/dep.js'), 'dep'); diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-globs/main.js b/packages/commonjs/test/fixtures/function/dynamic-require-globs/main.js index 64e2c1e87..e1d9514d9 100755 --- a/packages/commonjs/test/fixtures/function/dynamic-require-globs/main.js +++ b/packages/commonjs/test/fixtures/function/dynamic-require-globs/main.js @@ -7,12 +7,7 @@ function takeModule(withName) { t.is(takeModule('submodule1.js'), 'submodule1'); t.is(takeModule('submodule2.js'), 'submodule2'); t.is(takeModule('extramodule1.js'), 'extramodule1'); - -let hasThrown = false; -try { - takeModule('extramodule2.js'); -} catch (error) { - t.truthy(/Cannot find module '\.\/extramodule2\.js'/.test(error.message)); - hasThrown = true; -} -t.truthy(hasThrown); +t.throws(() => takeModule('extramodule2.js'), { + message: + 'Could not dynamically require "./extramodule2.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.' +}); diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-no-fallback/_config.js b/packages/commonjs/test/fixtures/function/dynamic-require-no-fallback/_config.js new file mode 100755 index 000000000..47ec76dd6 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-no-fallback/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'throws when there is no fallback for a dynamic require call' +}; diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-no-fallback/main.js b/packages/commonjs/test/fixtures/function/dynamic-require-no-fallback/main.js new file mode 100755 index 000000000..31ec4917c --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-no-fallback/main.js @@ -0,0 +1,10 @@ +/* eslint-disable import/no-dynamic-require, global-require */ + +function takeModule(withName) { + return require(withName); +} + +t.throws(() => takeModule('./dep.js'), { + message: + 'Could not dynamically require "./dep.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.' +}); diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-targets-fallback/_config.js b/packages/commonjs/test/fixtures/function/dynamic-require-targets-fallback/_config.js new file mode 100755 index 000000000..6123cc654 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-targets-fallback/_config.js @@ -0,0 +1,7 @@ +module.exports = { + description: 'keeps a require call as fallback when configured for dynamicRequireTargets', + pluginOptions: { + ignoreDynamicRequires: true, + dynamicRequireTargets: ['fixtures/function/dynamic-require-targets-fallback/dep1.js'] + } +}; diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-targets-fallback/dep1.js b/packages/commonjs/test/fixtures/function/dynamic-require-targets-fallback/dep1.js new file mode 100755 index 000000000..d0afb22c4 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-targets-fallback/dep1.js @@ -0,0 +1 @@ +module.exports = 'dep'; diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-targets-fallback/dep2.js b/packages/commonjs/test/fixtures/function/dynamic-require-targets-fallback/dep2.js new file mode 100755 index 000000000..d0afb22c4 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-targets-fallback/dep2.js @@ -0,0 +1 @@ +module.exports = 'dep'; diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-targets-fallback/main.js b/packages/commonjs/test/fixtures/function/dynamic-require-targets-fallback/main.js new file mode 100755 index 000000000..3652fbc73 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-targets-fallback/main.js @@ -0,0 +1,9 @@ +/* eslint-disable import/no-dynamic-require, global-require */ + +function takeModule(withName) { + return require(withName); +} + +t.is(takeModule('./dep1.js'), 'dep'); +// The bundled code will run from test/helpers/util.js +t.is(takeModule('../fixtures/function/dynamic-require-targets-fallback/dep2.js'), 'dep'); diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-targets-no-fallback/_config.js b/packages/commonjs/test/fixtures/function/dynamic-require-targets-no-fallback/_config.js new file mode 100755 index 000000000..ff229d8f0 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-targets-no-fallback/_config.js @@ -0,0 +1,7 @@ +module.exports = { + description: + 'throws when there is no require call configured as fallback for dynamicRequireTargets', + pluginOptions: { + dynamicRequireTargets: ['fixtures/function/dynamic-require-targets-no-fallback/dep1.js'] + } +}; diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-targets-no-fallback/dep1.js b/packages/commonjs/test/fixtures/function/dynamic-require-targets-no-fallback/dep1.js new file mode 100755 index 000000000..d0afb22c4 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-targets-no-fallback/dep1.js @@ -0,0 +1 @@ +module.exports = 'dep'; diff --git a/packages/commonjs/test/fixtures/function/dynamic-require-targets-no-fallback/main.js b/packages/commonjs/test/fixtures/function/dynamic-require-targets-no-fallback/main.js new file mode 100755 index 000000000..097c93f68 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/dynamic-require-targets-no-fallback/main.js @@ -0,0 +1,11 @@ +/* eslint-disable import/no-dynamic-require, global-require */ + +function takeModule(withName) { + return require(withName); +} + +t.is(takeModule('./dep1.js'), 'dep'); +t.throws(() => takeModule('./dep2.js'), { + message: + 'Could not dynamically require "./dep2.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.' +}); diff --git a/packages/commonjs/test/fixtures/function/global-not-overwritten/encode.js b/packages/commonjs/test/fixtures/function/global-not-overwritten/encode.js index 47df37ebe..11ef1a2aa 100644 --- a/packages/commonjs/test/fixtures/function/global-not-overwritten/encode.js +++ b/packages/commonjs/test/fixtures/function/global-not-overwritten/encode.js @@ -1,4 +1,4 @@ -exports.encodeURIComponent = function() { +exports.encodeURIComponent = function () { return encodeURIComponent(this.str); }; diff --git a/packages/commonjs/test/fixtures/function/inline/main.js b/packages/commonjs/test/fixtures/function/inline/main.js index 9c89da3ac..06c18b41a 100644 --- a/packages/commonjs/test/fixtures/function/inline/main.js +++ b/packages/commonjs/test/fixtures/function/inline/main.js @@ -1,4 +1,4 @@ /* eslint-disable global-require */ -module.exports = function() { +module.exports = function () { return require('./multiply')(2, require('./foo')); }; diff --git a/packages/commonjs/test/fixtures/function/inline/multiply.js b/packages/commonjs/test/fixtures/function/inline/multiply.js index 1d31f4c02..b5a6c5bea 100644 --- a/packages/commonjs/test/fixtures/function/inline/multiply.js +++ b/packages/commonjs/test/fixtures/function/inline/multiply.js @@ -1,3 +1,3 @@ -module.exports = function(a, b) { +module.exports = function (a, b) { return a * b; }; diff --git a/packages/commonjs/test/fixtures/function/toplevel-return-complex/bar.js b/packages/commonjs/test/fixtures/function/toplevel-return-complex/bar.js index 6c780a1b8..9d12d7de1 100644 --- a/packages/commonjs/test/fixtures/function/toplevel-return-complex/bar.js +++ b/packages/commonjs/test/fixtures/function/toplevel-return-complex/bar.js @@ -1,3 +1,3 @@ -module.exports = function() { +module.exports = function () { return true; }; diff --git a/packages/commonjs/test/fixtures/function/unsupported-dynamic-require/_config.js b/packages/commonjs/test/fixtures/function/unsupported-dynamic-require/_config.js deleted file mode 100755 index b77e73bbf..000000000 --- a/packages/commonjs/test/fixtures/function/unsupported-dynamic-require/_config.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - description: 'throws for unsupported dynamic require calls' -}; diff --git a/packages/commonjs/test/fixtures/function/unsupported-dynamic-require/main.js b/packages/commonjs/test/fixtures/function/unsupported-dynamic-require/main.js deleted file mode 100755 index 802f83fc6..000000000 --- a/packages/commonjs/test/fixtures/function/unsupported-dynamic-require/main.js +++ /dev/null @@ -1,10 +0,0 @@ -/* eslint-disable import/no-dynamic-require, global-require */ - -t.throws(() => require(getRequireTarget()), { - message: - 'Could not dynamically require "foo-bar". Please configure the dynamicRequireTargets option of @rollup/plugin-commonjs appropriately for this require call to behave properly.' -}); - -function getRequireTarget() { - return 'foo-bar'; -} diff --git a/packages/commonjs/test/fixtures/samples/dynamic-require-different-loader/submodule2.js b/packages/commonjs/test/fixtures/samples/dynamic-require-different-loader/submodule2.js index f470282a9..28a021e39 100755 --- a/packages/commonjs/test/fixtures/samples/dynamic-require-different-loader/submodule2.js +++ b/packages/commonjs/test/fixtures/samples/dynamic-require-different-loader/submodule2.js @@ -1,3 +1,3 @@ -module.exports = function() { +module.exports = function () { return 'Hello there'; }; diff --git a/packages/commonjs/test/fixtures/samples/umd/correct-scoping.js b/packages/commonjs/test/fixtures/samples/umd/correct-scoping.js index a4816cf64..c1008ba9d 100644 --- a/packages/commonjs/test/fixtures/samples/umd/correct-scoping.js +++ b/packages/commonjs/test/fixtures/samples/umd/correct-scoping.js @@ -1,5 +1,5 @@ if (typeof require === 'function') { - module.exports = (function(require) { + module.exports = (function (require) { return typeof require; })({}); } diff --git a/packages/commonjs/test/form.js b/packages/commonjs/test/form.js index 656e92fa2..ae245863a 100644 --- a/packages/commonjs/test/form.js +++ b/packages/commonjs/test/form.js @@ -74,8 +74,15 @@ readdirSync('./fixtures/form').forEach((dir) => { // this will benefit issues like `form/try-catch-remove` where whitespace is left in the line, // and testing on windows (\r\n) t.is( - actual.split('\n').map(x => x.trimEnd()).join('\n'), - expected.split('\n').map(x => x.trimEnd()).join('\n')); + actual + .split('\n') + .map((x) => x.trimEnd()) + .join('\n'), + expected + .split('\n') + .map((x) => x.trimEnd()) + .join('\n') + ); } }); }); diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index 1717deded..dd86c2686 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -423,7 +423,7 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ @@ -626,7 +626,7 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ @@ -831,7 +831,7 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ @@ -884,206 +884,6 @@ Generated by [AVA](https://avajs.dev). `, } -## dynamic-require-absolute-paths - -> Snapshot 1 - - { - 'main.js': `'use strict';␊ - ␊ - var Path = require('path');␊ - ␊ - function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }␊ - ␊ - var Path__default = /*#__PURE__*/_interopDefaultLegacy(Path);␊ - ␊ - var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊ - ␊ - function commonjsRegister (path, loader) {␊ - DYNAMIC_REQUIRE_LOADERS[path] = loader;␊ - }␊ - ␊ - const DYNAMIC_REQUIRE_LOADERS = Object.create(null);␊ - const DYNAMIC_REQUIRE_CACHE = Object.create(null);␊ - const DEFAULT_PARENT_MODULE = {␊ - id: '<' + 'rollup>', exports: {}, parent: undefined, filename: null, loaded: false, children: [], paths: []␊ - };␊ - const CHECKED_EXTENSIONS = ['', '.js', '.json'];␊ - ␊ - function normalize (path) {␊ - path = path.replace(/\\\\/g, '/');␊ - const parts = path.split('/');␊ - const slashed = parts[0] === '';␊ - for (let i = 1; i < parts.length; i++) {␊ - if (parts[i] === '.' || parts[i] === '') {␊ - parts.splice(i--, 1);␊ - }␊ - }␊ - for (let 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;␊ - }␊ - ␊ - function join () {␊ - if (arguments.length === 0)␊ - return '.';␊ - let joined;␊ - for (let i = 0; i < arguments.length; ++i) {␊ - let arg = arguments[i];␊ - if (arg.length > 0) {␊ - if (joined === undefined)␊ - joined = arg;␊ - else␊ - joined += '/' + arg;␊ - }␊ - }␊ - if (joined === undefined)␊ - return '.';␊ - ␊ - return joined;␊ - }␊ - ␊ - function isPossibleNodeModulesPath (modulePath) {␊ - let c0 = modulePath[0];␊ - if (c0 === '/' || c0 === '\\\\') return false;␊ - let 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 dirname (path) {␊ - if (path.length === 0)␊ - return '.';␊ - ␊ - let i = path.length - 1;␊ - while (i > 0) {␊ - const c = path.charCodeAt(i);␊ - if ((c === 47 || c === 92) && i !== path.length - 1)␊ - break;␊ - i--;␊ - }␊ - ␊ - if (i > 0)␊ - return path.substr(0, i);␊ - ␊ - if (path.chartCodeAt(0) === 47 || path.chartCodeAt(0) === 92)␊ - return path.charAt(0);␊ - ␊ - return '.';␊ - }␊ - ␊ - function commonjsResolveImpl (path, originalModuleDir, testCache) {␊ - const shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ - path = normalize(path);␊ - let relPath;␊ - if (path[0] === '/') {␊ - originalModuleDir = '/';␊ - }␊ - while (true) {␊ - if (!shouldTryNodeModules) {␊ - relPath = originalModuleDir ? normalize(originalModuleDir + '/' + path) : path;␊ - } else if (originalModuleDir) {␊ - relPath = normalize(originalModuleDir + '/node_modules/' + path);␊ - } else {␊ - relPath = normalize(join('node_modules', path));␊ - }␊ - ␊ - if (relPath.endsWith('/..')) {␊ - break; // Travelled too far up, avoid infinite loop␊ - }␊ - ␊ - for (let extensionIndex = 0; extensionIndex < CHECKED_EXTENSIONS.length; extensionIndex++) {␊ - const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];␊ - if (DYNAMIC_REQUIRE_CACHE[resolvedPath]) {␊ - return resolvedPath;␊ - } if (DYNAMIC_REQUIRE_LOADERS[resolvedPath]) {␊ - return resolvedPath;␊ - } }␊ - if (!shouldTryNodeModules) break;␊ - const nextDir = normalize(originalModuleDir + '/..');␊ - if (nextDir === originalModuleDir) break;␊ - originalModuleDir = nextDir;␊ - }␊ - return null;␊ - }␊ - ␊ - function commonjsResolve (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - return resolvedPath;␊ - }␊ - return require.resolve(path);␊ - }␊ - ␊ - function commonjsRequire (path, originalModuleDir) {␊ - const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ - if (resolvedPath !== null) {␊ - let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];␊ - if (cachedModule) return cachedModule.exports;␊ - const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];␊ - if (loader) {␊ - DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {␊ - id: resolvedPath,␊ - filename: resolvedPath,␊ - path: dirname(resolvedPath),␊ - exports: {},␊ - parent: DEFAULT_PARENT_MODULE,␊ - loaded: false,␊ - children: [],␊ - paths: [],␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? cachedModule.path : base);␊ - }␊ - };␊ - try {␊ - loader.call(commonjsGlobal, cachedModule, cachedModule.exports);␊ - } catch (error) {␊ - delete DYNAMIC_REQUIRE_CACHE[resolvedPath];␊ - throw error;␊ - }␊ - cachedModule.loaded = true;␊ - return cachedModule.exports;␊ - } }␊ - return require(path);␊ - }␊ - ␊ - commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ - commonjsRequire.resolve = commonjsResolve;␊ - ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-absolute-paths/submodule.js", function (module, exports) {␊ - module.exports = 'submodule';␊ - ␊ - });␊ - ␊ - /* eslint-disable import/no-dynamic-require, global-require */␊ - ␊ - ␊ - ␊ - const basePath = `${process.cwd()}/fixtures/function/dynamic-require-absolute-paths`;␊ - ␊ - t.is(commonjsRequire(Path__default['default'].resolve(`${basePath}/submodule.js`),"/$$rollup_base$$/fixtures/function/dynamic-require-absolute-paths"), 'submodule');␊ - ␊ - var main = {␊ - ␊ - };␊ - ␊ - module.exports = main;␊ - `, - } - ## dynamic-require-cache-reference > Snapshot 1 @@ -1251,7 +1051,7 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ @@ -1327,7 +1127,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 { - 'lib2-4ae05749.js': `'use strict';␊ + 'lib2-8f3d0aad.js': `'use strict';␊ ␊ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊ ␊ @@ -1489,7 +1289,7 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ @@ -1522,7 +1322,7 @@ Generated by [AVA](https://avajs.dev). `, 'main.js': `'use strict';␊ ␊ - var lib2 = require('./lib2-4ae05749.js');␊ + var lib2 = require('./lib2-8f3d0aad.js');␊ ␊ /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ @@ -1539,7 +1339,7 @@ Generated by [AVA](https://avajs.dev). `, 'main2.js': `'use strict';␊ ␊ - require('./lib2-4ae05749.js');␊ + require('./lib2-8f3d0aad.js');␊ ␊ `, } @@ -1711,7 +1511,7 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ @@ -1901,7 +1701,7 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ @@ -1936,6 +1736,30 @@ Generated by [AVA](https://avajs.dev). `, } +## dynamic-require-fallback + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + /* eslint-disable import/no-dynamic-require, global-require */␊ + ␊ + function takeModule(withName) {␊ + return require(withName);␊ + }␊ + ␊ + // The bundled code will run from test/helpers/util.js␊ + t.is(takeModule('../fixtures/function/dynamic-require-fallback/dep.js'), 'dep');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + ## dynamic-require-from-es-import > Snapshot 1 @@ -2103,7 +1927,7 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ @@ -2287,7 +2111,7 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ @@ -2317,15 +2141,10 @@ Generated by [AVA](https://avajs.dev). t.is(takeModule('submodule1.js'), 'submodule1');␊ t.is(takeModule('submodule2.js'), 'submodule2');␊ t.is(takeModule('extramodule1.js'), 'extramodule1');␊ - ␊ - let hasThrown = false;␊ - try {␊ - takeModule('extramodule2.js');␊ - } catch (error) {␊ - t.truthy(/Cannot find module '\\.\\/extramodule2\\.js'/.test(error.message));␊ - hasThrown = true;␊ - }␊ - t.truthy(hasThrown);␊ + t.throws(() => takeModule('extramodule2.js'), {␊ + message:␊ + 'Could not dynamically require "./extramodule2.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'␊ + });␊ ␊ var main = {␊ ␊ @@ -2502,7 +2321,7 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ @@ -2714,7 +2533,7 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ @@ -2731,22 +2550,485 @@ Generated by [AVA](https://avajs.dev). ␊ /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ - function takeModule(withName) {␊ - return commonjsRequire(`./${withName}`,"/$$rollup_base$$/fixtures/function/dynamic-require-json");␊ - }␊ + function takeModule(withName) {␊ + return commonjsRequire(`./${withName}`,"/$$rollup_base$$/fixtures/function/dynamic-require-json");␊ + }␊ + ␊ + t.deepEqual(takeModule('dynamic.json'), { value: 'present' });␊ + t.deepEqual(takeModule('dynamic'), { value: 'present' });␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## dynamic-require-no-fallback + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + function commonjsRequire (path) {␊ + 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.');␊ + }␊ + ␊ + /* eslint-disable import/no-dynamic-require, global-require */␊ + ␊ + function takeModule(withName) {␊ + return commonjsRequire(withName);␊ + }␊ + ␊ + t.throws(() => takeModule('./dep.js'), {␊ + message:␊ + 'Could not dynamically require "./dep.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'␊ + });␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## dynamic-require-package + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊ + ␊ + function commonjsRegister (path, loader) {␊ + DYNAMIC_REQUIRE_LOADERS[path] = loader;␊ + }␊ + ␊ + const DYNAMIC_REQUIRE_LOADERS = Object.create(null);␊ + const DYNAMIC_REQUIRE_CACHE = Object.create(null);␊ + const DEFAULT_PARENT_MODULE = {␊ + id: '<' + 'rollup>', exports: {}, parent: undefined, filename: null, loaded: false, children: [], paths: []␊ + };␊ + const CHECKED_EXTENSIONS = ['', '.js', '.json'];␊ + ␊ + function normalize (path) {␊ + path = path.replace(/\\\\/g, '/');␊ + const parts = path.split('/');␊ + const slashed = parts[0] === '';␊ + for (let i = 1; i < parts.length; i++) {␊ + if (parts[i] === '.' || parts[i] === '') {␊ + parts.splice(i--, 1);␊ + }␊ + }␊ + for (let 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;␊ + }␊ + ␊ + function join () {␊ + if (arguments.length === 0)␊ + return '.';␊ + let joined;␊ + for (let i = 0; i < arguments.length; ++i) {␊ + let arg = arguments[i];␊ + if (arg.length > 0) {␊ + if (joined === undefined)␊ + joined = arg;␊ + else␊ + joined += '/' + arg;␊ + }␊ + }␊ + if (joined === undefined)␊ + return '.';␊ + ␊ + return joined;␊ + }␊ + ␊ + function isPossibleNodeModulesPath (modulePath) {␊ + let c0 = modulePath[0];␊ + if (c0 === '/' || c0 === '\\\\') return false;␊ + let 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 dirname (path) {␊ + if (path.length === 0)␊ + return '.';␊ + ␊ + let i = path.length - 1;␊ + while (i > 0) {␊ + const c = path.charCodeAt(i);␊ + if ((c === 47 || c === 92) && i !== path.length - 1)␊ + break;␊ + i--;␊ + }␊ + ␊ + if (i > 0)␊ + return path.substr(0, i);␊ + ␊ + if (path.chartCodeAt(0) === 47 || path.chartCodeAt(0) === 92)␊ + return path.charAt(0);␊ + ␊ + return '.';␊ + }␊ + ␊ + function commonjsResolveImpl (path, originalModuleDir, testCache) {␊ + const shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ + path = normalize(path);␊ + let relPath;␊ + if (path[0] === '/') {␊ + originalModuleDir = '/';␊ + }␊ + while (true) {␊ + if (!shouldTryNodeModules) {␊ + relPath = originalModuleDir ? normalize(originalModuleDir + '/' + path) : path;␊ + } else if (originalModuleDir) {␊ + relPath = normalize(originalModuleDir + '/node_modules/' + path);␊ + } else {␊ + relPath = normalize(join('node_modules', path));␊ + }␊ + ␊ + if (relPath.endsWith('/..')) {␊ + break; // Travelled too far up, avoid infinite loop␊ + }␊ + ␊ + for (let extensionIndex = 0; extensionIndex < CHECKED_EXTENSIONS.length; extensionIndex++) {␊ + const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];␊ + if (DYNAMIC_REQUIRE_CACHE[resolvedPath]) {␊ + return resolvedPath;␊ + } if (DYNAMIC_REQUIRE_LOADERS[resolvedPath]) {␊ + return resolvedPath;␊ + } }␊ + if (!shouldTryNodeModules) break;␊ + const nextDir = normalize(originalModuleDir + '/..');␊ + if (nextDir === originalModuleDir) break;␊ + originalModuleDir = nextDir;␊ + }␊ + return null;␊ + }␊ + ␊ + function commonjsResolve (path, originalModuleDir) {␊ + const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + }␊ + ␊ + function commonjsRequire (path, originalModuleDir) {␊ + const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];␊ + if (cachedModule) return cachedModule.exports;␊ + const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];␊ + if (loader) {␊ + DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {␊ + id: resolvedPath,␊ + filename: resolvedPath,␊ + path: dirname(resolvedPath),␊ + exports: {},␊ + parent: DEFAULT_PARENT_MODULE,␊ + loaded: false,␊ + children: [],␊ + paths: [],␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? cachedModule.path : base);␊ + }␊ + };␊ + try {␊ + loader.call(commonjsGlobal, cachedModule, cachedModule.exports);␊ + } catch (error) {␊ + delete DYNAMIC_REQUIRE_CACHE[resolvedPath];␊ + throw error;␊ + }␊ + cachedModule.loaded = true;␊ + return cachedModule.exports;␊ + } }␊ + 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.');␊ + }␊ + ␊ + commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ + commonjsRequire.resolve = commonjsResolve;␊ + ␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package/entry.js", function (module, exports) {␊ + module.exports = 'same-directory';␊ + ␊ + });␊ + ␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package/sub/entry.js", function (module, exports) {␊ + module.exports = 'sub';␊ + ␊ + });␊ + ␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package/node_modules/custom-module/entry.js", function (module, exports) {␊ + module.exports = 'custom-module';␊ + ␊ + });␊ + ␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package", function (module, exports) {␊ + module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-package/entry.js", null);␊ + });␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package/sub", function (module, exports) {␊ + module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-package/sub/entry.js", null);␊ + });␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package/node_modules/custom-module", function (module, exports) {␊ + module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-package/node_modules/custom-module/entry.js", null);␊ + });␊ + ␊ + /* eslint-disable import/no-dynamic-require, global-require */␊ + ␊ + function takeModule(name) {␊ + return commonjsRequire(name,"/$$rollup_base$$/fixtures/function/dynamic-require-package/sub");␊ + }␊ + ␊ + var sub = {␊ + parent: takeModule('..'),␊ + customModule: takeModule('custom-module')␊ + };␊ + ␊ + /* eslint-disable import/no-dynamic-require, global-require */␊ + ␊ + function takeModule$1(name) {␊ + return commonjsRequire(name,"/$$rollup_base$$/fixtures/function/dynamic-require-package");␊ + }␊ + ␊ + t.is(takeModule$1('.'), 'same-directory');␊ + t.is(takeModule$1('./'), 'same-directory');␊ + t.is(takeModule$1('.//'), 'same-directory');␊ + ␊ + t.is(takeModule$1('./sub'), 'sub');␊ + ␊ + t.is(takeModule$1('custom-module'), 'custom-module');␊ + t.deepEqual(sub, { parent: 'same-directory', customModule: 'custom-module' });␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## dynamic-require-package-sub + +> Snapshot 1 + + { + 'entry.js': `'use strict';␊ + ␊ + var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊ + ␊ + function commonjsRegister (path, loader) {␊ + DYNAMIC_REQUIRE_LOADERS[path] = loader;␊ + }␊ + ␊ + const DYNAMIC_REQUIRE_LOADERS = Object.create(null);␊ + const DYNAMIC_REQUIRE_CACHE = Object.create(null);␊ + const DEFAULT_PARENT_MODULE = {␊ + id: '<' + 'rollup>', exports: {}, parent: undefined, filename: null, loaded: false, children: [], paths: []␊ + };␊ + const CHECKED_EXTENSIONS = ['', '.js', '.json'];␊ + ␊ + function normalize (path) {␊ + path = path.replace(/\\\\/g, '/');␊ + const parts = path.split('/');␊ + const slashed = parts[0] === '';␊ + for (let i = 1; i < parts.length; i++) {␊ + if (parts[i] === '.' || parts[i] === '') {␊ + parts.splice(i--, 1);␊ + }␊ + }␊ + for (let 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;␊ + }␊ + ␊ + function join () {␊ + if (arguments.length === 0)␊ + return '.';␊ + let joined;␊ + for (let i = 0; i < arguments.length; ++i) {␊ + let arg = arguments[i];␊ + if (arg.length > 0) {␊ + if (joined === undefined)␊ + joined = arg;␊ + else␊ + joined += '/' + arg;␊ + }␊ + }␊ + if (joined === undefined)␊ + return '.';␊ + ␊ + return joined;␊ + }␊ + ␊ + function isPossibleNodeModulesPath (modulePath) {␊ + let c0 = modulePath[0];␊ + if (c0 === '/' || c0 === '\\\\') return false;␊ + let 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 dirname (path) {␊ + if (path.length === 0)␊ + return '.';␊ + ␊ + let i = path.length - 1;␊ + while (i > 0) {␊ + const c = path.charCodeAt(i);␊ + if ((c === 47 || c === 92) && i !== path.length - 1)␊ + break;␊ + i--;␊ + }␊ + ␊ + if (i > 0)␊ + return path.substr(0, i);␊ + ␊ + if (path.chartCodeAt(0) === 47 || path.chartCodeAt(0) === 92)␊ + return path.charAt(0);␊ + ␊ + return '.';␊ + }␊ + ␊ + function commonjsResolveImpl (path, originalModuleDir, testCache) {␊ + const shouldTryNodeModules = isPossibleNodeModulesPath(path);␊ + path = normalize(path);␊ + let relPath;␊ + if (path[0] === '/') {␊ + originalModuleDir = '/';␊ + }␊ + while (true) {␊ + if (!shouldTryNodeModules) {␊ + relPath = originalModuleDir ? normalize(originalModuleDir + '/' + path) : path;␊ + } else if (originalModuleDir) {␊ + relPath = normalize(originalModuleDir + '/node_modules/' + path);␊ + } else {␊ + relPath = normalize(join('node_modules', path));␊ + }␊ + ␊ + if (relPath.endsWith('/..')) {␊ + break; // Travelled too far up, avoid infinite loop␊ + }␊ + ␊ + for (let extensionIndex = 0; extensionIndex < CHECKED_EXTENSIONS.length; extensionIndex++) {␊ + const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];␊ + if (DYNAMIC_REQUIRE_CACHE[resolvedPath]) {␊ + return resolvedPath;␊ + } if (DYNAMIC_REQUIRE_LOADERS[resolvedPath]) {␊ + return resolvedPath;␊ + } }␊ + if (!shouldTryNodeModules) break;␊ + const nextDir = normalize(originalModuleDir + '/..');␊ + if (nextDir === originalModuleDir) break;␊ + originalModuleDir = nextDir;␊ + }␊ + return null;␊ + }␊ + ␊ + function commonjsResolve (path, originalModuleDir) {␊ + const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + return resolvedPath;␊ + }␊ + return require.resolve(path);␊ + }␊ + ␊ + function commonjsRequire (path, originalModuleDir) {␊ + const resolvedPath = commonjsResolveImpl(path, originalModuleDir);␊ + if (resolvedPath !== null) {␊ + let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];␊ + if (cachedModule) return cachedModule.exports;␊ + const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];␊ + if (loader) {␊ + DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {␊ + id: resolvedPath,␊ + filename: resolvedPath,␊ + path: dirname(resolvedPath),␊ + exports: {},␊ + parent: DEFAULT_PARENT_MODULE,␊ + loaded: false,␊ + children: [],␊ + paths: [],␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? cachedModule.path : base);␊ + }␊ + };␊ + try {␊ + loader.call(commonjsGlobal, cachedModule, cachedModule.exports);␊ + } catch (error) {␊ + delete DYNAMIC_REQUIRE_CACHE[resolvedPath];␊ + throw error;␊ + }␊ + cachedModule.loaded = true;␊ + return cachedModule.exports;␊ + } }␊ + 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.');␊ + }␊ + ␊ + commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ + commonjsRequire.resolve = commonjsResolve;␊ + ␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package-sub/node_modules/custom-module/entry.js", function (module, exports) {␊ + module.exports = 'custom-module';␊ + ␊ + });␊ + ␊ + var main = "./entry.js";␊ + var require$$0 = {␊ + main: main␊ + };␊ + ␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package-sub/node_modules/custom-module/package.json", function (module, exports) {␊ + module.exports = require$$0;␊ + });␊ + ␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package-sub/node_modules/custom-module", function (module, exports) {␊ + module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-package-sub/node_modules/custom-module/entry.js", null);␊ + });␊ ␊ - t.deepEqual(takeModule('dynamic.json'), { value: 'present' });␊ - t.deepEqual(takeModule('dynamic'), { value: 'present' });␊ + t.is(commonjsRequire("custom-module", "/$$rollup_base$$/fixtures/function/dynamic-require-package-sub/sub"), 'custom-module');␊ ␊ - var main = {␊ + var entry = {␊ ␊ };␊ ␊ - module.exports = main;␊ + module.exports = entry;␊ `, } -## dynamic-require-package +## dynamic-require-relative-paths > Snapshot 1 @@ -2913,62 +3195,32 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ commonjsRequire.resolve = commonjsResolve;␊ ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package/entry.js", function (module, exports) {␊ - module.exports = 'same-directory';␊ - ␊ - });␊ - ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package/sub/entry.js", function (module, exports) {␊ - module.exports = 'sub';␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-relative-paths/sub/submodule.js", function (module, exports) {␊ + module.exports = 'submodule';␊ ␊ });␊ ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package/node_modules/custom-module/entry.js", function (module, exports) {␊ - module.exports = 'custom-module';␊ - ␊ - });␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-relative-paths/sub/subsub/subsubmodule.js", function (module, exports) {␊ + module.exports = 'subsubmodule';␊ ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package", function (module, exports) {␊ - module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-package/entry.js", null);␊ - });␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package/sub", function (module, exports) {␊ - module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-package/sub/entry.js", null);␊ - });␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package/node_modules/custom-module", function (module, exports) {␊ - module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-package/node_modules/custom-module/entry.js", null);␊ });␊ ␊ /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ - function takeModule(name) {␊ - return commonjsRequire(name,"/$$rollup_base$$/fixtures/function/dynamic-require-package/sub");␊ - }␊ - ␊ - var sub = {␊ - parent: takeModule('..'),␊ - customModule: takeModule('custom-module')␊ - };␊ - ␊ - /* eslint-disable import/no-dynamic-require, global-require */␊ - ␊ - function takeModule$1(name) {␊ - return commonjsRequire(name,"/$$rollup_base$$/fixtures/function/dynamic-require-package");␊ + function takeModuleWithDelimiter(name, delimiter) {␊ + return commonjsRequire(`.${delimiter}${name.replace(/=/g, delimiter)}`,"/$$rollup_base$$/fixtures/function/dynamic-require-relative-paths");␊ }␊ ␊ - t.is(takeModule$1('.'), 'same-directory');␊ - t.is(takeModule$1('./'), 'same-directory');␊ - t.is(takeModule$1('.//'), 'same-directory');␊ - ␊ - t.is(takeModule$1('./sub'), 'sub');␊ - ␊ - t.is(takeModule$1('custom-module'), 'custom-module');␊ - t.deepEqual(sub, { parent: 'same-directory', customModule: 'custom-module' });␊ + t.is(takeModuleWithDelimiter('sub=submodule.js', '/'), 'submodule');␊ + t.is(takeModuleWithDelimiter('sub=subsub=subsubmodule.js', '/'), 'subsubmodule');␊ + t.is(takeModuleWithDelimiter('sub=submodule.js', '\\\\'), 'submodule');␊ + t.is(takeModuleWithDelimiter('sub=subsub=subsubmodule.js', '\\\\'), 'subsubmodule');␊ ␊ var main = {␊ ␊ @@ -2978,12 +3230,12 @@ Generated by [AVA](https://avajs.dev). `, } -## dynamic-require-package-sub +## dynamic-require-resolve-index > Snapshot 1 { - 'entry.js': `'use strict';␊ + 'main.js': `'use strict';␊ ␊ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊ ␊ @@ -3145,41 +3397,72 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ commonjsRequire.resolve = commonjsResolve;␊ ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package-sub/node_modules/custom-module/entry.js", function (module, exports) {␊ - module.exports = 'custom-module';␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/index.js", function (module, exports) {␊ + module.exports = 'same-directory';␊ ␊ });␊ ␊ - var main = "./entry.js";␊ - var require$$0 = {␊ - main: main␊ - };␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/sub/index.js", function (module, exports) {␊ + module.exports = 'sub';␊ ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package-sub/node_modules/custom-module/package.json", function (module, exports) {␊ - module.exports = require$$0;␊ });␊ ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package-sub/node_modules/custom-module", function (module, exports) {␊ - module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-package-sub/node_modules/custom-module/entry.js", null);␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/node_modules/custom-module/index.js", function (module, exports) {␊ + module.exports = 'custom-module';␊ + ␊ });␊ ␊ - t.is(commonjsRequire("custom-module", "/$$rollup_base$$/fixtures/function/dynamic-require-package-sub/sub"), 'custom-module');␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index", function (module, exports) {␊ + module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/index.js", null);␊ + });␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/sub", function (module, exports) {␊ + module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/sub/index.js", null);␊ + });␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/node_modules/custom-module", function (module, exports) {␊ + module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/node_modules/custom-module/index.js", null);␊ + });␊ ␊ - var entry = {␊ + /* eslint-disable import/no-dynamic-require, global-require */␊ + ␊ + function takeModule(name) {␊ + return commonjsRequire(name,"/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/sub");␊ + }␊ ␊ + var sub = {␊ + parent: takeModule('..'),␊ + customModule: takeModule('custom-module')␊ };␊ ␊ - module.exports = entry;␊ + /* eslint-disable import/no-dynamic-require, global-require */␊ + ␊ + function takeModule$1(name) {␊ + return commonjsRequire(name,"/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index");␊ + }␊ + ␊ + t.is(takeModule$1('.'), 'same-directory');␊ + t.is(takeModule$1('./'), 'same-directory');␊ + t.is(takeModule$1('.//'), 'same-directory');␊ + ␊ + t.is(takeModule$1('./sub'), 'sub');␊ + ␊ + t.is(takeModule$1('custom-module'), 'custom-module');␊ + t.deepEqual(sub, { parent: 'same-directory', customModule: 'custom-module' });␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ `, } -## dynamic-require-relative-paths +## dynamic-require-resolve-reference > Snapshot 1 @@ -3346,32 +3629,37 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ commonjsRequire.resolve = commonjsResolve;␊ ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-relative-paths/sub/submodule.js", function (module, exports) {␊ - module.exports = 'submodule';␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module/index.js", function (module, exports) {␊ + module.exports = {␊ + foo: 'bar',␊ + };␊ ␊ });␊ ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-relative-paths/sub/subsub/subsubmodule.js", function (module, exports) {␊ - module.exports = 'subsubmodule';␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module2/index.js", function (module, exports) {␊ + module.exports = () => {␊ + return commonjsRequire.resolve('custom-module',"/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module2");␊ + };␊ ␊ });␊ ␊ - /* eslint-disable import/no-dynamic-require, global-require */␊ - ␊ - function takeModuleWithDelimiter(name, delimiter) {␊ - return commonjsRequire(`.${delimiter}${name.replace(/=/g, delimiter)}`,"/$$rollup_base$$/fixtures/function/dynamic-require-relative-paths");␊ - }␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module", function (module, exports) {␊ + module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module/index.js", null);␊ + });␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module2", function (module, exports) {␊ + module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module2/index.js", null);␊ + });␊ ␊ - t.is(takeModuleWithDelimiter('sub=submodule.js', '/'), 'submodule');␊ - t.is(takeModuleWithDelimiter('sub=subsub=subsubmodule.js', '/'), 'subsubmodule');␊ - t.is(takeModuleWithDelimiter('sub=submodule.js', '\\\\'), 'submodule');␊ - t.is(takeModuleWithDelimiter('sub=subsub=subsubmodule.js', '\\\\'), 'subsubmodule');␊ + t.is(␊ + commonjsRequire("custom-module2", "/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference")(),␊ + '/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module'␊ + );␊ ␊ var main = {␊ ␊ @@ -3381,7 +3669,7 @@ Generated by [AVA](https://avajs.dev). `, } -## dynamic-require-resolve-index +## dynamic-require-targets-fallback > Snapshot 1 @@ -3554,56 +3842,20 @@ Generated by [AVA](https://avajs.dev). commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ commonjsRequire.resolve = commonjsResolve;␊ ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/index.js", function (module, exports) {␊ - module.exports = 'same-directory';␊ - ␊ - });␊ - ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/sub/index.js", function (module, exports) {␊ - module.exports = 'sub';␊ - ␊ - });␊ - ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/node_modules/custom-module/index.js", function (module, exports) {␊ - module.exports = 'custom-module';␊ - ␊ - });␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-targets-fallback/dep1.js", function (module, exports) {␊ + module.exports = 'dep';␊ ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index", function (module, exports) {␊ - module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/index.js", null);␊ - });␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/sub", function (module, exports) {␊ - module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/sub/index.js", null);␊ - });␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/node_modules/custom-module", function (module, exports) {␊ - module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/node_modules/custom-module/index.js", null);␊ });␊ ␊ /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ - function takeModule(name) {␊ - return commonjsRequire(name,"/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index/sub");␊ - }␊ - ␊ - var sub = {␊ - parent: takeModule('..'),␊ - customModule: takeModule('custom-module')␊ - };␊ - ␊ - /* eslint-disable import/no-dynamic-require, global-require */␊ - ␊ - function takeModule$1(name) {␊ - return commonjsRequire(name,"/$$rollup_base$$/fixtures/function/dynamic-require-resolve-index");␊ + function takeModule(withName) {␊ + return commonjsRequire(withName,"/$$rollup_base$$/fixtures/function/dynamic-require-targets-fallback");␊ }␊ ␊ - t.is(takeModule$1('.'), 'same-directory');␊ - t.is(takeModule$1('./'), 'same-directory');␊ - t.is(takeModule$1('.//'), 'same-directory');␊ - ␊ - t.is(takeModule$1('./sub'), 'sub');␊ - ␊ - t.is(takeModule$1('custom-module'), 'custom-module');␊ - t.deepEqual(sub, { parent: 'same-directory', customModule: 'custom-module' });␊ + t.is(takeModule('./dep1.js'), 'dep');␊ + // The bundled code will run from test/helpers/util.js␊ + t.is(takeModule('../fixtures/function/dynamic-require-targets-fallback/dep2.js'), 'dep');␊ ␊ var main = {␊ ␊ @@ -3613,7 +3865,7 @@ Generated by [AVA](https://avajs.dev). `, } -## dynamic-require-resolve-reference +## dynamic-require-targets-no-fallback > Snapshot 1 @@ -3780,38 +4032,29 @@ Generated by [AVA](https://avajs.dev). cachedModule.loaded = true;␊ return cachedModule.exports;␊ } }␊ - return require(path);␊ + 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.');␊ }␊ ␊ commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ commonjsRequire.resolve = commonjsResolve;␊ ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module/index.js", function (module, exports) {␊ - module.exports = {␊ - foo: 'bar',␊ - };␊ + commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-targets-no-fallback/dep1.js", function (module, exports) {␊ + module.exports = 'dep';␊ ␊ });␊ ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module2/index.js", function (module, exports) {␊ - module.exports = () => {␊ - return commonjsRequire.resolve('custom-module',"/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module2");␊ - };␊ + /* eslint-disable import/no-dynamic-require, global-require */␊ ␊ - });␊ + function takeModule(withName) {␊ + return commonjsRequire(withName,"/$$rollup_base$$/fixtures/function/dynamic-require-targets-no-fallback");␊ + }␊ ␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module", function (module, exports) {␊ - module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module/index.js", null);␊ - });␊ - commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module2", function (module, exports) {␊ - module.exports = commonjsRequire("/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module2/index.js", null);␊ + t.is(takeModule('./dep1.js'), 'dep');␊ + t.throws(() => takeModule('./dep2.js'), {␊ + message:␊ + 'Could not dynamically require "./dep2.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'␊ });␊ ␊ - t.is(␊ - commonjsRequire("custom-module2", "/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference")(),␊ - '/$$rollup_base$$/fixtures/function/dynamic-require-resolve-reference/node_modules/custom-module'␊ - );␊ - ␊ var main = {␊ ␊ };␊ @@ -4275,7 +4518,7 @@ Generated by [AVA](https://avajs.dev). }␊ ␊ var encode = createCommonjsModule(function (module, exports) {␊ - exports.encodeURIComponent = function() {␊ + exports.encodeURIComponent = function () {␊ return encodeURIComponent(this.str);␊ };␊ ␊ @@ -5101,7 +5344,7 @@ Generated by [AVA](https://avajs.dev). { 'main.js': `'use strict';␊ ␊ - var multiply = function(a, b) {␊ + var multiply = function (a, b) {␊ return a * b;␊ };␊ ␊ @@ -5109,7 +5352,7 @@ Generated by [AVA](https://avajs.dev). ␊ /* eslint-disable global-require */␊ ␊ - var main = function() {␊ + var main = function () {␊ return multiply(2, foo);␊ };␊ ␊ @@ -6567,8 +6810,8 @@ Generated by [AVA](https://avajs.dev). return fn(module, module.exports), module.exports;␊ }␊ ␊ - function commonjsRequire (target) {␊ - throw new Error('Could not dynamically require "' + target + '". Please configure the dynamicRequireTargets option of @rollup/plugin-commonjs appropriately for this require call to behave properly.');␊ + function commonjsRequire (path) {␊ + 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.');␊ }␊ ␊ var foo = createCommonjsModule(function (module) {␊ @@ -6595,8 +6838,8 @@ Generated by [AVA](https://avajs.dev). return fn(module, module.exports), module.exports;␊ }␊ ␊ - function commonjsRequire (target) {␊ - throw new Error('Could not dynamically require "' + target + '". Please configure the dynamicRequireTargets option of @rollup/plugin-commonjs appropriately for this require call to behave properly.');␊ + function commonjsRequire (path) {␊ + 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.');␊ }␊ ␊ var foo = createCommonjsModule(function (module) {␊ @@ -6633,33 +6876,3 @@ Generated by [AVA](https://avajs.dev). module.exports = main;␊ `, } - -## unsupported-dynamic-require - -> Snapshot 1 - - { - 'main.js': `'use strict';␊ - ␊ - function commonjsRequire (target) {␊ - throw new Error('Could not dynamically require "' + target + '". Please configure the dynamicRequireTargets option of @rollup/plugin-commonjs appropriately for this require call to behave properly.');␊ - }␊ - ␊ - /* eslint-disable import/no-dynamic-require, global-require */␊ - ␊ - t.throws(() => commonjsRequire(getRequireTarget()), {␊ - message:␊ - 'Could not dynamically require "foo-bar". Please configure the dynamicRequireTargets option of @rollup/plugin-commonjs appropriately for this require call to behave properly.'␊ - });␊ - ␊ - function getRequireTarget() {␊ - return 'foo-bar';␊ - }␊ - ␊ - var main = {␊ - ␊ - };␊ - ␊ - module.exports = main;␊ - `, - } diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index 2522d27a6..a557beb7c 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ diff --git a/packages/commonjs/test/types.ts b/packages/commonjs/test/types.ts index 49fc2decf..d119c561f 100644 --- a/packages/commonjs/test/types.ts +++ b/packages/commonjs/test/types.ts @@ -15,6 +15,7 @@ const config: RollupOptions = { exclude: ['node_modules/foo/**', 'node_modules/bar/**', /node_modules/], extensions: ['.js', '.coffee'], ignoreGlobal: false, + ignoreDynamicRequires: true, requireReturnsDefault: 'auto', sourceMap: false, transformMixedEsModules: false, diff --git a/packages/commonjs/types/index.d.ts b/packages/commonjs/types/index.d.ts index 87b0b3f61..2b0b06797 100644 --- a/packages/commonjs/types/index.d.ts +++ b/packages/commonjs/types/index.d.ts @@ -35,10 +35,23 @@ interface RollupCommonJSOptions { */ ignoreGlobal?: boolean; /** - * If false, skips source map generation for CommonJS modules. This will improve performance. + * If false, skips source map generation for CommonJS modules. This will + * improve performance. * @default true */ sourceMap?: boolean; + /** + * Some `require` calls cannot be resolved statically to be translated to + * imports. + * When this option is set to `false`, the generated code will either + * directly throw an error when such a call is encountered or, when + * `dynamicRequireTargets` is used, when such a call cannot be resolved with a + * configured dynamic require target. + * Setting this option to `true` will instead leave the `require` call in the + * code or use it as a fallback for `dynamicRequireTargets`. + * @default false + */ + ignoreDynamicRequires?: boolean; /** * Instructs the plugin whether to enable mixed module transformations. This * is useful in scenarios with modules that contain a mix of ES `import`