Skip to content

Commit c954336

Browse files
authored
fix(commonjs): short-circuit to actual module entry point when using circular ref through a different entry (#888)
1 parent fa9fba6 commit c954336

File tree

8 files changed

+669
-107
lines changed

8 files changed

+669
-107
lines changed

packages/commonjs/src/dynamic-packages-manager.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@ export function getPackageEntryPoint(dirPath) {
2121
}
2222

2323
export function getDynamicPackagesModule(dynamicRequireModuleDirPaths, commonDir) {
24-
let code = `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');`;
24+
let code = `const commonjsRegisterOrShort = require('${HELPERS_ID}?commonjsRegisterOrShort');`;
2525
for (const dir of dynamicRequireModuleDirPaths) {
2626
const entryPoint = getPackageEntryPoint(dir);
2727

28-
code += `\ncommonjsRegister(${JSON.stringify(
28+
code += `\ncommonjsRegisterOrShort(${JSON.stringify(
2929
getVirtualPathForDynamicRequirePath(dir, commonDir)
30-
)}, function (module, exports) {
31-
module.exports = require(${JSON.stringify(normalizePathSlashes(join(dir, entryPoint)))});
32-
});`;
30+
)}, ${JSON.stringify(getVirtualPathForDynamicRequirePath(join(dir, entryPoint), commonDir))});`;
3331
}
3432
return code;
3533
}

packages/commonjs/src/helpers.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,18 @@ export function commonjsRegister (path, loader) {
7373
DYNAMIC_REQUIRE_LOADERS[path] = loader;
7474
}
7575
76+
export function commonjsRegisterOrShort (path, to) {
77+
const resolvedPath = commonjsResolveImpl(path, null, true);
78+
if (resolvedPath !== null && DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
79+
DYNAMIC_REQUIRE_CACHE[path] = DYNAMIC_REQUIRE_CACHE[resolvedPath];
80+
} else {
81+
DYNAMIC_REQUIRE_SHORTS[path] = to;
82+
}
83+
}
84+
7685
const DYNAMIC_REQUIRE_LOADERS = Object.create(null);
7786
const DYNAMIC_REQUIRE_CACHE = Object.create(null);
87+
const DYNAMIC_REQUIRE_SHORTS = Object.create(null);
7888
const DEFAULT_PARENT_MODULE = {
7989
id: '<' + 'rollup>', exports: {}, parent: undefined, filename: null, loaded: false, children: [], paths: []
8090
};
@@ -179,10 +189,13 @@ export function commonjsResolveImpl (path, originalModuleDir, testCache) {
179189
const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];
180190
if (DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
181191
return resolvedPath;
182-
};
192+
}
193+
if (DYNAMIC_REQUIRE_SHORTS[resolvedPath]) {
194+
return resolvedPath;
195+
}
183196
if (DYNAMIC_REQUIRE_LOADERS[resolvedPath]) {
184197
return resolvedPath;
185-
};
198+
}
186199
}
187200
if (!shouldTryNodeModules) break;
188201
const nextDir = normalize(originalModuleDir + '/..');
@@ -201,10 +214,17 @@ export function commonjsResolve (path, originalModuleDir) {
201214
}
202215
203216
export function commonjsRequire (path, originalModuleDir) {
204-
const resolvedPath = commonjsResolveImpl(path, originalModuleDir, true);
217+
let resolvedPath = commonjsResolveImpl(path, originalModuleDir, true);
205218
if (resolvedPath !== null) {
206219
let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];
207220
if (cachedModule) return cachedModule.exports;
221+
let shortTo = DYNAMIC_REQUIRE_SHORTS[resolvedPath];
222+
if (shortTo) {
223+
cachedModule = DYNAMIC_REQUIRE_CACHE[shortTo];
224+
if (cachedModule)
225+
return cachedModule.exports;
226+
resolvedPath = commonjsResolveImpl(shortTo, null, true);
227+
}
208228
const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];
209229
if (loader) {
210230
DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { nodeResolve } = require('@rollup/plugin-node-resolve');
2+
3+
module.exports = {
4+
description: 'resolves circular references through indirect access (../ to module\'s root)',
5+
options: {
6+
plugins: [nodeResolve()]
7+
},
8+
pluginOptions: {
9+
dynamicRequireTargets: [
10+
'fixtures/function/dynamic-require-root-circular/node_modules/custom-module{,/**/*.js}'
11+
]
12+
}
13+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* eslint-disable import/no-dynamic-require, global-require */
2+
3+
const custom = require('custom-module');
4+
5+
t.is(custom.get1(), 'all good');
6+
t.is(custom.get2(), 'indirect ref');
7+
t.is(custom.get3(), custom.get1());

packages/commonjs/test/fixtures/function/dynamic-require-root-circular/node_modules/custom-module/index.js

Lines changed: 9 additions & 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-root-circular/node_modules/custom-module/lib/circular.js

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

0 commit comments

Comments
 (0)