Skip to content

Commit ec70687

Browse files
authored
fix(commonjs): fixed access to node_modules dynamic module with subfolder (i.e 'logform/json') (#601)
1 parent 41d6fd3 commit ec70687

File tree

10 files changed

+245
-1
lines changed

10 files changed

+245
-1
lines changed

packages/commonjs/src/transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export function transformCommonjs(
254254
}
255255

256256
function hasDynamicModuleForPath(source) {
257-
if (!/[/\\]/.test(source)) {
257+
if (!/^(?:\.{0,2}[/\\]|[A-Za-z]:[/\\])/.test(source)) {
258258
try {
259259
const resolvedPath = normalizePathSlashes(
260260
nodeResolveSync(source, { basedir: dirname(id) })
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { nodeResolve } = require('@rollup/plugin-node-resolve');
2+
3+
module.exports = {
4+
description: 'resolves imports of node_modules module with halfway / subfolder access',
5+
options: {
6+
plugins: [nodeResolve()]
7+
},
8+
pluginOptions: {
9+
dynamicRequireTargets: [
10+
'fixtures/function/dynamic-require-slash-access',
11+
'fixtures/function/dynamic-require-slash-access/sub',
12+
'fixtures/function/dynamic-require-slash-access/node_modules/custom-module',
13+
'fixtures/function/dynamic-require-slash-access/node_modules/custom-module2/*.js'
14+
]
15+
}
16+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'same-directory';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-disable import/no-dynamic-require, global-require */
2+
3+
function takeModule(name) {
4+
return require(name);
5+
}
6+
7+
t.is(takeModule('.'), 'same-directory');
8+
t.is(takeModule('./'), 'same-directory');
9+
t.is(takeModule('.//'), 'same-directory');
10+
11+
t.is(takeModule('./sub'), 'sub');
12+
13+
t.is(takeModule('custom-module'), 'custom-module + sub');
14+
t.deepEqual(require('./sub/sub'), { parent: 'same-directory', customModule: 'custom-module' });

packages/commonjs/test/fixtures/function/dynamic-require-slash-access/node_modules/custom-module/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/commonjs/test/fixtures/function/dynamic-require-slash-access/node_modules/custom-module2/sub.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'sub';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* eslint-disable import/no-dynamic-require, global-require */
2+
3+
function takeModule(name) {
4+
return require(name);
5+
}
6+
7+
module.exports = {
8+
parent: takeModule('..'),
9+
customModule: takeModule('custom-module')
10+
};

packages/commonjs/test/snapshots/function.js.md

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,6 +2750,206 @@ Generated by [AVA](https://avajs.dev).
27502750
`,
27512751
}
27522752

2753+
## dynamic-require-slash-access
2754+
2755+
> Snapshot 1
2756+
2757+
{
2758+
'main.js': `'use strict';␊
2759+
2760+
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
2761+
2762+
function commonjsRegister (path, loader) {␊
2763+
DYNAMIC_REQUIRE_LOADERS[path] = loader;␊
2764+
}␊
2765+
2766+
const DYNAMIC_REQUIRE_LOADERS = Object.create(null);␊
2767+
const DYNAMIC_REQUIRE_CACHE = Object.create(null);␊
2768+
const DEFAULT_PARENT_MODULE = {␊
2769+
id: '<' + 'rollup>', exports: {}, parent: undefined, filename: null, loaded: false, children: [], paths: []␊
2770+
};␊
2771+
const CHECKED_EXTENSIONS = ['', '.js', '.json'];␊
2772+
2773+
function normalize (path) {␊
2774+
path = path.replace(/\\\\/g, '/');␊
2775+
const parts = path.split('/');␊
2776+
const slashed = parts[0] === '';␊
2777+
for (let i = 1; i < parts.length; i++) {␊
2778+
if (parts[i] === '.' || parts[i] === '') {␊
2779+
parts.splice(i--, 1);␊
2780+
}␊
2781+
}␊
2782+
for (let i = 1; i < parts.length; i++) {␊
2783+
if (parts[i] !== '..') continue;␊
2784+
if (i > 0 && parts[i - 1] !== '..' && parts[i - 1] !== '.') {␊
2785+
parts.splice(--i, 2);␊
2786+
i--;␊
2787+
}␊
2788+
}␊
2789+
path = parts.join('/');␊
2790+
if (slashed && path[0] !== '/')␊
2791+
path = '/' + path;␊
2792+
else if (path.length === 0)␊
2793+
path = '.';␊
2794+
return path;␊
2795+
}␊
2796+
2797+
function join () {␊
2798+
if (arguments.length === 0)␊
2799+
return '.';␊
2800+
let joined;␊
2801+
for (let i = 0; i < arguments.length; ++i) {␊
2802+
let arg = arguments[i];␊
2803+
if (arg.length > 0) {␊
2804+
if (joined === undefined)␊
2805+
joined = arg;␊
2806+
else␊
2807+
joined += '/' + arg;␊
2808+
}␊
2809+
}␊
2810+
if (joined === undefined)␊
2811+
return '.';␊
2812+
2813+
return joined;␊
2814+
}␊
2815+
2816+
function isPossibleNodeModulesPath (modulePath) {␊
2817+
let c0 = modulePath[0];␊
2818+
if (c0 === '/' || c0 === '\\\\') return false;␊
2819+
let c1 = modulePath[1], c2 = modulePath[2];␊
2820+
if ((c0 === '.' && (!c1 || c1 === '/' || c1 === '\\\\')) ||␊
2821+
(c0 === '.' && c1 === '.' && (!c2 || c2 === '/' || c2 === '\\\\'))) return false;␊
2822+
if (c1 === ':' && (c2 === '/' || c2 === '\\\\'))␊
2823+
return false;␊
2824+
return true;␊
2825+
}␊
2826+
2827+
function dirname (path) {␊
2828+
if (path.length === 0)␊
2829+
return '.';␊
2830+
2831+
let i = path.length - 1;␊
2832+
while (i > 0) {␊
2833+
const c = path.charCodeAt(i);␊
2834+
if ((c === 47 || c === 92) && i !== path.length - 1)␊
2835+
break;␊
2836+
i--;␊
2837+
}␊
2838+
2839+
if (i > 0)␊
2840+
return path.substr(0, i);␊
2841+
2842+
if (path.chartCodeAt(0) === 47 || path.chartCodeAt(0) === 92)␊
2843+
return path.charAt(0);␊
2844+
2845+
return '.';␊
2846+
}␊
2847+
2848+
function commonjsRequire (path, originalModuleDir) {␊
2849+
const shouldTryNodeModules = isPossibleNodeModulesPath(path);␊
2850+
path = normalize(path);␊
2851+
let relPath;␊
2852+
while (true) {␊
2853+
if (!shouldTryNodeModules) {␊
2854+
relPath = originalModuleDir ? normalize(originalModuleDir + '/' + path) : path;␊
2855+
} else if (originalModuleDir) {␊
2856+
relPath = normalize(originalModuleDir + '/node_modules/' + path);␊
2857+
} else {␊
2858+
relPath = normalize(join('node_modules', path));␊
2859+
}␊
2860+
for (let extensionIndex = 0; extensionIndex < CHECKED_EXTENSIONS.length; extensionIndex++) {␊
2861+
const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];␊
2862+
let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];␊
2863+
if (cachedModule) return cachedModule.exports;␊
2864+
const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];␊
2865+
if (loader) {␊
2866+
DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {␊
2867+
id: resolvedPath,␊
2868+
filename: resolvedPath,␊
2869+
path: dirname(resolvedPath),␊
2870+
exports: {},␊
2871+
parent: DEFAULT_PARENT_MODULE,␊
2872+
loaded: false,␊
2873+
children: [],␊
2874+
paths: [],␊
2875+
require: function (path, base) {␊
2876+
return commonjsRequire(path, (base === undefined || base === null) ? cachedModule.path : base);␊
2877+
}␊
2878+
};␊
2879+
try {␊
2880+
loader.call(commonjsGlobal, cachedModule, cachedModule.exports);␊
2881+
} catch (error) {␊
2882+
delete DYNAMIC_REQUIRE_CACHE[resolvedPath];␊
2883+
throw error;␊
2884+
}␊
2885+
cachedModule.loaded = true;␊
2886+
return cachedModule.exports;␊
2887+
} }␊
2888+
if (!shouldTryNodeModules) break;␊
2889+
const nextDir = normalize(originalModuleDir + '/..');␊
2890+
if (nextDir === originalModuleDir) break;␊
2891+
originalModuleDir = nextDir;␊
2892+
}␊
2893+
return require(path);␊
2894+
}␊
2895+
2896+
commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊
2897+
2898+
var dynamicRequireSlashAccess = 'same-directory';␊
2899+
2900+
var sub = 'sub';␊
2901+
2902+
var customModule = 'custom-module' + ' + ' + commonjsRequire("custom-module2/sub", "/$$rollup_base$$/fixtures/function/dynamic-require-slash-access/node_modules/custom-module");␊
2903+
2904+
commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-slash-access/node_modules/custom-module2/sub.js", function (module, exports) {␊
2905+
module.exports = 'sub';␊
2906+
2907+
});␊
2908+
2909+
commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-slash-access", function (module, exports) {␊
2910+
module.exports = dynamicRequireSlashAccess;␊
2911+
});␊
2912+
commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-slash-access/sub", function (module, exports) {␊
2913+
module.exports = sub;␊
2914+
});␊
2915+
commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-slash-access/node_modules/custom-module", function (module, exports) {␊
2916+
module.exports = customModule;␊
2917+
});␊
2918+
2919+
/* eslint-disable import/no-dynamic-require, global-require */␊
2920+
2921+
function takeModule(name) {␊
2922+
return commonjsRequire(name,"/$$rollup_base$$/fixtures/function/dynamic-require-slash-access/sub");␊
2923+
}␊
2924+
2925+
var sub$1 = {␊
2926+
parent: takeModule('..'),␊
2927+
customModule: takeModule('custom-module')␊
2928+
};␊
2929+
2930+
/* eslint-disable import/no-dynamic-require, global-require */␊
2931+
2932+
function takeModule$1(name) {␊
2933+
return commonjsRequire(name,"/$$rollup_base$$/fixtures/function/dynamic-require-slash-access");␊
2934+
}␊
2935+
2936+
t.is(takeModule$1('.'), 'same-directory');␊
2937+
t.is(takeModule$1('./'), 'same-directory');␊
2938+
t.is(takeModule$1('.//'), 'same-directory');␊
2939+
2940+
t.is(takeModule$1('./sub'), 'sub');␊
2941+
2942+
t.is(takeModule$1('custom-module'), 'custom-module + sub');␊
2943+
t.deepEqual(sub$1, { parent: 'same-directory', customModule: 'custom-module' });␊
2944+
2945+
var main = {␊
2946+
2947+
};␊
2948+
2949+
module.exports = main;␊
2950+
`,
2951+
}
2952+
27532953
## es6-export-with-global-sniffing
27542954

27552955
> Snapshot 1
138 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)