diff --git a/src/index.ts b/src/index.ts index e6554c1..5fe0087 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,7 +41,7 @@ export function ViteRsw(userOptions: RswPluginOptions): Plugin { const fileId = _path?.[1].replace(/^\//, '') + '_bg.wasm'; // build wasm file - if (!wasmMap.has(fileId) && config?.mode !== 'development') { + if (!wasmMap.has(fileId) && config?.command !== 'serve') { const source = fs.readFileSync(path.resolve(crateRoot, fileId)); const hash = createHash('md5').update(String(source)).digest('hex').substring(0, 8); const _name = config?.build?.assetsDir + '/' + path.basename(fileId).replace('.wasm', `.${hash}.wasm`); @@ -51,12 +51,12 @@ export function ViteRsw(userOptions: RswPluginOptions): Plugin { }); // fix: fetch or URL - code = loadWasm(code, path.basename(fileId), _name); + code = loadWasm(code, path.basename(fileId), config.base + _name); return code; } // wasm file path and rsw hot - return code.replace('import.meta.url.replace(/\\.js$/, \'_bg.wasm\');', `fetch('/${fileId}')`) + rswHot; + return code.replace(/import\.meta\.url\.replace\(\/\\\\\.js\$\/, \\'_bg\.wasm\\'\);/, `fetch('/${fileId}')`) + rswHot; } return code; }, diff --git a/src/utils.ts b/src/utils.ts index 7408c92..ddceffa 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -94,8 +94,9 @@ export function loadWasm(code: string, oPath: string, nPath: string) { `~>`, chalk.green(nPath), ); - code = code.replace('import.meta.url.replace(/\\.js$/, \'_bg.wasm\');', `fetch('${nPath}')`); - code = code.replace(`new URL('${oPath}', import.meta.url)`, `new URL('${nPath}', location.origin)`); + code = code.replace(/import\.meta\.url\.replace\(\/\\\\\.js\$\/, \\'_bg\.wasm\\'\);/, `fetch('${nPath}')`); + code = code.replace(`new URL('${oPath}',`, `new URL('${nPath}',`); + code = code.replace(/, import\.meta\.url\)/, `, location.origin)`); return code; } @@ -325,4 +326,4 @@ function clearRswErrorOverlay() { } window.createRswErrorOverlay = createRswErrorOverlay; -`; \ No newline at end of file +`;