Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,74 @@ describe('build', () => {
})
})

for (const suffix of ['?dpl=test', '#fragment']) {
describe(`dynamic imports with ${suffix}`, () => {
async function buildProject(library = false) {
const root = resolve(dirname, 'fixtures/dynamic-import-query-fragment')
const entry = resolve(root, library ? 'css-entry.js' : 'entry.js')
const result = await build({
root,
configFile: false,
logLevel: 'silent',
input: entry,
build: {
write: false,
minify: false,
cssCodeSplit: true,
lib: library ? { entry, formats: ['es'] } : false,
},
plugins: [
{
name: 'append-query-or-fragment',
// Append a query string or fragment before Vite looks up output chunks.
// experimental.renderBuiltUrl rewrites preload URLs after that lookup
// and leaves the import specifiers unchanged.
renderChunk(code) {
return code.replace(
/(import\(["'`])([^"'`]+\.m?js)(["'`]\))/g,
`$1$2${suffix}$3`,
)
},
},
],
})
if ('on' in result) throw new Error('Unexpected watcher result')
return Array.isArray(result) ? result[0] : result
}

test('preserves CSS dependencies of lazy JavaScript chunks', async () => {
const { output } = await buildProject()
const entry = output.find(
(chunk): chunk is OutputChunk =>
chunk.type === 'chunk' && chunk.isEntry,
)
assert(entry)
const css = output.find(
(asset) => asset.type === 'asset' && asset.fileName.endsWith('.css'),
)
assert(css)
expect(entry.code).toContain(suffix)
expect(entry.code).toContain(css.fileName)
})

test('removes imports of deleted CSS-only chunks in library builds', async () => {
const { output } = await buildProject(true)
const entry = output.find(
(chunk): chunk is OutputChunk =>
chunk.type === 'chunk' && chunk.isEntry,
)
assert(entry)
expect(
output.some(
(asset) =>
asset.type === 'asset' && asset.fileName.endsWith('.css'),
),
).toBe(true)
expect(entry.code).not.toMatch(/\bimport\s*\(/)
})
})
}

test.for([
[true, true],
[true, false],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.loadLazy = () => import('./lazy.css')
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.loadLazy = () => import('./lazy.js')
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.lazy {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './lazy.css'

export const lazy = true
5 changes: 3 additions & 2 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { init, parse as parseImports } from 'es-module-lexer'
import MagicString from 'magic-string'
import type { RenderedChunk, SourceMap } from 'rolldown'
import { viteBuildImportAnalysisPlugin as nativeBuildImportAnalysisPlugin } from 'rolldown/experimental'
import { cleanUrl } from '../../shared/utils'
import type { PartialEnvironment } from '../baseEnvironment'
import { toOutputFilePathInJS } from '../build'
import type { ResolvedConfig } from '../config'
Expand Down Expand Up @@ -565,7 +566,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin[] {

const normalizedFile = path.posix.join(
path.posix.dirname(chunk.fileName),
url,
cleanUrl(url),
)
const fileName =
importMap?.mapping[normalizedFile] ?? normalizedFile
Expand Down Expand Up @@ -671,7 +672,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin[] {
if (url) {
normalizedFile = path.posix.join(
path.posix.dirname(chunk.fileName),
url,
cleanUrl(url),
)

let shouldRewriteImport = false
Expand Down
Loading