@@ -4,7 +4,10 @@ import chalk from 'chalk';
44import { isString } from 'narrowing' ;
55import { Plugin } from 'vite' ;
66
7- function vitePluginWasmPack ( crates : string [ ] | string ) : Plugin {
7+ function vitePluginWasmPack (
8+ crates : string [ ] | string ,
9+ isNodeModule ?: boolean
10+ ) : Plugin {
811 const prefix = '@vite-plugin-wasm-pack@' ;
912 const pkg = 'pkg' ; // default folder of wasm-pack module
1013 let config_base : string ;
@@ -17,7 +20,10 @@ function vitePluginWasmPack(crates: string[] | string): Plugin {
1720 const wasmMap = new Map < string , string > ( ) ; // { 'my_crate_bg.wasm': '../../wasm-game/pkg/wasm_game_bg.wasm' }
1821 cratePaths . forEach ( ( cratePath ) => {
1922 const wasmFile = wasmFilename ( cratePath ) ;
20- wasmMap . set ( wasmFile , path . join ( cratePath , pkg , wasmFile ) ) ;
23+ const wasmPath = isNodeModule
24+ ? path . join ( 'node_modules' , cratePath , wasmFile )
25+ : path . join ( cratePath , pkg , wasmFile ) ;
26+ wasmMap . set ( wasmFile , wasmPath ) ;
2127 } ) ;
2228
2329 return {
@@ -52,7 +58,9 @@ function vitePluginWasmPack(crates: string[] | string): Plugin {
5258
5359 async buildStart ( inputOptions ) {
5460 for await ( const cratePath of cratePaths ) {
55- const pkgPath = path . join ( cratePath , pkg ) ;
61+ const pkgPath = isNodeModule
62+ ? path . join ( 'node_modules' , cratePath )
63+ : path . join ( cratePath , pkg ) ;
5664 const crateName = path . basename ( cratePath ) ;
5765 if ( ! fs . existsSync ( pkgPath ) ) {
5866 console . error (
@@ -65,12 +73,13 @@ function vitePluginWasmPack(crates: string[] | string): Plugin {
6573 `Can't find ${ pkgPath } , run 'wasm-pack build ${ cratePath } --target web' first`
6674 ) ;
6775 }
68- // copy pkg generated by wasm-pack to node_modules
69- try {
70- await fs . copy ( pkgPath , path . join ( 'node_modules' , crateName ) ) ;
71- } catch ( error ) {
72- this . error ( `copy crates failed` ) ;
73- return ;
76+ if ( ! isNodeModule ) {
77+ // copy pkg generated by wasm-pack to node_modules
78+ try {
79+ await fs . copy ( pkgPath , path . join ( 'node_modules' , crateName ) ) ;
80+ } catch ( error ) {
81+ this . error ( `copy crates failed` ) ;
82+ }
7483 }
7584 // replace default load path with '/assets/xxx.wasm'
7685 const jsName = crateName . replace ( '-' , '_' ) + '.js' ;
0 commit comments