Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(css): dynamic import css in package fetches removed js (fixes #7955, #6823) #7969

Merged
merged 4 commits into from May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
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
@@ -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
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
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
@@ -0,0 +1 @@
import('./pkg.css')
7 changes: 7 additions & 0 deletions packages/playground/dynamic-import/pkg/package.json
@@ -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
@@ -0,0 +1,3 @@
.pkg-css {
color: blue;
}
5 changes: 4 additions & 1 deletion packages/vite/src/node/plugins/importAnalysisBuild.ts
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.