diff --git a/fixtures/import-npm/README.md b/fixtures/import-npm/README.md new file mode 100644 index 0000000000000..102e9248c1013 --- /dev/null +++ b/fixtures/import-npm/README.md @@ -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 \ No newline at end of file diff --git a/fixtures/import-npm/package-lock.json b/fixtures/import-npm/package-lock.json new file mode 100644 index 0000000000000..13ab9633e7fd7 --- /dev/null +++ b/fixtures/import-npm/package-lock.json @@ -0,0 +1,198 @@ +{ + "name": "import-npm", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "import-npm", + "workspaces": [ + "packages/*" + ] + }, + "../../packages/workers-tsconfig": { + "name": "@cloudflare/workers-tsconfig", + "version": "0.0.0", + "dev": true + }, + "../../packages/wrangler": { + "version": "3.84.0", + "dev": true, + "license": "MIT OR Apache-2.0", + "dependencies": { + "@cloudflare/kv-asset-handler": "workspace:*", + "@cloudflare/workers-shared": "workspace:*", + "@esbuild-plugins/node-globals-polyfill": "^0.2.3", + "@esbuild-plugins/node-modules-polyfill": "^0.2.2", + "blake3-wasm": "^2.1.5", + "chokidar": "^3.5.3", + "date-fns": "^4.1.0", + "esbuild": "0.17.19", + "itty-time": "^1.0.6", + "miniflare": "workspace:*", + "nanoid": "^3.3.3", + "path-to-regexp": "^6.3.0", + "resolve": "^1.22.8", + "selfsigned": "^2.0.1", + "source-map": "^0.6.1", + "unenv": "npm:unenv-nightly@2.0.0-20241024-111401-d4156ac", + "workerd": "1.20241022.0", + "xxhash-wasm": "^1.0.1" + }, + "bin": { + "wrangler": "bin/wrangler.js", + "wrangler2": "bin/wrangler.js" + }, + "devDependencies": { + "@cloudflare/cli": "workspace:*", + "@cloudflare/eslint-config-worker": "workspace:*", + "@cloudflare/pages-shared": "workspace:^", + "@cloudflare/types": "6.18.4", + "@cloudflare/workers-tsconfig": "workspace:*", + "@cloudflare/workers-types": "^4.20241022.0", + "@cspotcode/source-map-support": "0.8.1", + "@iarna/toml": "^3.0.0", + "@microsoft/api-extractor": "^7.47.0", + "@sentry/node": "^7.86.0", + "@sentry/types": "^7.86.0", + "@sentry/utils": "^7.86.0", + "@types/body-parser": "^1.19.2", + "@types/command-exists": "^1.2.0", + "@types/express": "^4.17.13", + "@types/glob-to-regexp": "^0.4.1", + "@types/is-ci": "^3.0.0", + "@types/javascript-time-ago": "^2.0.3", + "@types/mime": "^3.0.4", + "@types/minimatch": "^5.1.2", + "@types/prompts": "^2.0.14", + "@types/react": "^18.3.3", + "@types/resolve": "^1.20.6", + "@types/serve-static": "^1.13.10", + "@types/shell-quote": "^1.7.2", + "@types/signal-exit": "^3.0.1", + "@types/supports-color": "^8.1.1", + "@types/ws": "^8.5.7", + "@types/yargs": "^17.0.22", + "@vitest/ui": "catalog:default", + "@webcontainer/env": "^1.1.0", + "body-parser": "^1.20.0", + "chalk": "^5.2.0", + "cli-table3": "^0.6.3", + "clipboardy": "^3.0.0", + "cmd-shim": "^4.1.0", + "command-exists": "^1.2.9", + "concurrently": "^8.2.2", + "devtools-protocol": "^0.0.1182435", + "dotenv": "^16.0.0", + "execa": "^6.1.0", + "express": "^4.18.1", + "find-up": "^6.3.0", + "get-port": "^7.0.0", + "glob-to-regexp": "^0.4.1", + "http-terminator": "^3.2.0", + "https-proxy-agent": "7.0.2", + "ignore": "^5.2.0", + "ink": "^3.2.0", + "ink-select-input": "^4.2.1", + "ink-spinner": "^4.0.3", + "ink-table": "^3.0.0", + "is-ci": "^3.0.1", + "javascript-time-ago": "^2.5.4", + "md5-file": "5.0.0", + "mime": "^3.0.0", + "minimatch": "^5.1.0", + "mock-socket": "^9.3.1", + "msw": "2.4.3", + "open": "^8.4.0", + "p-queue": "^7.2.0", + "patch-console": "^1.0.0", + "pretty-bytes": "^6.0.0", + "prompts": "^2.4.2", + "ps-list": "^8.1.1", + "react": "^18.3.1", + "react-error-boundary": "^3.1.4", + "semiver": "^1.1.0", + "serve-static": "^1.15.0", + "shell-quote": "^1.8.1", + "shellac": "^0.8.0", + "signal-exit": "^3.0.7", + "strip-ansi": "^7.1.0", + "supports-color": "^9.2.2", + "timeago.js": "^4.0.2", + "ts-dedent": "^2.2.0", + "ts-json-schema-generator": "^1.5.0", + "undici": "catalog:default", + "update-check": "^1.5.4", + "vitest": "catalog:default", + "vitest-websocket-mock": "^0.4.0", + "ws": "^8.18.0", + "xdg-app-paths": "^8.3.0", + "yargs": "^17.7.2", + "yoga-layout": "file:../../vendor/yoga-layout-2.0.0-beta.1.tgz" + }, + "engines": { + "node": ">=16.17.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + }, + "peerDependencies": { + "@cloudflare/workers-types": "^4.20241022.0" + }, + "peerDependenciesMeta": { + "@cloudflare/workers-types": { + "optional": true + } + } + }, + "../import-wasm-static": {}, + "node_modules/@cloudflare/workers-tsconfig": { + "resolved": "../../packages/workers-tsconfig", + "link": true + }, + "node_modules/@fastify/busboy": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/@fastify/busboy/-/busboy-2.1.1.tgz", + "integrity": "sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14" + } + }, + "node_modules/import-example": { + "resolved": "packages/import-example", + "link": true + }, + "node_modules/import-wasm-static": { + "resolved": "../import-wasm-static", + "link": true + }, + "node_modules/undici": { + "version": "5.28.4", + "resolved": "https://registry.npmjs.org/undici/-/undici-5.28.4.tgz", + "integrity": "sha512-72RFADWFqKmUb2hmmvNODKL3p9hcB6Gt2DOQMis1SEBaV6a4MH8soBvzg+95CYhCKPFedut2JY9bMfrDl9D23g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@fastify/busboy": "^2.0.0" + }, + "engines": { + "node": ">=14.0" + } + }, + "node_modules/wrangler": { + "resolved": "../../packages/wrangler", + "link": true + }, + "packages/import-example": { + "dependencies": { + "import-wasm-static": "../../../../fixtures/import-wasm-static" + }, + "devDependencies": { + "@cloudflare/workers-tsconfig": "../../../../packages/workers-tsconfig", + "undici": "^5.28.4", + "wrangler": "../../../../packages/wrangler" + } + } + } +} diff --git a/fixtures/import-npm/package.json b/fixtures/import-npm/package.json new file mode 100644 index 0000000000000..5fe397ebae46e --- /dev/null +++ b/fixtures/import-npm/package.json @@ -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/*" + ] +} diff --git a/fixtures/import-npm/packages/import-example/package.json b/fixtures/import-npm/packages/import-example/package.json new file mode 100644 index 0000000000000..7c364acc9d1db --- /dev/null +++ b/fixtures/import-npm/packages/import-example/package.json @@ -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" + } +} diff --git a/fixtures/import-npm/packages/import-example/src/index.js b/fixtures/import-npm/packages/import-example/src/index.js new file mode 100644 index 0000000000000..bf0f6d049d0df --- /dev/null +++ b/fixtures/import-npm/packages/import-example/src/index.js @@ -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(", ")); + }, +}; diff --git a/fixtures/import-npm/packages/import-example/tests/index.test.ts b/fixtures/import-npm/packages/import-example/tests/index.test.ts new file mode 100644 index 0000000000000..d95e5578fc841 --- /dev/null +++ b/fixtures/import-npm/packages/import-example/tests/index.test.ts @@ -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) | 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"); + }); +}); diff --git a/fixtures/import-npm/packages/import-example/tests/tsconfig.json b/fixtures/import-npm/packages/import-example/tests/tsconfig.json new file mode 100644 index 0000000000000..540c9d65408af --- /dev/null +++ b/fixtures/import-npm/packages/import-example/tests/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "@cloudflare/workers-tsconfig/tsconfig.json", + "compilerOptions": { + "types": ["node"] + }, + "include": ["**/*.ts", "../../../../../node-types.d.ts"] +} diff --git a/fixtures/import-npm/packages/import-example/tsconfig.json b/fixtures/import-npm/packages/import-example/tsconfig.json new file mode 100644 index 0000000000000..fbcafb8e45f25 --- /dev/null +++ b/fixtures/import-npm/packages/import-example/tsconfig.json @@ -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"] +} diff --git a/fixtures/import-npm/packages/import-example/vitest.config.mts b/fixtures/import-npm/packages/import-example/vitest.config.mts new file mode 100644 index 0000000000000..12e6a147fc9ac --- /dev/null +++ b/fixtures/import-npm/packages/import-example/vitest.config.mts @@ -0,0 +1,9 @@ +import { defineProject, mergeConfig } from "vitest/config"; +import configShared from "../../../../vitest.shared"; + +export default mergeConfig( + configShared, + defineProject({ + test: {}, + }) +); diff --git a/fixtures/import-npm/packages/import-example/wrangler.toml b/fixtures/import-npm/packages/import-example/wrangler.toml new file mode 100644 index 0000000000000..e79b15dd724a0 --- /dev/null +++ b/fixtures/import-npm/packages/import-example/wrangler.toml @@ -0,0 +1,4 @@ +name = "import-example" +compatibility_date = "2024-10-31" + +main = "src/index.js"