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 @@

Import the CommonJS external package that omits the js suffix

import loadSub from '@vitejs/test-dep-incompatible' loadSub() // should show an error that tells there's an incompatible dep + +

Pre-bundle transitive dependency 'some-package.pdf'

+
prebundled: ???
+
+ no dual package: ??? +
+ diff --git a/playground/optimize-deps/package.json b/playground/optimize-deps/package.json index b51f1a97d5f81e..7dfb8b7b8b9c7a 100644 --- a/playground/optimize-deps/package.json +++ b/playground/optimize-deps/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6e2b94a66bb60f..6b3c6cf220a5e3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -962,6 +962,12 @@ importers: '@vitejs/test-dep-relative-to-main': specifier: file:./dep-relative-to-main version: file:playground/optimize-deps/dep-relative-to-main + '@vitejs/test-dep-with-asset-ext1.pdf': + specifier: file:./dep-with-asset-ext/dep1 + version: file:playground/optimize-deps/dep-with-asset-ext/dep1 + '@vitejs/test-dep-with-asset-ext2.pdf': + specifier: file:./dep-with-asset-ext/dep2 + version: file:playground/optimize-deps/dep-with-asset-ext/dep2 '@vitejs/test-dep-with-builtin-module-cjs': specifier: file:./dep-with-builtin-module-cjs version: file:playground/optimize-deps/dep-with-builtin-module-cjs @@ -1081,6 +1087,14 @@ importers: playground/optimize-deps/dep-relative-to-main: {} + playground/optimize-deps/dep-with-asset-ext/dep1: {} + + playground/optimize-deps/dep-with-asset-ext/dep2: + dependencies: + '@vitejs/test-dep-with-asset-ext1.pdf': + specifier: file:../dep1 + version: file:playground/optimize-deps/dep-with-asset-ext/dep1 + playground/optimize-deps/dep-with-builtin-module-cjs: {} playground/optimize-deps/dep-with-builtin-module-esm: {} @@ -3636,6 +3650,12 @@ packages: '@vitejs/test-dep-to-optimize@file:playground/worker/dep-to-optimize': resolution: {directory: playground/worker/dep-to-optimize, type: directory} + '@vitejs/test-dep-with-asset-ext1.pdf@file:playground/optimize-deps/dep-with-asset-ext/dep1': + resolution: {directory: playground/optimize-deps/dep-with-asset-ext/dep1, type: directory} + + '@vitejs/test-dep-with-asset-ext2.pdf@file:playground/optimize-deps/dep-with-asset-ext/dep2': + resolution: {directory: playground/optimize-deps/dep-with-asset-ext/dep2, type: directory} + '@vitejs/test-dep-with-builtin-module-cjs@file:playground/optimize-deps/dep-with-builtin-module-cjs': resolution: {directory: playground/optimize-deps/dep-with-builtin-module-cjs, type: directory} @@ -9241,6 +9261,12 @@ snapshots: '@vitejs/test-dep-to-optimize@file:playground/worker/dep-to-optimize': {} + '@vitejs/test-dep-with-asset-ext1.pdf@file:playground/optimize-deps/dep-with-asset-ext/dep1': {} + + '@vitejs/test-dep-with-asset-ext2.pdf@file:playground/optimize-deps/dep-with-asset-ext/dep2': + dependencies: + '@vitejs/test-dep-with-asset-ext1.pdf': file:playground/optimize-deps/dep-with-asset-ext/dep1 + '@vitejs/test-dep-with-builtin-module-cjs@file:playground/optimize-deps/dep-with-builtin-module-cjs': {} '@vitejs/test-dep-with-builtin-module-esm@file:playground/optimize-deps/dep-with-builtin-module-esm': {}