From 0fbba94d279962e6047a6e6687e71401b495a1f5 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 14 Nov 2024 09:15:41 +0900 Subject: [PATCH 1/3] fix(optimizer): workaround firefox source map false warning --- packages/vite/src/node/optimizer/index.ts | 22 +++++++++++++++++++ .../dep-source-map-no-sources/all.js | 2 ++ .../dep-source-map-no-sources/package.json | 9 ++++++++ .../dep-source-map-no-sources/sub.js | 1 + playground/optimize-deps/index.html | 6 +++++ playground/optimize-deps/package.json | 1 + pnpm-lock.yaml | 10 +++++++++ 7 files changed, 51 insertions(+) create mode 100644 playground/optimize-deps/dep-source-map-no-sources/all.js create mode 100644 playground/optimize-deps/dep-source-map-no-sources/package.json create mode 100644 playground/optimize-deps/dep-source-map-no-sources/sub.js diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index b859319c0a81e3..59ee9375de855c 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -673,6 +673,28 @@ export function runOptimizeDeps( browserHash: metadata.browserHash, }) } + } else { + // workaround Firefox warning by removing blank source map reference + // https://github.com/evanw/esbuild/issues/3945 + const output = meta.outputs[o] + // filter by exact bytes + if (output.bytes === 93) { + const jsMapPath = path.resolve( + processingCacheDirOutputPath, + path.relative(processingCacheDirOutputPath, o), + ) + const jsPath = jsMapPath.slice(0, -4) + if (fs.existsSync(jsPath) && fs.existsSync(jsMapPath)) { + const map = JSON.parse(fs.readFileSync(jsMapPath, 'utf-8')) + if (map.sources.length === 0) { + const js = fs.readFileSync(jsPath, 'utf-8') + fs.writeFileSync( + jsPath, + js.slice(0, js.lastIndexOf('//# sourceMappingURL=')), + ) + } + } + } } } diff --git a/playground/optimize-deps/dep-source-map-no-sources/all.js b/playground/optimize-deps/dep-source-map-no-sources/all.js new file mode 100644 index 00000000000000..33b7c388d5f003 --- /dev/null +++ b/playground/optimize-deps/dep-source-map-no-sources/all.js @@ -0,0 +1,2 @@ +export const all = 'all' +export { sub } from './sub.js' diff --git a/playground/optimize-deps/dep-source-map-no-sources/package.json b/playground/optimize-deps/dep-source-map-no-sources/package.json new file mode 100644 index 00000000000000..cd0db894af0ceb --- /dev/null +++ b/playground/optimize-deps/dep-source-map-no-sources/package.json @@ -0,0 +1,9 @@ +{ + "name": "@vitejs/test-dep-source-map-no-sources", + "private": true, + "version": "0.0.0", + "type": "module", + "exports": { + "./*": "./*" + } +} diff --git a/playground/optimize-deps/dep-source-map-no-sources/sub.js b/playground/optimize-deps/dep-source-map-no-sources/sub.js new file mode 100644 index 00000000000000..125abf53cf2920 --- /dev/null +++ b/playground/optimize-deps/dep-source-map-no-sources/sub.js @@ -0,0 +1 @@ +export const sub = 'sub' diff --git a/playground/optimize-deps/index.html b/playground/optimize-deps/index.html index 10ca2dc0eac439..02a869c4e5a8a7 100644 --- a/playground/optimize-deps/index.html +++ b/playground/optimize-deps/index.html @@ -299,3 +299,9 @@

Pre-bundle transitive dependency 'some-package.pdf'

text('.dep-with-asset-ext-prebundled', isPreBundled) text('.dep-with-asset-ext-no-dual-package', original === reexport) + + diff --git a/playground/optimize-deps/package.json b/playground/optimize-deps/package.json index a1e4c17a16f027..35383a562db30b 100644 --- a/playground/optimize-deps/package.json +++ b/playground/optimize-deps/package.json @@ -29,6 +29,7 @@ "@vitejs/test-dep-optimize-exports-with-root-glob": "file:./dep-optimize-exports-with-root-glob", "@vitejs/test-dep-optimize-with-glob": "file:./dep-optimize-with-glob", "@vitejs/test-dep-relative-to-main": "file:./dep-relative-to-main", + "@vitejs/test-dep-source-map-no-sources": "file:./dep-source-map-no-sources", "@vitejs/test-dep-with-asset-ext1.pdf": "file:./dep-with-asset-ext/dep1", "@vitejs/test-dep-with-asset-ext2.pdf": "file:./dep-with-asset-ext/dep2", "@vitejs/test-dep-with-builtin-module-cjs": "file:./dep-with-builtin-module-cjs", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 71dd2452846c51..b66c87dc9c34d0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -960,6 +960,9 @@ importers: '@vitejs/test-dep-relative-to-main': specifier: file:./dep-relative-to-main version: file:playground/optimize-deps/dep-relative-to-main + '@vitejs/test-dep-source-map-no-sources': + specifier: file:./dep-source-map-no-sources + version: file:playground/optimize-deps/dep-source-map-no-sources '@vitejs/test-dep-with-asset-ext1.pdf': specifier: file:./dep-with-asset-ext/dep1 version: file:playground/optimize-deps/dep-with-asset-ext/dep1 @@ -1085,6 +1088,8 @@ importers: playground/optimize-deps/dep-relative-to-main: {} + playground/optimize-deps/dep-source-map-no-sources: {} + playground/optimize-deps/dep-with-asset-ext/dep1: {} playground/optimize-deps/dep-with-asset-ext/dep2: @@ -3293,6 +3298,9 @@ packages: '@vitejs/test-dep-self-reference-url-worker@file:playground/worker/dep-self-reference-url-worker': resolution: {directory: playground/worker/dep-self-reference-url-worker, type: directory} + '@vitejs/test-dep-source-map-no-sources@file:playground/optimize-deps/dep-source-map-no-sources': + resolution: {directory: playground/optimize-deps/dep-source-map-no-sources, type: directory} + '@vitejs/test-dep-that-imports@file:playground/external/dep-that-imports': resolution: {directory: playground/external/dep-that-imports, type: directory} @@ -8851,6 +8859,8 @@ snapshots: '@vitejs/test-dep-self-reference-url-worker@file:playground/worker/dep-self-reference-url-worker': {} + '@vitejs/test-dep-source-map-no-sources@file:playground/optimize-deps/dep-source-map-no-sources': {} + '@vitejs/test-dep-that-imports@file:playground/external/dep-that-imports(typescript@5.6.3)': dependencies: slash3: slash@3.0.0 From 878b56f66cee89d952108effbc934eeb755939dc Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 15 Nov 2024 08:31:16 +0900 Subject: [PATCH 2/3] chore: comment Co-authored-by: Bjorn Lu --- packages/vite/src/node/optimizer/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 59ee9375de855c..450caecfeeaa8b 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -677,7 +677,7 @@ export function runOptimizeDeps( // workaround Firefox warning by removing blank source map reference // https://github.com/evanw/esbuild/issues/3945 const output = meta.outputs[o] - // filter by exact bytes + // filter by exact bytes of an empty source map if (output.bytes === 93) { const jsMapPath = path.resolve( processingCacheDirOutputPath, From 8d3d768c16c62aad0fec18b8d9ac3d89bba831d0 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 15 Nov 2024 08:31:41 +0900 Subject: [PATCH 3/3] refactor: path resolve Co-authored-by: Bjorn Lu --- packages/vite/src/node/optimizer/index.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 450caecfeeaa8b..41b9d1a80897e0 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -679,10 +679,7 @@ export function runOptimizeDeps( const output = meta.outputs[o] // filter by exact bytes of an empty source map if (output.bytes === 93) { - const jsMapPath = path.resolve( - processingCacheDirOutputPath, - path.relative(processingCacheDirOutputPath, o), - ) + const jsMapPath = path.resolve(o) const jsPath = jsMapPath.slice(0, -4) if (fs.existsSync(jsPath) && fs.existsSync(jsMapPath)) { const map = JSON.parse(fs.readFileSync(jsMapPath, 'utf-8'))