Skip to content

Commit 025eebf

Browse files
authored
fix(css): dynamic import css in package fetches removed js (fixes #7955, #6823) (#7969)
1 parent 7f9f8f1 commit 025eebf

File tree

10 files changed

+41
-3
lines changed

10 files changed

+41
-3
lines changed

packages/playground/dynamic-import/__tests__/dynamic-import.spec.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isBuild, untilUpdated } from '../../testUtils'
1+
import { getColor, isBuild, untilUpdated } from '../../testUtils'
22

33
test('should load literal dynamic import', async () => {
44
await page.click('.baz')
@@ -59,3 +59,8 @@ test('should load dynamic import with css', async () => {
5959
true
6060
)
6161
})
62+
63+
test('should load dynamic import with css in package', async () => {
64+
await page.click('.pkg-css')
65+
await untilUpdated(() => getColor('.pkg-css'), 'blue', true)
66+
})

packages/playground/dynamic-import/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<button class="issue-2658-1">Issue 2658 - 1</button>
99
<button class="issue-2658-2">Issue 2658 - 2</button>
1010
<button class="css">css</button>
11+
<button class="pkg-css">pkg-css</button>
1112

1213
<div class="view"></div>
1314

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* don't include dynamic import inside this file */
2+
3+
import 'pkg'

packages/playground/dynamic-import/nested/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ document.querySelector('.css').addEventListener('click', async () => {
7070
text('.view', 'dynamic import css')
7171
})
7272

73+
document.querySelector('.pkg-css').addEventListener('click', async () => {
74+
await import('./deps')
75+
text('.view', 'dynamic import css in package')
76+
})
77+
7378
function text(el, text) {
7479
document.querySelector(el).textContent = text
7580
}

packages/playground/dynamic-import/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
"dev": "vite",
77
"build": "vite build",
88
"debug": "node --inspect-brk ../../vite/bin/vite",
9-
"preview": "vite preview"
9+
"preview": "vite preview",
10+
"postinstall": "ts-node ../../../scripts/patchFileDeps.ts"
11+
},
12+
"dependencies": {
13+
"pkg": "file:./pkg"
1014
}
1115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import('./pkg.css')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "pkg",
3+
"type": "module",
4+
"private": true,
5+
"version": "1.0.0",
6+
"main": "index.js"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.pkg-css {
2+
color: blue;
3+
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export const preloadBaseMarker = `__VITE_PRELOAD_BASE__`
2424
const preloadHelperId = 'vite/preload-helper'
2525
const preloadMarkerWithQuote = `"${preloadMarker}"` as const
2626

27+
const dynamicImportPrefixRE = /import\s*\(/
28+
2729
/**
2830
* Helper for preloading CSS and direct imports of async chunks in parallel to
2931
* the async chunk itself.
@@ -118,7 +120,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
118120
async transform(source, importer) {
119121
if (
120122
importer.includes('node_modules') &&
121-
!source.includes('import.meta.glob')
123+
!source.includes('import.meta.glob') &&
124+
!dynamicImportPrefixRE.test(source)
122125
) {
123126
return
124127
}

pnpm-lock.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)