Skip to content

Commit

Permalink
fix(optimizer): workaround firefox's false warning for no sources sou…
Browse files Browse the repository at this point in the history
…rce map (#18665)

Co-authored-by: Bjorn Lu <[email protected]>
  • Loading branch information
hi-ogawa and bluwy authored Nov 19, 2024
1 parent f005461 commit 473424e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,25 @@ 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 of an empty source map
if (output.bytes === 93) {
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'))
if (map.sources.length === 0) {
const js = fs.readFileSync(jsPath, 'utf-8')
fs.writeFileSync(
jsPath,
js.slice(0, js.lastIndexOf('//# sourceMappingURL=')),
)
}
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions playground/optimize-deps/dep-source-map-no-sources/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const all = 'all'
export { sub } from './sub.js'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@vitejs/test-dep-source-map-no-sources",
"private": true,
"version": "0.0.0",
"type": "module",
"exports": {
"./*": "./*"
}
}
1 change: 1 addition & 0 deletions playground/optimize-deps/dep-source-map-no-sources/sub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const sub = 'sub'
6 changes: 6 additions & 0 deletions playground/optimize-deps/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,9 @@ <h2>Pre-bundle transitive dependency 'some-package.pdf'</h2>
text('.dep-with-asset-ext-prebundled', isPreBundled)
text('.dep-with-asset-ext-no-dual-package', original === reexport)
</script>

<script type="module">
// manually check Firefox doesn't show warning in devtool debugger
import * as sub from '@vitejs/test-dep-source-map-no-sources/sub.js'
import * as all from '@vitejs/test-dep-source-map-no-sources/all.js'
</script>
1 change: 1 addition & 0 deletions playground/optimize-deps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 473424e

Please sign in to comment.