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 d019cff90..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; @@ -437,7 +440,8 @@ export default function transformCommonjs( uses.exports || uses.require || uses.commonjsHelpers || - hasRemovedRequire + hasRemovedRequire || + isRestorableCompiledEsm ) && (ignoreGlobal || !uses.global) ) { diff --git a/packages/commonjs/test/fixtures/form/compiled-esm-define-exports-empty/input.js b/packages/commonjs/test/fixtures/form/compiled-esm-define-exports-empty/input.js new file mode 100644 index 000000000..a6a850fe2 --- /dev/null +++ b/packages/commonjs/test/fixtures/form/compiled-esm-define-exports-empty/input.js @@ -0,0 +1 @@ +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/packages/commonjs/test/fixtures/form/compiled-esm-define-exports-empty/output.js b/packages/commonjs/test/fixtures/form/compiled-esm-define-exports-empty/output.js new file mode 100644 index 000000000..43b03ffa5 --- /dev/null +++ b/packages/commonjs/test/fixtures/form/compiled-esm-define-exports-empty/output.js @@ -0,0 +1,6 @@ +var input = /*#__PURE__*/Object.defineProperty({ + +}, '__esModule', {value: true}); + +export default input; +export { input as __moduleExports }; 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` diff --git a/packages/node-resolve/CHANGELOG.md b/packages/node-resolve/CHANGELOG.md index 3c5b72103..727a4d954 100755 --- a/packages/node-resolve/CHANGELOG.md +++ b/packages/node-resolve/CHANGELOG.md @@ -1,5 +1,13 @@ # @rollup/plugin-node-resolve ChangeLog +## v11.2.1 + +_2021-03-26_ + +### Bugfixes + +- fix: fs.exists is incorrectly promisified (#835) + ## v11.2.0 _2021-02-14_ diff --git a/packages/node-resolve/package.json b/packages/node-resolve/package.json index 3e33fde3f..d4c316e19 100644 --- a/packages/node-resolve/package.json +++ b/packages/node-resolve/package.json @@ -1,6 +1,6 @@ { "name": "@rollup/plugin-node-resolve", - "version": "11.2.0", + "version": "11.2.1", "publishConfig": { "access": "public" }, diff --git a/packages/node-resolve/src/fs.js b/packages/node-resolve/src/fs.js index 11fa93067..a0b9346a5 100644 --- a/packages/node-resolve/src/fs.js +++ b/packages/node-resolve/src/fs.js @@ -2,8 +2,16 @@ import fs from 'fs'; import { promisify } from 'util'; -export const exists = promisify(fs.exists); +export const access = promisify(fs.access); export const readFile = promisify(fs.readFile); export const realpath = promisify(fs.realpath); export { realpathSync } from 'fs'; export const stat = promisify(fs.stat); +export async function exists(filePath) { + try { + await access(filePath); + return true; + } catch { + return false; + } +} diff --git a/packages/replace/CHANGELOG.md b/packages/replace/CHANGELOG.md index 9994d28fa..d50a3780e 100755 --- a/packages/replace/CHANGELOG.md +++ b/packages/replace/CHANGELOG.md @@ -1,5 +1,13 @@ # @rollup/plugin-replace ChangeLog +## v2.4.2 + +_2021-03-26_ + +### Updates + +- docs: document the `values` option (#814) + ## v2.4.1 _2021-02-22_ diff --git a/packages/replace/README.md b/packages/replace/README.md index abb42e5f7..4b415d911 100644 --- a/packages/replace/README.md +++ b/packages/replace/README.md @@ -114,6 +114,31 @@ Default: `null` A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted. +### `values` + +Type: `{ [key: String]: Replacement }`, where `Replacement` is either a string or a `function` that returns a string. +Default: `{}` + +To avoid mixing replacement strings with the other options, you can specify replacements in the `values` option. For example, the following signature: + +```js +replace({ + include: ["src/**/*.js"], + changed: "replaced" +}); +``` + +Can be replaced with: + +```js +replace({ + include: ["src/**/*.js"], + values: { + changed: "replaced" + } +}); +``` + ## Word Boundaries By default, values will only match if they are surrounded by _word boundaries_. diff --git a/packages/replace/package.json b/packages/replace/package.json index fa9c415f1..ae1b15af4 100644 --- a/packages/replace/package.json +++ b/packages/replace/package.json @@ -1,6 +1,6 @@ { "name": "@rollup/plugin-replace", - "version": "2.4.1", + "version": "2.4.2", "publishConfig": { "access": "public" }, diff --git a/packages/typescript/CHANGELOG.md b/packages/typescript/CHANGELOG.md index 3b26c14cc..3f847c8e1 100644 --- a/packages/typescript/CHANGELOG.md +++ b/packages/typescript/CHANGELOG.md @@ -1,5 +1,14 @@ # @rollup/plugin-typescript ChangeLog +## v8.2.1 + +_2021-03-26_ + +### Bugfixes + +- fix: bump TypeScript version (#818) +- fix: update readme and peerDeps version (#830) + ## v8.2.0 _2021-02-14_ diff --git a/packages/typescript/README.md b/packages/typescript/README.md index 70b47bfc9..2a32944ce 100644 --- a/packages/typescript/README.md +++ b/packages/typescript/README.md @@ -13,7 +13,7 @@ ## Requirements -This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+. This plugin also requires at least [TypeScript 3.4](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html). +This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+. This plugin also requires at least [TypeScript 3.7](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html). ## Install diff --git a/packages/typescript/package.json b/packages/typescript/package.json index 8d9a3b676..ed03e77cb 100644 --- a/packages/typescript/package.json +++ b/packages/typescript/package.json @@ -1,6 +1,6 @@ { "name": "@rollup/plugin-typescript", - "version": "8.2.0", + "version": "8.2.1", "publishConfig": { "access": "public" }, @@ -48,7 +48,7 @@ "peerDependencies": { "rollup": "^2.14.0", "tslib": "*", - "typescript": ">=3.4.0" + "typescript": ">=3.7.0" }, "dependencies": { "@rollup/pluginutils": "^3.1.0", @@ -61,7 +61,7 @@ "@types/node": "^10.0.0", "buble": "^0.20.0", "rollup": "^2.14.0", - "typescript": "^4.1.2" + "typescript": "^4.2.2" }, "types": "types/index.d.ts" } diff --git a/packages/typescript/src/preflight.ts b/packages/typescript/src/preflight.ts index 39cc1731c..3b7e70e2d 100644 --- a/packages/typescript/src/preflight.ts +++ b/packages/typescript/src/preflight.ts @@ -17,8 +17,6 @@ ${pluginName}: Rollup requires that TypeScript produces ES Modules. Unfortunatel "module" other than "esnext". Unless you know what you're doing, please change "module" to "esnext" in the target tsconfig.json file or plugin options.`.replace(/\n/g, ''); -const rootDirErrorMessage = `${pluginName}: The "rootDir" or "rootDirs" option is required when the "tsconfig" option is not specified and "declaration" is "true".`; - const tsLibErrorMessage = `${pluginName}: Could not find module 'tslib', which is required by this plugin. Is it installed?`; let undef; @@ -30,11 +28,6 @@ export const preflight = ({ config, context, rollupOptions, tslib }: PreflightOp context.warn(moduleErrorMessage); } - const { options } = config; - if (options.declaration && !options.configFilePath && !options.rootDir && !options.rootDirs) { - context.error(rootDirErrorMessage); - } - if (!rollupOptions.preserveModules && tslib === null) { context.error(tsLibErrorMessage); } diff --git a/packages/typescript/test/snapshots/tsconfig.ts.md b/packages/typescript/test/snapshots/tsconfig.ts.md deleted file mode 100644 index cd5733ae1..000000000 --- a/packages/typescript/test/snapshots/tsconfig.ts.md +++ /dev/null @@ -1,25 +0,0 @@ -# Snapshot report for `test/tsconfig.ts` - -The actual snapshot is saved in `tsconfig.ts.snap`. - -Generated by [AVA](https://avajs.dev). - -## inline config without tsconfig + rootDir - -> Snapshot 1 - - [ - 'main.js', - 'types/main.d.ts', - ] - -## inline config without tsconfig without rootDir fails - -> Snapshot 1 - - Error { - code: 'PLUGIN_ERROR', - hook: 'buildStart', - plugin: 'typescript', - message: '@rollup/plugin-typescript: The "rootDir" or "rootDirs" option is required when the "tsconfig" option is not specified and "declaration" is "true".', - } diff --git a/packages/typescript/test/snapshots/tsconfig.ts.snap b/packages/typescript/test/snapshots/tsconfig.ts.snap deleted file mode 100644 index ecf6b0f74..000000000 Binary files a/packages/typescript/test/snapshots/tsconfig.ts.snap and /dev/null differ diff --git a/packages/typescript/test/test.js b/packages/typescript/test/test.js index 55bfb158e..47cf62734 100644 --- a/packages/typescript/test/test.js +++ b/packages/typescript/test/test.js @@ -1020,11 +1020,7 @@ test.serial('picks up on newly included typescript files in watch mode', async ( t.true(usage, 'should contain usage'); }); -// TODO: upgrade TypeScript when there is a release containing the fix for this issue -// https://github.com/microsoft/TypeScript/pull/41811. Then enable this test -// More details at https://github.com/rollup/plugins/issues/287. This fails with the message: -// Unexpected token (Note that you need plugins to import files that are not JavaScript) -test.serial.skip('works when code is in src directory', async (t) => { +test.serial('works when code is in src directory', async (t) => { const bundle = await rollup({ input: 'fixtures/src-dir/src/index.ts', output: [ diff --git a/packages/typescript/test/tsconfig.ts b/packages/typescript/test/tsconfig.ts deleted file mode 100644 index 567e9d9b6..000000000 --- a/packages/typescript/test/tsconfig.ts +++ /dev/null @@ -1,50 +0,0 @@ -import test from 'ava'; -import { rollup } from 'rollup'; - -import typescript from '..'; - -import { getCode, onwarn } from '../../../util/test'; - -test.beforeEach(() => process.chdir(__dirname)); - -test.serial('inline config without tsconfig + rootDir', async (t) => { - const bundle = await rollup({ - input: 'fixtures/basic/main.ts', - plugins: [ - typescript({ - declaration: true, - declarationDir: 'fixtures/basic/dist/types', - exclude: 'fixtures/basic/dist/types', - include: 'fixtures/basic/*.ts', - tsconfig: false, - rootDir: 'fixtures/basic' - }) - ], - onwarn - }); - const files = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true); - const [, { source }] = files; - - t.snapshot(files.map(({ fileName }) => fileName)); - t.true((source as string)?.includes('declare const answer = 42;')); -}); - -test.serial('inline config without tsconfig without rootDir fails', async (t) => { - const fail = () => - rollup({ - input: 'fixtures/basic/main.ts', - plugins: [ - typescript({ - declaration: true, - declarationDir: 'fixtures/basic/dist/types', - exclude: 'fixtures/basic/dist/types', - include: 'fixtures/basic/*.ts', - tsconfig: false - }) - ], - onwarn - }); - - const error = await t.throwsAsync(fail); - t.snapshot(error); -}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fee7f3d3f..3c62f4cf0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -460,11 +460,11 @@ importers: devDependencies: '@rollup/plugin-buble': 0.21.3_rollup@2.32.1 '@rollup/plugin-commonjs': 11.1.0_rollup@2.32.1 - '@rollup/plugin-typescript': 5.0.2_rollup@2.32.1+typescript@4.1.2 + '@rollup/plugin-typescript': 5.0.2_rollup@2.32.1+typescript@4.2.2 '@types/node': 10.17.48 buble: 0.20.0 rollup: 2.32.1 - typescript: 4.1.2 + typescript: 4.2.2 specifiers: '@rollup/plugin-buble': ^0.21.3 '@rollup/plugin-commonjs': ^11.1.0 @@ -474,7 +474,7 @@ importers: buble: ^0.20.0 resolve: ^1.17.0 rollup: ^2.14.0 - typescript: ^4.1.2 + typescript: ^4.2.2 packages/url: dependencies: '@rollup/pluginutils': 3.1.0_rollup@2.32.1 @@ -1790,6 +1790,21 @@ packages: typescript: '>=3.4.0' resolution: integrity: sha512-CkS028Itwjqm1uLbFVfpJgtVtnNvZ+og/m6UlNRR5wOOnNTWPcVQzOu5xGdEX+WWJxdvWIqUq2uR/RBt2ZipWg== + /@rollup/plugin-typescript/5.0.2_rollup@2.32.1+typescript@4.2.2: + dependencies: + '@rollup/pluginutils': 3.1.0_rollup@2.32.1 + resolve: 1.18.1 + rollup: 2.32.1 + typescript: 4.2.2 + dev: true + engines: + node: '>=8.0.0' + peerDependencies: + rollup: ^2.14.0 + tslib: '*' + typescript: '>=3.4.0' + resolution: + integrity: sha512-CkS028Itwjqm1uLbFVfpJgtVtnNvZ+og/m6UlNRR5wOOnNTWPcVQzOu5xGdEX+WWJxdvWIqUq2uR/RBt2ZipWg== /@rollup/plugin-typescript/6.0.0_rollup@2.32.1+typescript@4.1.2: dependencies: '@rollup/pluginutils': 3.1.0_rollup@2.32.1 @@ -7441,6 +7456,13 @@ packages: hasBin: true resolution: integrity: sha512-thGloWsGH3SOxv1SoY7QojKi0tc+8FnOmiarEGMbd/lar7QOEd3hvlx3Fp5y6FlDUGl9L+pd4n2e+oToGMmhRQ== + /typescript/4.2.2: + dev: true + engines: + node: '>=4.2.0' + hasBin: true + resolution: + integrity: sha512-tbb+NVrLfnsJy3M59lsDgrzWIflR4d4TIUjz+heUnHZwdF7YsrMTKoRERiIvI2lvBG95dfpLxB21WZhys1bgaQ== /unicode-canonical-property-names-ecmascript/1.0.4: engines: node: '>=4'