Skip to content

Commit 473424e

Browse files
hi-ogawabluwy
andauthored
fix(optimizer): workaround firefox's false warning for no sources source map (#18665)
Co-authored-by: Bjorn Lu <[email protected]>
1 parent f005461 commit 473424e

File tree

7 files changed

+48
-0
lines changed

7 files changed

+48
-0
lines changed

packages/vite/src/node/optimizer/index.ts

+19
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,25 @@ export function runOptimizeDeps(
673673
browserHash: metadata.browserHash,
674674
})
675675
}
676+
} else {
677+
// workaround Firefox warning by removing blank source map reference
678+
// https://github.com/evanw/esbuild/issues/3945
679+
const output = meta.outputs[o]
680+
// filter by exact bytes of an empty source map
681+
if (output.bytes === 93) {
682+
const jsMapPath = path.resolve(o)
683+
const jsPath = jsMapPath.slice(0, -4)
684+
if (fs.existsSync(jsPath) && fs.existsSync(jsMapPath)) {
685+
const map = JSON.parse(fs.readFileSync(jsMapPath, 'utf-8'))
686+
if (map.sources.length === 0) {
687+
const js = fs.readFileSync(jsPath, 'utf-8')
688+
fs.writeFileSync(
689+
jsPath,
690+
js.slice(0, js.lastIndexOf('//# sourceMappingURL=')),
691+
)
692+
}
693+
}
694+
}
676695
}
677696
}
678697

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const all = 'all'
2+
export { sub } from './sub.js'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@vitejs/test-dep-source-map-no-sources",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"exports": {
7+
"./*": "./*"
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const sub = 'sub'

playground/optimize-deps/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,9 @@ <h2>Pre-bundle transitive dependency 'some-package.pdf'</h2>
299299
text('.dep-with-asset-ext-prebundled', isPreBundled)
300300
text('.dep-with-asset-ext-no-dual-package', original === reexport)
301301
</script>
302+
303+
<script type="module">
304+
// manually check Firefox doesn't show warning in devtool debugger
305+
import * as sub from '@vitejs/test-dep-source-map-no-sources/sub.js'
306+
import * as all from '@vitejs/test-dep-source-map-no-sources/all.js'
307+
</script>

playground/optimize-deps/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@vitejs/test-dep-optimize-exports-with-root-glob": "file:./dep-optimize-exports-with-root-glob",
3030
"@vitejs/test-dep-optimize-with-glob": "file:./dep-optimize-with-glob",
3131
"@vitejs/test-dep-relative-to-main": "file:./dep-relative-to-main",
32+
"@vitejs/test-dep-source-map-no-sources": "file:./dep-source-map-no-sources",
3233
"@vitejs/test-dep-with-asset-ext1.pdf": "file:./dep-with-asset-ext/dep1",
3334
"@vitejs/test-dep-with-asset-ext2.pdf": "file:./dep-with-asset-ext/dep2",
3435
"@vitejs/test-dep-with-builtin-module-cjs": "file:./dep-with-builtin-module-cjs",

pnpm-lock.yaml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)