From ee6f387d517eb78e974a92e7e39f60e7f1d3231c Mon Sep 17 00:00:00 2001 From: blaine-arcjet <146491715+blaine-arcjet@users.noreply.github.com> Date: Fri, 9 Feb 2024 08:53:05 -0700 Subject: [PATCH] chore(rollup): Externalize all imports that end with `.wasm?module` (#217) This change externalizes any file we import that ends in `.wasm?module` and links to the cloudflare worker documentation. This is a cleaner solution than what existed once we start adding more wasm modules, such as [Rate Limit](https://github.com/arcjet/arcjet-js/pull/205). --- analyze/rollup.config.js | 6 ------ rollup-config/index.js | 13 +++++++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/analyze/rollup.config.js b/analyze/rollup.config.js index afd84d916..badca9348 100644 --- a/analyze/rollup.config.js +++ b/analyze/rollup.config.js @@ -13,12 +13,6 @@ export default createConfig(import.meta.url, { external: true, }; } - if (source === "./wasm/arcjet_analyze_js_req_bg.wasm?module") { - return { - id: "./wasm/arcjet_analyze_js_req_bg.wasm?module", - external: true, - }; - } // TODO: Generation of this file can be handled via rollup plugin so we // wouldn't need to externalize here if (source === "./wasm/arcjet.wasm.js") { diff --git a/rollup-config/index.js b/rollup-config/index.js index 42b40fcc3..7aa871ebf 100644 --- a/rollup-config/index.js +++ b/rollup-config/index.js @@ -96,6 +96,19 @@ export function createConfig(root, { plugins = [] } = {}) { include: "test/*.ts", noEmitOnError: true, }), + { + name: "externalize-edge-wasm", + resolveId(id) { + // Cloudflare uses the `.wasm?module` suffix to make WebAssembly + // available in their Workers product. This is documented at + // https://developers.cloudflare.com/workers/runtime-apis/webassembly/javascript/#bundling + // Next.js supports the same syntax, but it is undocumented. + if (id.endsWith(".wasm?module")) { + return { id, external: true }; + } + return null; + }, + }, ...plugins, ], };