Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading