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 abnormal after build #3333

Merged
merged 9 commits into from Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
Expand Up @@ -45,3 +45,13 @@ test('should load dynamic import with vars', async () => {
await page.click('.bar')
await untilUpdated(() => page.textContent('.view'), 'Bar view', true)
})

// dynamic import css
test('should load dynamic import with css', async () => {
await page.click('.css')
await untilUpdated(
() => page.$eval('.css', (node) => window.getComputedStyle(node).boxSizing),
'border-box',
true
)
})
2 changes: 2 additions & 0 deletions packages/playground/dynamic-import/css/index.css
@@ -0,0 +1,2 @@
.css { box-sizing: border-box; }
.view { color: red; }
1 change: 1 addition & 0 deletions packages/playground/dynamic-import/index.html
Expand Up @@ -7,6 +7,7 @@
<button class="mxdjson">Mxdjson</button>
<button class="issue-2658-1">Issue 2658 - 1</button>
<button class="issue-2658-2">Issue 2658 - 2</button>
<button class="css">css</button>

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

Expand Down
5 changes: 5 additions & 0 deletions packages/playground/dynamic-import/nested/index.js
Expand Up @@ -65,6 +65,11 @@ document.querySelector('.issue-2658-2').addEventListener('click', async () => {
text('.view', msg)
})

document.querySelector('.css').addEventListener('click', async () => {
await import('../css/index.css')
text('.view', 'dynamic import css')
})

function text(el, text) {
document.querySelector(el).textContent = text
}
3 changes: 3 additions & 0 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -120,6 +120,8 @@ export const chunkToEmittedCssFileMap = new WeakMap<
Set<string>
>()

export const romovedPureCssFiles = new Map<string, RenderedChunk>()
crcong marked this conversation as resolved.
Show resolved Hide resolved

const postcssConfigCache = new WeakMap<
ResolvedConfig,
PostCSSConfigResult | null
Expand Down Expand Up @@ -472,6 +474,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
}
}
pureCssChunks.forEach((fileName) => {
romovedPureCssFiles.set(fileName, bundle[fileName] as RenderedChunk)
delete bundle[fileName]
})
}
Expand Down
22 changes: 19 additions & 3 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -4,7 +4,7 @@ import { Plugin } from '../plugin'
import MagicString from 'magic-string'
import { ImportSpecifier, init, parse as parseImports } from 'es-module-lexer'
import { OutputChunk } from 'rollup'
import { chunkToEmittedCssFileMap } from './css'
import { chunkToEmittedCssFileMap, romovedPureCssFiles } from './css'
import { transformImportGlob } from '../importGlob'

/**
Expand Down Expand Up @@ -232,10 +232,11 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
if (imports.length) {
const s = new MagicString(code)
for (let index = 0; index < imports.length; index++) {
const { s: start, e: end } = imports[index]
const { s: start, e: end, d: dynamicIndex } = imports[index]
// check the chunk being imported
const url = code.slice(start, end)
const deps: Set<string> = new Set()
let hasRomovedPureCssChunk = false
crcong marked this conversation as resolved.
Show resolved Hide resolved

if (url[0] === `"` && url[url.length - 1] === `"`) {
const ownerFilename = chunk.fileName
Expand All @@ -255,6 +256,19 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
})
}
chunk.imports.forEach(addDeps)
} else {
const chunk = romovedPureCssFiles.get(filename)
if (chunk) {
const cssFiles = chunkToEmittedCssFileMap.get(chunk)
if (cssFiles && cssFiles.size > 0) {
cssFiles.forEach((file) => {
deps.add(config.base + file)
})
hasRomovedPureCssChunk = true
}

s.overwrite(dynamicIndex, end + 1, 'Promise.resolve({})')
}
}
}
const normalizedFile = path.posix.join(
Expand All @@ -276,7 +290,9 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
markPos + preloadMarker.length + 1,
// the dep list includes the main chunk, so only need to
// preload when there are actual other deps.
deps.size > 1
deps.size > 1 ||
// main chunk is removed
(hasRomovedPureCssChunk && deps.size > 0)
? `[${[...deps].map((d) => JSON.stringify(d)).join(',')}]`
: `[]`
)
Expand Down