Skip to content

Commit aeb0fe0

Browse files
fix: resolve npm modules correctly (#656)
1 parent e3a458e commit aeb0fe0

File tree

3 files changed

+91
-53
lines changed

3 files changed

+91
-53
lines changed

.changeset/thick-bees-yell.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: resolve npm modules correctly
6+
7+
When implementing legacy module specifiers, we didn't throughly test the interaction when there weren't any other files next to the entry worker, and importing npm modules. It would create a Regex that matched _every_ import, and fail because a file of that name wasn't present in the source directory. This fix constructs a better regex, applies it only when there are more files next to the worker, and increases test coverage for that scenario.
8+
9+
Fixes https://github.com/cloudflare/wrangler2/issues/655

packages/wrangler/src/__tests__/publish.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,30 @@ export default{
23622362
`"Deprecation warning: detected a legacy module import in \\"./index.js\\". This will stop working in the future. Replace references to \\"index.wasm\\" with \\"./index.wasm\\";"`
23632363
);
23642364
});
2365+
2366+
it("should not match regular module specifiers when there aren't any possible legacy module matches", async () => {
2367+
// see https://github.com/cloudflare/wrangler2/issues/655 for bug details
2368+
2369+
fs.writeFileSync(
2370+
"./index.js",
2371+
`import inner from './inner/index.js'; export default {};`
2372+
);
2373+
fs.mkdirSync("./inner", { recursive: true });
2374+
fs.writeFileSync("./inner/index.js", `export default 123`);
2375+
mockSubDomainRequest();
2376+
mockUploadWorkerRequest();
2377+
2378+
await runWrangler(
2379+
"publish index.js --compatibility-date 2022-03-17 --name test-name"
2380+
);
2381+
expect(std.out).toMatchInlineSnapshot(`
2382+
"Uploaded test-name (TIMINGS)
2383+
Published test-name (TIMINGS)
2384+
test-name.test-sub-domain.workers.dev"
2385+
`);
2386+
expect(std.err).toMatchInlineSnapshot(`""`);
2387+
expect(std.warn).toMatchInlineSnapshot(`""`);
2388+
});
23652389
});
23662390

23672391
/** Write a mock Worker script to disk. */

packages/wrangler/src/module-collection.ts

+58-53
Original file line numberDiff line numberDiff line change
@@ -117,62 +117,67 @@ export default function createModuleCollector(props: {
117117
});
118118
});
119119

120-
build.onResolve(
121-
{
122-
filter: new RegExp(
123-
[...props.wrangler1xlegacyModuleReferences.fileNames]
124-
.map((fileName) => `^${fileName}$`)
125-
.join("|")
126-
),
127-
},
128-
async (args: esbuild.OnResolveArgs) => {
129-
if (
130-
args.kind !== "import-statement" &&
131-
args.kind !== "require-call"
132-
) {
133-
return;
134-
}
135-
// In the future, this will simply throw an error
136-
console.warn(
137-
`Deprecation warning: detected a legacy module import in "./${path.relative(
138-
process.cwd(),
139-
args.importer
140-
)}". This will stop working in the future. Replace references to "${
120+
if (props.wrangler1xlegacyModuleReferences.fileNames.size > 0) {
121+
build.onResolve(
122+
{
123+
filter: new RegExp(
124+
"^(" +
125+
[...props.wrangler1xlegacyModuleReferences.fileNames].join(
126+
"|"
127+
) +
128+
")$"
129+
),
130+
},
131+
async (args: esbuild.OnResolveArgs) => {
132+
if (
133+
args.kind !== "import-statement" &&
134+
args.kind !== "require-call"
135+
) {
136+
return;
137+
}
138+
// In the future, this will simply throw an error
139+
console.warn(
140+
`Deprecation warning: detected a legacy module import in "./${path.relative(
141+
process.cwd(),
142+
args.importer
143+
)}". This will stop working in the future. Replace references to "${
144+
args.path
145+
}" with "./${args.path}";`
146+
);
147+
148+
// take the file and massage it to a
149+
// transportable/manageable format
150+
const filePath = path.join(
151+
props.wrangler1xlegacyModuleReferences.rootDirectory,
141152
args.path
142-
}" with "./${args.path}";`
143-
);
153+
);
154+
const fileContent = await readFile(filePath);
155+
const fileHash = crypto
156+
.createHash("sha1")
157+
.update(fileContent)
158+
.digest("hex");
159+
const fileName = `./${fileHash}-${path.basename(args.path)}`;
144160

145-
// take the file and massage it to a
146-
// transportable/manageable format
147-
const filePath = path.join(
148-
props.wrangler1xlegacyModuleReferences.rootDirectory,
149-
args.path
150-
);
151-
const fileContent = await readFile(filePath);
152-
const fileHash = crypto
153-
.createHash("sha1")
154-
.update(fileContent)
155-
.digest("hex");
156-
const fileName = `./${fileHash}-${path.basename(args.path)}`;
157-
158-
const { rule } =
159-
rulesMatchers.find(({ regex }) => regex.test(fileName)) || {};
160-
if (rule) {
161-
// add the module to the array
162-
modules.push({
163-
name: fileName,
164-
content: fileContent,
165-
type: RuleTypeToModuleType[rule.type],
166-
});
167-
return {
168-
path: fileName, // change the reference to the changed module
169-
external: props.format === "modules", // mark it as external in the bundle
170-
namespace: `wrangler-module-${rule.type}`, // just a tag, this isn't strictly necessary
171-
watchFiles: [filePath], // we also add the file to esbuild's watch list
172-
};
161+
const { rule } =
162+
rulesMatchers.find(({ regex }) => regex.test(fileName)) || {};
163+
if (rule) {
164+
// add the module to the array
165+
modules.push({
166+
name: fileName,
167+
content: fileContent,
168+
type: RuleTypeToModuleType[rule.type],
169+
});
170+
return {
171+
path: fileName, // change the reference to the changed module
172+
external: props.format === "modules", // mark it as external in the bundle
173+
namespace: `wrangler-module-${rule.type}`, // just a tag, this isn't strictly necessary
174+
watchFiles: [filePath], // we also add the file to esbuild's watch list
175+
};
176+
}
173177
}
174-
}
175-
);
178+
);
179+
}
180+
176181
// ~ end legacy module specifier support ~
177182

178183
rules?.forEach((rule) => {

0 commit comments

Comments
 (0)