@@ -258,6 +258,30 @@ Generated by [AVA](https://avajs.dev).
258258 `,
259259 }
260260
261+ ## conditional-require-non-strict
262+
263+ > Snapshot 1
264+
265+ {
266+ 'main.js': `'use strict';␊
267+ ␊
268+ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
269+ ␊
270+ var main = {};␊
271+ ␊
272+ commonjsGlobal.foo = true;␊
273+ ␊
274+ commonjsGlobal.bar = true;␊
275+ ␊
276+ commonjsGlobal.main = true;␊
277+ ␊
278+ t.is(commonjsGlobal.foo, true, 'foo');␊
279+ t.is(commonjsGlobal.main, true, 'main');␊
280+ ␊
281+ module.exports = main;␊
282+ `,
283+ }
284+
261285## custom-options
262286
263287> Snapshot 1
@@ -7565,3 +7589,127 @@ Generated by [AVA](https://avajs.dev).
75657589 module.exports = main;␊
75667590 `,
75677591 }
7592+
7593+ ## dynamic-require-empty
7594+
7595+ > Snapshot 1
7596+
7597+ {
7598+ 'main.js': `'use strict';␊
7599+ ␊
7600+ var submodule = {};␊
7601+ ␊
7602+ var hasRequiredSubmodule;␊
7603+ ␊
7604+ function requireSubmodule () {␊
7605+ if (hasRequiredSubmodule) return submodule;␊
7606+ hasRequiredSubmodule = 1;␊
7607+ ␊
7608+ return submodule;␊
7609+ }␊
7610+ ␊
7611+ var dynamicModules;␊
7612+ ␊
7613+ function getDynamicModules() {␊
7614+ return dynamicModules || (dynamicModules = {␊
7615+ "/fixtures/function/dynamic-require-empty/submodule.js": requireSubmodule␊
7616+ });␊
7617+ }␊
7618+ ␊
7619+ function createCommonjsRequire(originalModuleDir) {␊
7620+ function handleRequire(path) {␊
7621+ var resolvedPath = commonjsResolve(path, originalModuleDir);␊
7622+ if (resolvedPath !== null) {␊
7623+ return getDynamicModules()[resolvedPath]();␊
7624+ }␊
7625+ 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.');␊
7626+ }␊
7627+ handleRequire.resolve = function (path) {␊
7628+ var resolvedPath = commonjsResolve(path, originalModuleDir);␊
7629+ if (resolvedPath !== null) {␊
7630+ return resolvedPath;␊
7631+ }␊
7632+ return require.resolve(path);␊
7633+ };␊
7634+ return handleRequire;␊
7635+ }␊
7636+ ␊
7637+ function commonjsResolve (path, originalModuleDir) {␊
7638+ var shouldTryNodeModules = isPossibleNodeModulesPath(path);␊
7639+ path = normalize(path);␊
7640+ var relPath;␊
7641+ if (path[0] === '/') {␊
7642+ originalModuleDir = '';␊
7643+ }␊
7644+ var modules = getDynamicModules();␊
7645+ var checkedExtensions = ['', '.js', '.json'];␊
7646+ while (true) {␊
7647+ if (!shouldTryNodeModules) {␊
7648+ relPath = normalize(originalModuleDir + '/' + path);␊
7649+ } else {␊
7650+ relPath = normalize(originalModuleDir + '/node_modules/' + path);␊
7651+ }␊
7652+ ␊
7653+ if (relPath.endsWith('/..')) {␊
7654+ break; // Travelled too far up, avoid infinite loop␊
7655+ }␊
7656+ ␊
7657+ for (var extensionIndex = 0; extensionIndex < checkedExtensions.length; extensionIndex++) {␊
7658+ var resolvedPath = relPath + checkedExtensions[extensionIndex];␊
7659+ if (modules[resolvedPath]) {␊
7660+ return resolvedPath;␊
7661+ }␊
7662+ }␊
7663+ if (!shouldTryNodeModules) break;␊
7664+ var nextDir = normalize(originalModuleDir + '/..');␊
7665+ if (nextDir === originalModuleDir) break;␊
7666+ originalModuleDir = nextDir;␊
7667+ }␊
7668+ return null;␊
7669+ }␊
7670+ ␊
7671+ function isPossibleNodeModulesPath (modulePath) {␊
7672+ var c0 = modulePath[0];␊
7673+ if (c0 === '/' || c0 === '\\\\') return false;␊
7674+ var c1 = modulePath[1], c2 = modulePath[2];␊
7675+ if ((c0 === '.' && (!c1 || c1 === '/' || c1 === '\\\\')) ||␊
7676+ (c0 === '.' && c1 === '.' && (!c2 || c2 === '/' || c2 === '\\\\'))) return false;␊
7677+ if (c1 === ':' && (c2 === '/' || c2 === '\\\\')) return false;␊
7678+ return true;␊
7679+ }␊
7680+ ␊
7681+ function normalize (path) {␊
7682+ path = path.replace(/\\\\/g, '/');␊
7683+ var parts = path.split('/');␊
7684+ var slashed = parts[0] === '';␊
7685+ for (var i = 1; i < parts.length; i++) {␊
7686+ if (parts[i] === '.' || parts[i] === '') {␊
7687+ parts.splice(i--, 1);␊
7688+ }␊
7689+ }␊
7690+ for (var i = 1; i < parts.length; i++) {␊
7691+ if (parts[i] !== '..') continue;␊
7692+ if (i > 0 && parts[i - 1] !== '..' && parts[i - 1] !== '.') {␊
7693+ parts.splice(--i, 2);␊
7694+ i--;␊
7695+ }␊
7696+ }␊
7697+ path = parts.join('/');␊
7698+ if (slashed && path[0] !== '/') path = '/' + path;␊
7699+ else if (path.length === 0) path = '.';␊
7700+ return path;␊
7701+ }␊
7702+ ␊
7703+ var main = {};␊
7704+ ␊
7705+ /* eslint-disable import/no-dynamic-require, global-require */␊
7706+ ␊
7707+ function takeModule(withName) {␊
7708+ return createCommonjsRequire("/fixtures/function/dynamic-require-empty")(`./${withName}`);␊
7709+ }␊
7710+ ␊
7711+ t.deepEqual(takeModule('submodule'), {});␊
7712+ ␊
7713+ module.exports = main;␊
7714+ `,
7715+ }
0 commit comments