fix(optimizer): workaround firefox's false warning for no sources source map (#18665)

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
This commit is contained in:
Hiroshi Ogawa 2024-11-19 13:24:49 +09:00 committed by GitHub
parent f005461ecc
commit 473424ee8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 48 additions and 0 deletions

View File

@ -673,6 +673,25 @@ export function runOptimizeDeps(
browserHash: metadata.browserHash, 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=')),
)
}
}
}
} }
} }

View File

@ -0,0 +1,2 @@
export const all = 'all'
export { sub } from './sub.js'

View File

@ -0,0 +1,9 @@
{
"name": "@vitejs/test-dep-source-map-no-sources",
"private": true,
"version": "0.0.0",
"type": "module",
"exports": {
"./*": "./*"
}
}

View File

@ -0,0 +1 @@
export const sub = 'sub'

View File

@ -299,3 +299,9 @@
text('.dep-with-asset-ext-prebundled', isPreBundled) text('.dep-with-asset-ext-prebundled', isPreBundled)
text('.dep-with-asset-ext-no-dual-package', original === reexport) text('.dep-with-asset-ext-no-dual-package', original === reexport)
</script> </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>

View File

@ -29,6 +29,7 @@
"@vitejs/test-dep-optimize-exports-with-root-glob": "file:./dep-optimize-exports-with-root-glob", "@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-optimize-with-glob": "file:./dep-optimize-with-glob",
"@vitejs/test-dep-relative-to-main": "file:./dep-relative-to-main", "@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-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-asset-ext2.pdf": "file:./dep-with-asset-ext/dep2",
"@vitejs/test-dep-with-builtin-module-cjs": "file:./dep-with-builtin-module-cjs", "@vitejs/test-dep-with-builtin-module-cjs": "file:./dep-with-builtin-module-cjs",

View File

@ -967,6 +967,9 @@ importers:
'@vitejs/test-dep-relative-to-main': '@vitejs/test-dep-relative-to-main':
specifier: file:./dep-relative-to-main specifier: file:./dep-relative-to-main
version: file:playground/optimize-deps/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': '@vitejs/test-dep-with-asset-ext1.pdf':
specifier: file:./dep-with-asset-ext/dep1 specifier: file:./dep-with-asset-ext/dep1
version: file:playground/optimize-deps/dep-with-asset-ext/dep1 version: file:playground/optimize-deps/dep-with-asset-ext/dep1
@ -1092,6 +1095,8 @@ importers:
playground/optimize-deps/dep-relative-to-main: {} 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/dep1: {}
playground/optimize-deps/dep-with-asset-ext/dep2: playground/optimize-deps/dep-with-asset-ext/dep2:
@ -3344,6 +3349,9 @@ packages:
'@vitejs/test-dep-self-reference-url-worker@file:playground/worker/dep-self-reference-url-worker': '@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} 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': '@vitejs/test-dep-that-imports@file:playground/external/dep-that-imports':
resolution: {directory: playground/external/dep-that-imports, type: directory} resolution: {directory: playground/external/dep-that-imports, type: directory}
@ -9005,6 +9013,8 @@ snapshots:
'@vitejs/test-dep-self-reference-url-worker@file:playground/worker/dep-self-reference-url-worker': {} '@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)': '@vitejs/test-dep-that-imports@file:playground/external/dep-that-imports(typescript@5.6.3)':
dependencies: dependencies:
slash3: slash@3.0.0 slash3: slash@3.0.0