Skip to content

Commit

Permalink
fix(optimizer): don't externalize transitive dep package name with as…
Browse files Browse the repository at this point in the history
…set extension (#18152)
  • Loading branch information
hi-ogawa authored Oct 7, 2024
1 parent 460aaff commit fafc7e2
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'
import type { ImportKind, Plugin } from 'esbuild'
import { KNOWN_ASSET_TYPES } from '../constants'
import { JS_TYPES_RE, KNOWN_ASSET_TYPES } from '../constants'
import type { PackageCache } from '../packages'
import {
escapeRegex,
Expand Down Expand Up @@ -157,15 +157,17 @@ export function esbuildDepPlugin(

const resolved = await resolve(id, importer, kind)
if (resolved) {
if (kind === 'require-call') {
// #16116 fix: Import the module.scss path, which is actually module.scss.js
if (resolved.endsWith('.js')) {
return {
path: resolved,
external: false,
}
// `resolved` can be javascript even when `id` matches `allExternalTypes`
// due to cjs resolution (e.g. require("./test.pdf") for "./test.pdf.js")
// or package name (e.g. import "some-package.pdf")
if (JS_TYPES_RE.test(resolved)) {
return {
path: resolved,
external: false,
}
}

if (kind === 'require-call') {
// here it is not set to `external: true` to convert `require` to `import`
return {
path: resolved,
Expand Down
9 changes: 9 additions & 0 deletions playground/optimize-deps/__tests__/optimize-deps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,12 @@ test('import the CommonJS external package that omits the js suffix', async () =
page.textContent('.external-package-tsx-js'),
).toBe('tsx')
})

test('external package name with asset extension', async () => {
await expectWithRetry(() =>
page.textContent('.dep-with-asset-ext-no-dual-package'),
).toBe('true')
await expectWithRetry(() =>
page.textContent('.dep-with-asset-ext-prebundled'),
).toBe(String(isServe))
})
3 changes: 3 additions & 0 deletions playground/optimize-deps/dep-with-asset-ext/dep1/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default { random: Math.random() }

export const isPreBundled = import.meta.url.includes('/.vite/deps/')
6 changes: 6 additions & 0 deletions playground/optimize-deps/dep-with-asset-ext/dep1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@vitejs/test-dep-with-asset-ext1.pdf",
"private": true,
"type": "module",
"exports": "./index.mjs"
}
1 change: 1 addition & 0 deletions playground/optimize-deps/dep-with-asset-ext/dep2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@vitejs/test-dep-with-asset-ext1.pdf'
9 changes: 9 additions & 0 deletions playground/optimize-deps/dep-with-asset-ext/dep2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@vitejs/test-dep-with-asset-ext2.pdf",
"private": true,
"type": "module",
"exports": "./index.js",
"dependencies": {
"@vitejs/test-dep-with-asset-ext1.pdf": "file:../dep1"
}
}
12 changes: 12 additions & 0 deletions playground/optimize-deps/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,15 @@ <h2>Import the CommonJS external package that omits the js suffix</h2>
import loadSub from '@vitejs/test-dep-incompatible'
loadSub() // should show an error that tells there's an incompatible dep
</script>

<h2>Pre-bundle transitive dependency 'some-package.pdf'</h2>
<div>prebundled: <span class="dep-with-asset-ext-prebundled">???</span></div>
<div>
no dual package: <span class="dep-with-asset-ext-no-dual-package">???</span>
</div>
<script type="module">
import original, { isPreBundled } from '@vitejs/test-dep-with-asset-ext1.pdf'
import reexport from '@vitejs/test-dep-with-asset-ext2.pdf'
text('.dep-with-asset-ext-prebundled', isPreBundled)
text('.dep-with-asset-ext-no-dual-package', original === reexport)
</script>
2 changes: 2 additions & 0 deletions playground/optimize-deps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"@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-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",
"@vitejs/test-dep-with-builtin-module-esm": "file:./dep-with-builtin-module-esm",
"@vitejs/test-dep-with-dynamic-import": "file:./dep-with-dynamic-import",
Expand Down
26 changes: 26 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 fafc7e2

Please sign in to comment.