summaryrefslogtreecommitdiff
path: root/packages/integrations/cloudflare/src/utils/rewriteWasmImportPath.ts
blob: ada19bb56be24e5308be884ce354c57031104f0f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import esbuild from 'esbuild';
import { basename } from 'node:path';

/**
 *
 * @param relativePathToAssets - relative path from the final location for the current esbuild output bundle, to the assets directory.
 */
export function rewriteWasmImportPath({
	relativePathToAssets,
}: {
	relativePathToAssets: string;
}): esbuild.Plugin {
	return {
		name: 'wasm-loader',
		setup(build) {
			build.onResolve({ filter: /.*\.wasm.mjs$/ }, (args) => {
				const updatedPath = [
					relativePathToAssets.replaceAll('\\', '/'),
					basename(args.path).replace(/\.mjs$/, ''),
				].join('/');

				return {
					path: updatedPath,
					external: true, // mark it as external in the bundle
				};
			});
		},
	};
}