From 4c4f098c027f0055a0d44684d4cb8494a3ddb696 Mon Sep 17 00:00:00 2001 From: Karim Abou Zeid Date: Tue, 13 Apr 2021 17:29:28 +0200 Subject: [PATCH 1/4] Fix support for `config.base`. Fix `import.meta.url` being expanded. Fix use command instead of mode. --- src/index.ts | 4 ++-- src/utils.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index e6554c1..ce27a41 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,7 +51,7 @@ 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; } diff --git a/src/utils.ts b/src/utils.ts index 7408c92..2e78167 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -95,7 +95,7 @@ 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(`new URL('${oPath}', import\.meta\.url)`, `new URL('${nPath}', location.origin)`); return code; } @@ -325,4 +325,4 @@ function clearRswErrorOverlay() { } window.createRswErrorOverlay = createRswErrorOverlay; -`; \ No newline at end of file +`; From b8fdb745d86c90d626bcc90d92f4f68d04691cf2 Mon Sep 17 00:00:00 2001 From: Karim Abou Zeid Date: Tue, 13 Apr 2021 18:01:39 +0200 Subject: [PATCH 2/4] Revert import.meta.url --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 2e78167..9631dbb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -95,7 +95,7 @@ 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(`new URL('${oPath}', import.meta.url)`, `new URL('${nPath}', location.origin)`); return code; } From 91eee51a132fd9bc178e8bb1dc8897d040687f05 Mon Sep 17 00:00:00 2001 From: Karim Abou Zeid Date: Tue, 13 Apr 2021 20:34:04 +0200 Subject: [PATCH 3/4] Fix import.meta.url --- package.json | 2 +- src/index.ts | 2 +- src/utils.ts | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 9db0906..5e43dfa 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@typescript-eslint/eslint-plugin": "^4.15.1", "@typescript-eslint/parser": "^4.15.1", "eslint": "^7.20.0", - "tsup": "^4.0.0", + "tsup": "^4.8.21", "typescript": "^4.1.3", "vite": "^2.1.0" } diff --git a/src/index.ts b/src/index.ts index ce27a41..57c3d7b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -56,7 +56,7 @@ export function ViteRsw(userOptions: RswPluginOptions): Plugin { } // 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 9631dbb..ac05b71 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; } From bb929366a9e29ded27c21ca21c476e6b26322dd1 Mon Sep 17 00:00:00 2001 From: Karim Abou Zeid Date: Tue, 13 Apr 2021 20:39:23 +0200 Subject: [PATCH 4/4] Final fix --- package.json | 2 +- src/index.ts | 2 +- src/utils.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 5e43dfa..9db0906 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@typescript-eslint/eslint-plugin": "^4.15.1", "@typescript-eslint/parser": "^4.15.1", "eslint": "^7.20.0", - "tsup": "^4.8.21", + "tsup": "^4.0.0", "typescript": "^4.1.3", "vite": "^2.1.0" } diff --git a/src/index.ts b/src/index.ts index 57c3d7b..5fe0087 100644 --- a/src/index.ts +++ b/src/index.ts @@ -56,7 +56,7 @@ export function ViteRsw(userOptions: RswPluginOptions): Plugin { } // 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 ac05b71..ddceffa 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -94,7 +94,7 @@ 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(/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;