Skip to content

Commit 11338d0

Browse files
authored
Fix wrangler module import under npm monorepos (#7130)
* Update import resolution for files and package exports In an npm workspace environment, wrangler will now be able to successfully resolve package exports. Previously, wrangler would only be able to resolve modules in a relative `node_modules` directory and not workspace root `node_modules` directory. * Use esbuild plugin
1 parent 6ecc74e commit 11338d0

File tree

16 files changed

+341
-91
lines changed

16 files changed

+341
-91
lines changed

.changeset/shy-cups-grin.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
Update import resolution for files and package exports
6+
7+
In an npm workspace environment, wrangler will now be able to successfully resolve package exports.
8+
9+
Previously, wrangler would only be able to resolve modules in a relative `node_modules` directory and not the workspace root `node_modules` directory.

fixtures/import-npm/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# import-npm example
2+
3+
This is an npm workspace within pnpm in order to test dependencies managed through npm can also be resolved by wrangler

fixtures/import-npm/package-lock.json

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

fixtures/import-npm/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "import-npm",
3+
"private": true,
4+
"description": "",
5+
"author": "",
6+
"workspaces": [
7+
"packages/*"
8+
],
9+
"scripts": {
10+
"check:type": "rm -rf node_modules && npm install && npm run check:type --workspaces",
11+
"test:ci": "npm install && npm run test:ci --workspaces",
12+
"test:watch": "npm install && npm run test:watch --workspaces",
13+
"type:tests": "rm -rf node_modules && npm install && npm run type:tests --workspaces"
14+
}
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "import-example",
3+
"private": true,
4+
"description": "",
5+
"author": "",
6+
"main": "src/index.js",
7+
"scripts": {
8+
"check:type": "tsc",
9+
"test:ci": "vitest run",
10+
"test:watch": "vitest",
11+
"type:tests": "tsc -p ./tests/tsconfig.json"
12+
},
13+
"dependencies": {
14+
"import-wasm-static": "../../../../fixtures/import-wasm-static"
15+
},
16+
"devDependencies": {
17+
"@cloudflare/workers-tsconfig": "../../../../packages/workers-tsconfig",
18+
"undici": "^5.28.4",
19+
"wrangler": "../../../../packages/wrangler"
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// this is from the `import-wasm-static` fixture defined above
2+
// and setup inside package.json to mimic an npm package
3+
import multiply from "import-wasm-static/multiply.wasm";
4+
import otherMultiple from "import-wasm-static/wasm/not-exported.wasm";
5+
6+
export default {
7+
async fetch(request) {
8+
// just instantiate and return something
9+
// we're really just testing the imports at the top of this file
10+
const multiplyModule = await WebAssembly.instantiate(multiply);
11+
const otherModule = await WebAssembly.instantiate(otherMultiple);
12+
13+
const results = [
14+
multiplyModule.exports.multiply(7, 3),
15+
otherModule.exports.multiply(7, 3),
16+
];
17+
return new Response(results.join(", "));
18+
},
19+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { resolve } from "path";
2+
import { fetch } from "undici";
3+
import { afterAll, beforeAll, describe, it } from "vitest";
4+
import { runWranglerDev } from "../../../../shared/src/run-wrangler-long-lived";
5+
6+
describe("wrangler correctly imports wasm files with npm resolution", () => {
7+
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined;
8+
9+
beforeAll(async () => {
10+
({ ip, port, stop } = await runWranglerDev(resolve(__dirname, ".."), [
11+
"--port=0",
12+
"--inspector-port=0",
13+
]));
14+
});
15+
16+
afterAll(async () => {
17+
await stop?.();
18+
});
19+
20+
// if the worker compiles, is running, and returns 21 (7 * 3) we can assume that the wasm module was imported correctly
21+
it("responds", async ({ expect }) => {
22+
const response = await fetch(`http://${ip}:${port}/`);
23+
const text = await response.text();
24+
expect(text).toBe("21, 21");
25+
});
26+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
3+
"compilerOptions": {
4+
"types": ["node"]
5+
},
6+
"include": ["**/*.ts", "../../../../../node-types.d.ts"]
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"esModuleInterop": true,
5+
"module": "CommonJS",
6+
"lib": ["ES2020"],
7+
"types": ["node"],
8+
"moduleResolution": "node",
9+
"noEmit": true,
10+
"skipLibCheck": true
11+
},
12+
"include": ["tests", "../../../../node-types.d.ts"]
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineProject, mergeConfig } from "vitest/config";
2+
import configShared from "../../../../vitest.shared";
3+
4+
export default mergeConfig(
5+
configShared,
6+
defineProject({
7+
test: {},
8+
})
9+
);

0 commit comments

Comments
 (0)