Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrangler module import under npm monorepos #7130

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/shy-cups-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": minor
---

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 the workspace root `node_modules` directory.
3 changes: 3 additions & 0 deletions fixtures/import-npm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# import-npm example

This is an npm workspace within pnpm in order to test dependencies managed through npm can also be resolved by wrangler
185 changes: 185 additions & 0 deletions fixtures/import-npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions fixtures/import-npm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "import-npm",
"private": true,
"description": "",
"author": "",
"workspaces": [
"packages/*"
],
"scripts": {
"check:type": "rm -rf node_modules && npm install && npm run check:type --workspaces",
"test:ci": "npm install && npm run test:ci --workspaces",
"test:watch": "npm install && npm run test:watch --workspaces",
"type:tests": "rm -rf node_modules && npm install && npm run type:tests --workspaces"
}
}
21 changes: 21 additions & 0 deletions fixtures/import-npm/packages/import-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "import-example",
"private": true,
"description": "",
"author": "",
"main": "src/index.js",
"scripts": {
"check:type": "tsc",
"test:ci": "vitest run",
"test:watch": "vitest",
"type:tests": "tsc -p ./tests/tsconfig.json"
},
"dependencies": {
"import-wasm-static": "../../../../fixtures/import-wasm-static"
},
"devDependencies": {
"@cloudflare/workers-tsconfig": "../../../../packages/workers-tsconfig",
"undici": "^5.28.4",
"wrangler": "../../../../packages/wrangler"
}
}
19 changes: 19 additions & 0 deletions fixtures/import-npm/packages/import-example/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// this is from the `import-wasm-static` fixture defined above
// and setup inside package.json to mimic an npm package
import multiply from "import-wasm-static/multiply.wasm";
import otherMultiple from "import-wasm-static/wasm/not-exported.wasm";

export default {
async fetch(request) {
// just instantiate and return something
// we're really just testing the imports at the top of this file
const multiplyModule = await WebAssembly.instantiate(multiply);
const otherModule = await WebAssembly.instantiate(otherMultiple);

const results = [
multiplyModule.exports.multiply(7, 3),
otherModule.exports.multiply(7, 3),
];
return new Response(results.join(", "));
},
};
26 changes: 26 additions & 0 deletions fixtures/import-npm/packages/import-example/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { resolve } from "path";
import { fetch } from "undici";
import { afterAll, beforeAll, describe, it } from "vitest";
import { runWranglerDev } from "../../../../shared/src/run-wrangler-long-lived";

describe("wrangler correctly imports wasm files with npm resolution", () => {
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined;

beforeAll(async () => {
({ ip, port, stop } = await runWranglerDev(resolve(__dirname, ".."), [
"--port=0",
"--inspector-port=0",
]));
});

afterAll(async () => {
await stop?.();
});

// if the worker compiles, is running, and returns 21 (7 * 3) we can assume that the wasm module was imported correctly
it("responds", async ({ expect }) => {
const response = await fetch(`http://${ip}:${port}/`);
const text = await response.text();
expect(text).toBe("21, 21");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
"compilerOptions": {
"types": ["node"]
},
"include": ["**/*.ts", "../../../../../node-types.d.ts"]
}
13 changes: 13 additions & 0 deletions fixtures/import-npm/packages/import-example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES2020",
"esModuleInterop": true,
"module": "CommonJS",
"lib": ["ES2020"],
"types": ["node"],
"moduleResolution": "node",
"noEmit": true,
"skipLibCheck": true
},
"include": ["tests", "../../../../node-types.d.ts"]
}
9 changes: 9 additions & 0 deletions fixtures/import-npm/packages/import-example/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineProject, mergeConfig } from "vitest/config";
import configShared from "../../../../vitest.shared";

export default mergeConfig(
configShared,
defineProject({
test: {},
})
);
4 changes: 4 additions & 0 deletions fixtures/import-npm/packages/import-example/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = "import-example"
compatibility_date = "2024-10-31"

main = "src/index.js"
11 changes: 11 additions & 0 deletions fixtures/import-npm/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": ["//"],
"tasks": {
"test": {
"dependsOn": ["wrangler#build"]
},
"test:ci": {
"dependsOn": ["wrangler#build"]
}
}
}
1 change: 0 additions & 1 deletion packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"nanoid": "^3.3.3",
"path-to-regexp": "^6.3.0",
"resolve": "^1.22.8",
"resolve.exports": "^2.0.2",
"selfsigned": "^2.0.1",
"source-map": "^0.6.1",
"unenv": "npm:unenv-nightly@2.0.0-20241121-161142-806b5c0",
Expand Down
20 changes: 0 additions & 20 deletions packages/wrangler/src/__tests__/module-collection.test.ts

This file was deleted.

Loading
Loading