-
Notifications
You must be signed in to change notification settings - Fork 741
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
6ecc74e
commit 11338d0
Showing
16 changed files
with
341 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
fixtures/import-npm/packages/import-example/tests/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
fixtures/import-npm/packages/import-example/tests/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
fixtures/import-npm/packages/import-example/vitest.config.mts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {}, | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.