Skip to content

Commit

Permalink
Embed npm workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbabcock committed Oct 31, 2024
1 parent f4529d2 commit 0bf2da7
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 0 deletions.
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
198 changes: 198 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": "",
"scripts": {
"check:type": "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": "npm run type:tests --workspaces"
},
"workspaces": [
"packages/*"
]
}
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"

0 comments on commit 0bf2da7

Please sign in to comment.