diff --git a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts index e3bb4730eef1d7..b3069f43ae4b9e 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 { CSS_LANGS_RE, KNOWN_ASSET_TYPES } from '../constants' +import { KNOWN_ASSET_TYPES } from '../constants' import { getDepOptimizationConfig } from '..' import type { PackageCache, ResolvedConfig } from '..' import { @@ -12,6 +12,7 @@ import { normalizePath, } from '../utils' import { browserExternalId, optionalPeerDepId } from '../plugins/resolve' +import { isCSSRequest, isModuleCSSRequest } from '../plugins/css' const externalWithConversionNamespace = 'vite:dep-pre-bundle:external-conversion' @@ -172,10 +173,11 @@ export function esbuildDepPlugin( // import itself with prefix (this is the actual part of require-import conversion) const modulePath = `"${convertedExternalPrefix}${args.path}"` return { - contents: CSS_LANGS_RE.test(args.path) - ? `import ${modulePath};` - : `export { default } from ${modulePath};` + - `export * from ${modulePath};`, + contents: + isCSSRequest(args.path) && !isModuleCSSRequest(args.path) + ? `import ${modulePath};` + : `export { default } from ${modulePath};` + + `export * from ${modulePath};`, loader: 'js', } }, diff --git a/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/playground/optimize-deps/__tests__/optimize-deps.spec.ts index 2f312882a68e5d..1e54aa1bd700b5 100644 --- a/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -215,6 +215,7 @@ test('pre bundle css require', async () => { } expect(await getColor('.css-require')).toBe('red') + expect(await getColor('.css-module-require')).toBe('red') }) test.runIf(isBuild)('no missing deps during build', async () => { diff --git a/playground/optimize-deps/dep-css-require/mod.cjs b/playground/optimize-deps/dep-css-require/mod.cjs new file mode 100644 index 00000000000000..b2428066205643 --- /dev/null +++ b/playground/optimize-deps/dep-css-require/mod.cjs @@ -0,0 +1,2 @@ +const style = require('./mod.module.css') +module.exports = style diff --git a/playground/optimize-deps/dep-css-require/mod.module.css b/playground/optimize-deps/dep-css-require/mod.module.css new file mode 100644 index 00000000000000..d27a321f0a48a8 --- /dev/null +++ b/playground/optimize-deps/dep-css-require/mod.module.css @@ -0,0 +1,3 @@ +.cssModuleRequire { + color: red; +} diff --git a/playground/optimize-deps/index.html b/playground/optimize-deps/index.html index a07e6a17798154..6cf34f1f4c3469 100644 --- a/playground/optimize-deps/index.html +++ b/playground/optimize-deps/index.html @@ -99,6 +99,9 @@