mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 22:59:10 +00:00
fix(optimizer): include exports for css modules (#13519)
This commit is contained in:
parent
19993119c1
commit
1fd9919c9a
@ -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',
|
||||
}
|
||||
},
|
||||
|
@ -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 () => {
|
||||
|
2
playground/optimize-deps/dep-css-require/mod.cjs
Normal file
2
playground/optimize-deps/dep-css-require/mod.cjs
Normal file
@ -0,0 +1,2 @@
|
||||
const style = require('./mod.module.css')
|
||||
module.exports = style
|
3
playground/optimize-deps/dep-css-require/mod.module.css
Normal file
3
playground/optimize-deps/dep-css-require/mod.module.css
Normal file
@ -0,0 +1,3 @@
|
||||
.cssModuleRequire {
|
||||
color: red;
|
||||
}
|
@ -99,6 +99,9 @@
|
||||
<h2>Pre bundle css require</h2>
|
||||
<div class="css-require">css require</div>
|
||||
|
||||
<h2>Pre bundle css modules require</h2>
|
||||
<div class="css-module-require">This should be red</div>
|
||||
|
||||
<script>
|
||||
function text(el, text) {
|
||||
document.querySelector(el).textContent = text
|
||||
@ -127,6 +130,10 @@
|
||||
|
||||
import '@vitejs/test-dep-cjs-with-assets'
|
||||
import '@vitejs/test-dep-css-require'
|
||||
import cssModuleRequire from '@vitejs/test-dep-css-require/mod.cjs'
|
||||
document
|
||||
.querySelector('.css-module-require')
|
||||
.classList.add(cssModuleRequire.cssModuleRequire)
|
||||
|
||||
import { env } from '@vitejs/test-dep-node-env'
|
||||
text('.node-env', env)
|
||||
|
Loading…
Reference in New Issue
Block a user