diff --git a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts index d66e918432f02f..088cb35d770bab 100644 --- a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts +++ b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts @@ -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, @@ -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, diff --git a/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/playground/optimize-deps/__tests__/optimize-deps.spec.ts index 7f0d7f0e6a0ee6..83df0bebf4de08 100644 --- a/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -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)) +}) diff --git a/playground/optimize-deps/dep-with-asset-ext/dep1/index.mjs b/playground/optimize-deps/dep-with-asset-ext/dep1/index.mjs new file mode 100644 index 00000000000000..0c18cd78e5a5ac --- /dev/null +++ b/playground/optimize-deps/dep-with-asset-ext/dep1/index.mjs @@ -0,0 +1,3 @@ +export default { random: Math.random() } + +export const isPreBundled = import.meta.url.includes('/.vite/deps/') diff --git a/playground/optimize-deps/dep-with-asset-ext/dep1/package.json b/playground/optimize-deps/dep-with-asset-ext/dep1/package.json new file mode 100644 index 00000000000000..cec7eb4442bec8 --- /dev/null +++ b/playground/optimize-deps/dep-with-asset-ext/dep1/package.json @@ -0,0 +1,6 @@ +{ + "name": "@vitejs/test-dep-with-asset-ext1.pdf", + "private": true, + "type": "module", + "exports": "./index.mjs" +} diff --git a/playground/optimize-deps/dep-with-asset-ext/dep2/index.js b/playground/optimize-deps/dep-with-asset-ext/dep2/index.js new file mode 100644 index 00000000000000..51eb63870348db --- /dev/null +++ b/playground/optimize-deps/dep-with-asset-ext/dep2/index.js @@ -0,0 +1 @@ +export { default } from '@vitejs/test-dep-with-asset-ext1.pdf' diff --git a/playground/optimize-deps/dep-with-asset-ext/dep2/package.json b/playground/optimize-deps/dep-with-asset-ext/dep2/package.json new file mode 100644 index 00000000000000..2134bc6de3eabb --- /dev/null +++ b/playground/optimize-deps/dep-with-asset-ext/dep2/package.json @@ -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" + } +} diff --git a/playground/optimize-deps/index.html b/playground/optimize-deps/index.html index f79f2bf5b6e921..10ca2dc0eac439 100644 --- a/playground/optimize-deps/index.html +++ b/playground/optimize-deps/index.html @@ -287,3 +287,15 @@