mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 22:59:10 +00:00
parent
aeb5b7436d
commit
12d0cc0e58
@ -145,11 +145,15 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
|
||||
d: dynamicIndex
|
||||
} = imports[index]
|
||||
|
||||
if (dynamicIndex > -1 && insertPreload) {
|
||||
const isDynamic = dynamicIndex > -1
|
||||
|
||||
if (isDynamic && insertPreload) {
|
||||
needPreloadHelper = true
|
||||
const original = source.slice(expStart, expEnd)
|
||||
const replacement = `${preloadMethod}(() => ${original},${isModernFlag}?"${preloadMarker}":void 0)`
|
||||
str().overwrite(expStart, expEnd, replacement, { contentOnly: true })
|
||||
str().prependLeft(expStart, `${preloadMethod}(() => `)
|
||||
str().appendRight(
|
||||
expEnd,
|
||||
`,${isModernFlag}?"${preloadMarker}":void 0)`
|
||||
)
|
||||
}
|
||||
|
||||
// Differentiate CSS imports that use the default export from those that
|
||||
@ -159,14 +163,16 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
|
||||
if (
|
||||
specifier &&
|
||||
isCSSRequest(specifier) &&
|
||||
source.slice(expStart, start).includes('from') &&
|
||||
// always inject ?used query when it is a dynamic import
|
||||
// because there is no way to check whether the default export is used
|
||||
(source.slice(expStart, start).includes('from') || isDynamic) &&
|
||||
// already has ?used query (by import.meta.glob)
|
||||
!specifier.match(/\?used(&|$)/) &&
|
||||
// edge case for package names ending with .css (e.g normalize.css)
|
||||
!(bareImportRE.test(specifier) && !specifier.includes('/'))
|
||||
) {
|
||||
const url = specifier.replace(/\?|$/, (m) => `?used${m ? '&' : ''}`)
|
||||
str().overwrite(start, end, dynamicIndex > -1 ? `'${url}'` : url, {
|
||||
str().overwrite(start, end, isDynamic ? `'${url}'` : url, {
|
||||
contentOnly: true
|
||||
})
|
||||
}
|
||||
|
@ -1,8 +1,23 @@
|
||||
import { findAssetFile, getColor, isBuild, readManifest } from '../../testUtils'
|
||||
|
||||
test('should load both stylesheets', async () => {
|
||||
test('should load all stylesheets', async () => {
|
||||
expect(await getColor('h1')).toBe('red')
|
||||
expect(await getColor('h2')).toBe('blue')
|
||||
expect(await getColor('.dynamic')).toBe('green')
|
||||
})
|
||||
|
||||
test('should load dynamic import with inline', async () => {
|
||||
const css = await page.textContent('.dynamic-inline')
|
||||
expect(css).toMatch('.inline')
|
||||
|
||||
expect(await getColor('.inline')).not.toBe('yellow')
|
||||
})
|
||||
|
||||
test('should load dynamic import with module', async () => {
|
||||
const css = await page.textContent('.dynamic-module')
|
||||
expect(css).toMatch('_mod_')
|
||||
|
||||
expect(await getColor('.mod')).toBe('yellow')
|
||||
})
|
||||
|
||||
if (isBuild) {
|
||||
@ -10,6 +25,7 @@ if (isBuild) {
|
||||
expect(findAssetFile(/style.*\.js$/)).toBe('')
|
||||
expect(findAssetFile('main.*.js$')).toMatch(`/* empty css`)
|
||||
expect(findAssetFile('other.*.js$')).toMatch(`/* empty css`)
|
||||
expect(findAssetFile(/async.*\.js$/)).toBe('')
|
||||
})
|
||||
|
||||
test('should generate correct manifest', async () => {
|
||||
|
3
playground/css-codesplit/async.css
Normal file
3
playground/css-codesplit/async.css
Normal file
@ -0,0 +1,3 @@
|
||||
.dynamic {
|
||||
color: green;
|
||||
}
|
@ -1,2 +1,11 @@
|
||||
<h1>This should be red</h1>
|
||||
<h2>This should be blue</h2>
|
||||
|
||||
<p class="dynamic">This should be green</p>
|
||||
<p class="inline">This should not be yellow</p>
|
||||
<p class="dynamic-inline"></p>
|
||||
<p class="mod">This should be yellow</p>
|
||||
<p class="dynamic-module"></p>
|
||||
|
||||
<script type="module" src="/main.js"></script>
|
||||
<div id="app"></div>
|
||||
|
3
playground/css-codesplit/inline.css
Normal file
3
playground/css-codesplit/inline.css
Normal file
@ -0,0 +1,3 @@
|
||||
.inline {
|
||||
color: yellow;
|
||||
}
|
@ -1,6 +1,15 @@
|
||||
import './style.css'
|
||||
import './main.css'
|
||||
|
||||
document.getElementById(
|
||||
'app'
|
||||
).innerHTML = `<h1>This should be red</h1><h2>This should be blue</h2>`
|
||||
import('./async.css')
|
||||
|
||||
import('./inline.css?inline').then((css) => {
|
||||
document.querySelector('.dynamic-inline').textContent = css.default
|
||||
})
|
||||
|
||||
import('./mod.module.css').then((css) => {
|
||||
document.querySelector('.dynamic-module').textContent = JSON.stringify(
|
||||
css.default
|
||||
)
|
||||
document.querySelector('.mod').classList.add(css.default.mod)
|
||||
})
|
||||
|
3
playground/css-codesplit/mod.module.css
Normal file
3
playground/css-codesplit/mod.module.css
Normal file
@ -0,0 +1,3 @@
|
||||
.mod {
|
||||
color: yellow;
|
||||
}
|
Loading…
Reference in New Issue
Block a user