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

Commit b1ca38f

Browse files
committed
Fix detection of imports in string scripts without scriptPaths
Module resolution requires a path to resolve modules to. When users just specify a `script`, we don't have this. Miniflare was meant to block importing in this case, but the detection was using the full module path (e.g. `/path/to/<script:0>`) where it should've been using the name (e.g. `<script:0>`).
1 parent e507beb commit b1ca38f

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

packages/miniflare/src/plugins/core/modules.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,18 @@ export class ModuleLocator {
210210
// noinspection JSUnusedGlobalSymbols
211211
const visitors = {
212212
ImportDeclaration: (node: estree.ImportDeclaration) => {
213-
this.#visitModule(modulePath, type, node.source);
213+
this.#visitModule(modulePath, name, type, node.source);
214214
},
215215
ExportNamedDeclaration: (node: estree.ExportNamedDeclaration) => {
216216
if (node.source != null) {
217-
this.#visitModule(modulePath, type, node.source);
217+
this.#visitModule(modulePath, name, type, node.source);
218218
}
219219
},
220220
ExportAllDeclaration: (node: estree.ExportAllDeclaration) => {
221-
this.#visitModule(modulePath, type, node.source);
221+
this.#visitModule(modulePath, name, type, node.source);
222222
},
223223
ImportExpression: (node: estree.ImportExpression) => {
224-
this.#visitModule(modulePath, type, node.source);
224+
this.#visitModule(modulePath, name, type, node.source);
225225
},
226226
CallExpression: isESM
227227
? undefined
@@ -233,7 +233,7 @@ export class ModuleLocator {
233233
node.callee.name === "require" &&
234234
argument !== undefined
235235
) {
236-
this.#visitModule(modulePath, type, argument);
236+
this.#visitModule(modulePath, name, type, argument);
237237
}
238238
},
239239
};
@@ -242,10 +242,11 @@ export class ModuleLocator {
242242

243243
#visitModule(
244244
referencingPath: string,
245+
referencingName: string,
245246
referencingType: JavaScriptModuleRuleType,
246247
specExpression: estree.Expression | estree.SpreadElement
247248
) {
248-
if (maybeGetStringScriptPathIndex(referencingPath) !== undefined) {
249+
if (maybeGetStringScriptPathIndex(referencingName) !== undefined) {
249250
const prefix = getResolveErrorPrefix(referencingPath);
250251
throw new MiniflareCoreError(
251252
"ERR_MODULE_STRING_SCRIPT",

packages/miniflare/test/index.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ test("Miniflare: `node:`, `cloudflare:` and `workerd:` modules", async (t) => {
464464
const mf = new Miniflare({
465465
modules: true,
466466
compatibilityFlags: ["nodejs_compat", "rtti_api"],
467+
scriptPath: "index.mjs",
467468
script: `
468469
import assert from "node:assert";
469470
import { Buffer } from "node:buffer";

0 commit comments

Comments
 (0)