Skip to content

Commit 12d0cc0

Browse files
authoredMay 9, 2022
fix(css): preserve dynamic import css code (fix #5348) (#7746)
1 parent aeb5b74 commit 12d0cc0

File tree

7 files changed

+59
-10
lines changed

7 files changed

+59
-10
lines changed
 

‎packages/vite/src/node/plugins/importAnalysisBuild.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,15 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
145145
d: dynamicIndex
146146
} = imports[index]
147147

148-
if (dynamicIndex > -1 && insertPreload) {
148+
const isDynamic = dynamicIndex > -1
149+
150+
if (isDynamic && insertPreload) {
149151
needPreloadHelper = true
150-
const original = source.slice(expStart, expEnd)
151-
const replacement = `${preloadMethod}(() => ${original},${isModernFlag}?"${preloadMarker}":void 0)`
152-
str().overwrite(expStart, expEnd, replacement, { contentOnly: true })
152+
str().prependLeft(expStart, `${preloadMethod}(() => `)
153+
str().appendRight(
154+
expEnd,
155+
`,${isModernFlag}?"${preloadMarker}":void 0)`
156+
)
153157
}
154158

155159
// Differentiate CSS imports that use the default export from those that
@@ -159,14 +163,16 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
159163
if (
160164
specifier &&
161165
isCSSRequest(specifier) &&
162-
source.slice(expStart, start).includes('from') &&
166+
// always inject ?used query when it is a dynamic import
167+
// because there is no way to check whether the default export is used
168+
(source.slice(expStart, start).includes('from') || isDynamic) &&
163169
// already has ?used query (by import.meta.glob)
164170
!specifier.match(/\?used(&|$)/) &&
165171
// edge case for package names ending with .css (e.g normalize.css)
166172
!(bareImportRE.test(specifier) && !specifier.includes('/'))
167173
) {
168174
const url = specifier.replace(/\?|$/, (m) => `?used${m ? '&' : ''}`)
169-
str().overwrite(start, end, dynamicIndex > -1 ? `'${url}'` : url, {
175+
str().overwrite(start, end, isDynamic ? `'${url}'` : url, {
170176
contentOnly: true
171177
})
172178
}

‎playground/css-codesplit/__tests__/css-codesplit.spec.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
import { findAssetFile, getColor, isBuild, readManifest } from '../../testUtils'
22

3-
test('should load both stylesheets', async () => {
3+
test('should load all stylesheets', async () => {
44
expect(await getColor('h1')).toBe('red')
55
expect(await getColor('h2')).toBe('blue')
6+
expect(await getColor('.dynamic')).toBe('green')
7+
})
8+
9+
test('should load dynamic import with inline', async () => {
10+
const css = await page.textContent('.dynamic-inline')
11+
expect(css).toMatch('.inline')
12+
13+
expect(await getColor('.inline')).not.toBe('yellow')
14+
})
15+
16+
test('should load dynamic import with module', async () => {
17+
const css = await page.textContent('.dynamic-module')
18+
expect(css).toMatch('_mod_')
19+
20+
expect(await getColor('.mod')).toBe('yellow')
621
})
722

823
if (isBuild) {
924
test('should remove empty chunk', async () => {
1025
expect(findAssetFile(/style.*\.js$/)).toBe('')
1126
expect(findAssetFile('main.*.js$')).toMatch(`/* empty css`)
1227
expect(findAssetFile('other.*.js$')).toMatch(`/* empty css`)
28+
expect(findAssetFile(/async.*\.js$/)).toBe('')
1329
})
1430

1531
test('should generate correct manifest', async () => {

‎playground/css-codesplit/async.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.dynamic {
2+
color: green;
3+
}

‎playground/css-codesplit/index.html

+9
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1+
<h1>This should be red</h1>
2+
<h2>This should be blue</h2>
3+
4+
<p class="dynamic">This should be green</p>
5+
<p class="inline">This should not be yellow</p>
6+
<p class="dynamic-inline"></p>
7+
<p class="mod">This should be yellow</p>
8+
<p class="dynamic-module"></p>
9+
110
<script type="module" src="/main.js"></script>
211
<div id="app"></div>

‎playground/css-codesplit/inline.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.inline {
2+
color: yellow;
3+
}

‎playground/css-codesplit/main.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import './style.css'
22
import './main.css'
33

4-
document.getElementById(
5-
'app'
6-
).innerHTML = `<h1>This should be red</h1><h2>This should be blue</h2>`
4+
import('./async.css')
5+
6+
import('./inline.css?inline').then((css) => {
7+
document.querySelector('.dynamic-inline').textContent = css.default
8+
})
9+
10+
import('./mod.module.css').then((css) => {
11+
document.querySelector('.dynamic-module').textContent = JSON.stringify(
12+
css.default
13+
)
14+
document.querySelector('.mod').classList.add(css.default.mod)
15+
})
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.mod {
2+
color: yellow;
3+
}

0 commit comments

Comments
 (0)
Please sign in to comment.