Skip to content

Commit df36558

Browse files
sapphi-redholm
andauthored
fix(build): resolve dynamic imports with queries appended when collecting CSS dependencies (#23909)
Co-authored-by: Christian Holm <[email protected]>
1 parent 52261e6 commit df36558

6 files changed

Lines changed: 79 additions & 2 deletions

File tree

‎packages/vite/src/node/__tests__/build.spec.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,74 @@ describe('build', () => {
502502
})
503503
})
504504

505+
for (const suffix of ['?dpl=test', '#fragment']) {
506+
describe(`dynamic imports with ${suffix}`, () => {
507+
async function buildProject(library = false) {
508+
const root = resolve(dirname, 'fixtures/dynamic-import-query-fragment')
509+
const entry = resolve(root, library ? 'css-entry.js' : 'entry.js')
510+
const result = await build({
511+
root,
512+
configFile: false,
513+
logLevel: 'silent',
514+
input: entry,
515+
build: {
516+
write: false,
517+
minify: false,
518+
cssCodeSplit: true,
519+
lib: library ? { entry, formats: ['es'] } : false,
520+
},
521+
plugins: [
522+
{
523+
name: 'append-query-or-fragment',
524+
// Append a query string or fragment before Vite looks up output chunks.
525+
// experimental.renderBuiltUrl rewrites preload URLs after that lookup
526+
// and leaves the import specifiers unchanged.
527+
renderChunk(code) {
528+
return code.replace(
529+
/(import\(["'`])([^"'`]+\.m?js)(["'`]\))/g,
530+
`$1$2${suffix}$3`,
531+
)
532+
},
533+
},
534+
],
535+
})
536+
if ('on' in result) throw new Error('Unexpected watcher result')
537+
return Array.isArray(result) ? result[0] : result
538+
}
539+
540+
test('preserves CSS dependencies of lazy JavaScript chunks', async () => {
541+
const { output } = await buildProject()
542+
const entry = output.find(
543+
(chunk): chunk is OutputChunk =>
544+
chunk.type === 'chunk' && chunk.isEntry,
545+
)
546+
assert(entry)
547+
const css = output.find(
548+
(asset) => asset.type === 'asset' && asset.fileName.endsWith('.css'),
549+
)
550+
assert(css)
551+
expect(entry.code).toContain(suffix)
552+
expect(entry.code).toContain(css.fileName)
553+
})
554+
555+
test('removes imports of deleted CSS-only chunks in library builds', async () => {
556+
const { output } = await buildProject(true)
557+
const entry = output.find(
558+
(chunk): chunk is OutputChunk =>
559+
chunk.type === 'chunk' && chunk.isEntry,
560+
)
561+
assert(entry)
562+
expect(
563+
output.some(
564+
(asset) =>
565+
asset.type === 'asset' && asset.fileName.endsWith('.css'),
566+
),
567+
).toBe(true)
568+
expect(entry.code).not.toMatch(/\bimport\s*\(/)
569+
})
570+
})
571+
}
572+
505573
test.for([
506574
[true, true],
507575
[true, false],
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
window.loadLazy = () => import('./lazy.css')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
window.loadLazy = () => import('./lazy.js')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.lazy {
2+
color: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './lazy.css'
2+
3+
export const lazy = true

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { init, parse as parseImports } from 'es-module-lexer'
66
import MagicString from 'magic-string'
77
import type { RenderedChunk, SourceMap } from 'rolldown'
88
import { viteBuildImportAnalysisPlugin as nativeBuildImportAnalysisPlugin } from 'rolldown/experimental'
9+
import { cleanUrl } from '../../shared/utils'
910
import type { PartialEnvironment } from '../baseEnvironment'
1011
import { toOutputFilePathInJS } from '../build'
1112
import type { ResolvedConfig } from '../config'
@@ -565,7 +566,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin[] {
565566

566567
const normalizedFile = path.posix.join(
567568
path.posix.dirname(chunk.fileName),
568-
url,
569+
cleanUrl(url),
569570
)
570571
const fileName =
571572
importMap?.mapping[normalizedFile] ?? normalizedFile
@@ -671,7 +672,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin[] {
671672
if (url) {
672673
normalizedFile = path.posix.join(
673674
path.posix.dirname(chunk.fileName),
674-
url,
675+
cleanUrl(url),
675676
)
676677

677678
let shouldRewriteImport = false

0 commit comments

Comments
 (0)