Skip to content

Commit

Permalink
fix(css): dynamic import css in package fetches removed js (fixes #7955
Browse files Browse the repository at this point in the history
…, #6823) (#7969)
  • Loading branch information
sapphi-red authored May 1, 2022
1 parent 7f9f8f1 commit 025eebf
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isBuild, untilUpdated } from '../../testUtils'
import { getColor, isBuild, untilUpdated } from '../../testUtils'

test('should load literal dynamic import', async () => {
await page.click('.baz')
Expand Down Expand Up @@ -59,3 +59,8 @@ test('should load dynamic import with css', async () => {
true
)
})

test('should load dynamic import with css in package', async () => {
await page.click('.pkg-css')
await untilUpdated(() => getColor('.pkg-css'), 'blue', true)
})
1 change: 1 addition & 0 deletions packages/playground/dynamic-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<button class="issue-2658-1">Issue 2658 - 1</button>
<button class="issue-2658-2">Issue 2658 - 2</button>
<button class="css">css</button>
<button class="pkg-css">pkg-css</button>

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

Expand Down
3 changes: 3 additions & 0 deletions packages/playground/dynamic-import/nested/deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* don't include dynamic import inside this file */

import 'pkg'
5 changes: 5 additions & 0 deletions packages/playground/dynamic-import/nested/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ document.querySelector('.css').addEventListener('click', async () => {
text('.view', 'dynamic import css')
})

document.querySelector('.pkg-css').addEventListener('click', async () => {
await import('./deps')
text('.view', 'dynamic import css in package')
})

function text(el, text) {
document.querySelector(el).textContent = text
}
6 changes: 5 additions & 1 deletion packages/playground/dynamic-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"preview": "vite preview"
"preview": "vite preview",
"postinstall": "ts-node ../../../scripts/patchFileDeps.ts"
},
"dependencies": {
"pkg": "file:./pkg"
}
}
1 change: 1 addition & 0 deletions packages/playground/dynamic-import/pkg/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import('./pkg.css')
7 changes: 7 additions & 0 deletions packages/playground/dynamic-import/pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "pkg",
"type": "module",
"private": true,
"version": "1.0.0",
"main": "index.js"
}
3 changes: 3 additions & 0 deletions packages/playground/dynamic-import/pkg/pkg.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.pkg-css {
color: blue;
}
5 changes: 4 additions & 1 deletion packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export const preloadBaseMarker = `__VITE_PRELOAD_BASE__`
const preloadHelperId = 'vite/preload-helper'
const preloadMarkerWithQuote = `"${preloadMarker}"` as const

const dynamicImportPrefixRE = /import\s*\(/

/**
* Helper for preloading CSS and direct imports of async chunks in parallel to
* the async chunk itself.
Expand Down Expand Up @@ -118,7 +120,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
async transform(source, importer) {
if (
importer.includes('node_modules') &&
!source.includes('import.meta.glob')
!source.includes('import.meta.glob') &&
!dynamicImportPrefixRE.test(source)
) {
return
}
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 025eebf

Please sign in to comment.