Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 29ba2a0

Browse files
committed
Allow linking additional modules via CommonJS modules
1 parent 4660f2b commit 29ba2a0

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

packages/runner-vm/src/linker.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export class ModuleLinker {
101101
const exports = this.loadCommonJSModule(
102102
errorBase,
103103
identifier,
104+
spec,
104105
referencing.context
105106
);
106107
module = new vm.SyntheticModule<{ default: Context }>(
@@ -151,13 +152,24 @@ export class ModuleLinker {
151152
private loadCommonJSModule(
152153
errorBase: string,
153154
identifier: string,
155+
spec: string,
154156
context: vm.Context
155157
): any {
156158
// If we've already seen a module with the same identifier, return it, to
157159
// handle import cycles
158160
const cached = this.#cjsModuleCache.get(identifier);
159161
if (cached) return cached.exports;
160162

163+
const additionalModule = this.additionalModules[spec];
164+
const module: CommonJSModule = { exports: {} };
165+
166+
// If this is an additional module, return it immediately
167+
if (additionalModule) {
168+
module.exports.default = additionalModule.default;
169+
this.#cjsModuleCache.set(identifier, module);
170+
return module.exports;
171+
}
172+
161173
// Find first matching module rule ("ignore" requires relative paths)
162174
const relativeIdentifier = path.relative("", identifier);
163175
const rule = this.moduleRules.find((rule) =>
@@ -170,9 +182,8 @@ export class ModuleLinker {
170182
);
171183
}
172184

173-
// Create module and store in cache now as require is sync, so may load
174-
// this module again before this function returns
175-
const module: CommonJSModule = { exports: {} };
185+
// Store in cache now as require is sync, so may load this module again
186+
// before this function returns
176187
this.#cjsModuleCache.set(identifier, module);
177188

178189
// Load module based on rule type
@@ -221,7 +232,7 @@ export class ModuleLinker {
221232
const errorBase = `Unable to resolve "${relative}" dependency "${spec}"`;
222233
// Get path to specified module relative to referencing module
223234
const identifier = path.resolve(referencingDirname, spec);
224-
return this.loadCommonJSModule(errorBase, identifier, context);
235+
return this.loadCommonJSModule(errorBase, identifier, spec, context);
225236
};
226237
}
227238
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const additional = require("ADDITIONAL");
2+
module.exports = `CommonJS ${additional.default}`;

packages/runner-vm/test/linker.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ test("ModuleLinker: stack trace references correct line for CommonJS modules", a
207207
t.regex(e.stack, /cjserror\.cjs:2:9/);
208208
}
209209
});
210+
test("ModuleLinker: links additional module via CommonJS module", async (t) => {
211+
const callback = (defaultExport: string) => {
212+
t.is(defaultExport, "CommonJS test");
213+
};
214+
const additionalModules = { ADDITIONAL: { default: "test" } };
215+
await runner.run(
216+
{ callback },
217+
{ code: 'import s from "./cjsadditional.cjs"; callback(s);', filePath },
218+
processedModuleRules,
219+
additionalModules
220+
);
221+
});
210222

211223
const sizePath = path.join(fixturesPath, "size");
212224
const sizeEntryPath = path.join(sizePath, "entry.mjs");

0 commit comments

Comments
 (0)