From 5337f25b83b895ceef9dd94f4489d4c065553121 Mon Sep 17 00:00:00 2001 From: greym0uth Date: Fri, 20 May 2022 17:58:19 -0700 Subject: [PATCH] fix bundling with newer wasm-pack --- .gitignore | 3 ++- src/index.ts | 72 +++++++++++++++++++++++----------------------------- 2 files changed, 34 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index a548dc6..b6a32a1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules dist esm yarn.lock -example/my-crate/target \ No newline at end of file +example/my-crate/target +.DS_Store diff --git a/src/index.ts b/src/index.ts index de11496..71785b2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -94,51 +94,29 @@ function vitePluginWasmPack( }, async buildStart(_inputOptions) { - const prepareBuild = async (cratePath: string, isNodeModule: boolean) => { - const pkgPath = isNodeModule - ? path.dirname(require.resolve(cratePath)) - : path.join(cratePath, pkg); + const prepareBuild = async (cratePath: string) => { + const pkgPath = path.join(cratePath, pkg); const crateName = path.basename(cratePath); if (!fs.existsSync(pkgPath)) { - if (isNodeModule) { - console.error( - chalk.bold.red('Error: ') + - `Can't find ${chalk.bold(pkgPath)}, run ${chalk.bold.red( - `npm install ${cratePath}` - )} first` - ); - } else { - console.error( - chalk.bold.red('Error: ') + - `Can't find ${chalk.bold(pkgPath)}, run ${chalk.bold.red( - `wasm-pack build ${cratePath} --target web` - )} first` - ); - } + console.error( + chalk.bold.red('Error: ') + + `Can't find ${chalk.bold(pkgPath)}, run ${chalk.bold.red( + `wasm-pack build ${cratePath} --target web` + )} first` + ); } - if (!isNodeModule) { - // copy pkg generated by wasm-pack to node_modules - try { - await fs.copy(pkgPath, path.join('node_modules', crateName)); - } catch (error) { - this.error(`copy crates failed`); - } + // copy pkg generated by wasm-pack to node_modules + try { + await fs.copy(pkgPath, path.join('node_modules', crateName)); + } catch (error) { + this.error(`copy crates failed`); } // replace default load path with '/assets/xxx.wasm' const jsName = crateName.replace(/\-/g, '_') + '.js'; - /** - * if use node module and name is '@group/test' - * cratePath === '@group/test' - * crateName === 'test' - */ - let jsPath = path.join('./node_modules', crateName, jsName); - if (isNodeModule) { - jsPath = path.join(pkgPath, jsName); - } - const regex = /input = new URL\('(.+)'.+;/g; let code = fs.readFileSync(path.resolve(jsPath), { encoding: 'utf-8' }); + const regex = /input = new URL\('(.+)'.+;/g; code = code.replace(regex, (_match, group1) => { return `input = "${path.posix.join( config_base, @@ -150,12 +128,26 @@ function vitePluginWasmPack( }; for await (const cratePath of cratePaths) { - await prepareBuild(cratePath, false); + await prepareBuild(cratePath); } + }, - for await (const cratePath of modulePaths) { - await prepareBuild(cratePath, true); - } + transform(code, id) { + const cratePath = modulePaths.find((modulePath) => require.resolve(modulePath) === id); + if (!cratePath) return code; + const crateName = path.basename(cratePath); + + const wasmAssetPath = path.posix.join( + config_base, + config_assetsDir, + crateName + ); + const regex = /input = new URL\('(.+)'.+;/g; + code = code.replace(regex, `input = '${wasmAssetPath}'`); + // Newer versions of wasm-pack use 'import.meta.url' to get the js file. + code = code.replace(/input = import\.meta\.url/g, `input = '${wasmAssetPath}.js'`); + + return code; }, configureServer({ middlewares }) {