diff --git a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts index 0c32af75219438..ee2a6e18cf04c6 100644 --- a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts +++ b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts @@ -13,6 +13,10 @@ import { import { browserExternalId } from '../plugins/resolve' import type { ExportsData } from '.' +const externalWithConversionNamespace = + 'vite:dep-pre-bundle:external-conversion' +const convertedExternalPrefix = 'vite-dep-pre-bundle-external:' + const externalTypes = [ 'css', // supported pre-processor types @@ -80,20 +84,42 @@ export function esbuildDepPlugin( name: 'vite:dep-pre-bundle', setup(build) { // externalize assets and commonly known non-js file types + // See #8459 for more details about this require-import conversion build.onResolve( { filter: new RegExp(`\\.(` + allExternalTypes.join('|') + `)(\\?.*)?$`) }, async ({ path: id, importer, kind }) => { + // if the prefix exist, it is already converted to `import`, so set `external: true` + if (id.startsWith(convertedExternalPrefix)) { + return { + path: id.slice(convertedExternalPrefix.length), + external: true + } + } + const resolved = await resolve(id, importer, kind) if (resolved) { + // here it is not set to `external: true` to convert `require` to `import` return { path: resolved, - external: true + namespace: externalWithConversionNamespace } } } ) + build.onLoad( + { filter: /./, namespace: externalWithConversionNamespace }, + (args) => { + // import itself with prefix (this is the actual part of require-import conversion) + return { + contents: + `export { default } from "${convertedExternalPrefix}${args.path}";` + + `export * from "${convertedExternalPrefix}${args.path}";`, + loader: 'js' + } + } + ) function resolveEntry(id: string) { const flatId = flattenId(id) diff --git a/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/playground/optimize-deps/__tests__/optimize-deps.spec.ts index a32274dca25e0c..929455300ba555 100644 --- a/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -77,7 +77,11 @@ test('dep with dynamic import', async () => { }) test('dep with css import', async () => { - expect(await getColor('h1')).toBe('red') + expect(await getColor('.dep-linked-include')).toBe('red') +}) + +test('CJS dep with css import', async () => { + expect(await getColor('.cjs-with-assets')).toBe('blue') }) test('dep w/ non-js files handled via plugin', async () => { diff --git a/playground/optimize-deps/dep-cjs-with-assets/foo.css b/playground/optimize-deps/dep-cjs-with-assets/foo.css new file mode 100644 index 00000000000000..8347f9fb0c358e --- /dev/null +++ b/playground/optimize-deps/dep-cjs-with-assets/foo.css @@ -0,0 +1,3 @@ +.cjs-with-assets { + color: blue; +} diff --git a/playground/optimize-deps/dep-cjs-with-assets/index.js b/playground/optimize-deps/dep-cjs-with-assets/index.js new file mode 100644 index 00000000000000..26b296af650d88 --- /dev/null +++ b/playground/optimize-deps/dep-cjs-with-assets/index.js @@ -0,0 +1,3 @@ +require('./foo.css') + +exports.a = 11 diff --git a/playground/optimize-deps/dep-cjs-with-assets/package.json b/playground/optimize-deps/dep-cjs-with-assets/package.json new file mode 100644 index 00000000000000..2e4f6e68c7510c --- /dev/null +++ b/playground/optimize-deps/dep-cjs-with-assets/package.json @@ -0,0 +1,6 @@ +{ + "name": "dep-cjs-with-assets", + "private": true, + "version": "0.0.0", + "main": "index.js" +} diff --git a/playground/optimize-deps/dep-linked-include/test.css b/playground/optimize-deps/dep-linked-include/test.css index 60f1eab97137f7..20ae3263941d1e 100644 --- a/playground/optimize-deps/dep-linked-include/test.css +++ b/playground/optimize-deps/dep-linked-include/test.css @@ -1,3 +1,3 @@ -body { +.dep-linked-include { color: red; } diff --git a/playground/optimize-deps/index.html b/playground/optimize-deps/index.html index 3cd619f9ce9236..c70ab5b2f09322 100644 --- a/playground/optimize-deps/index.html +++ b/playground/optimize-deps/index.html @@ -35,6 +35,12 @@