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

feat: remove already loaded imports from __vite__mapDeps #18652

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
72 changes: 43 additions & 29 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
ImportSpecifier,
} from 'es-module-lexer'
import { init, parse as parseImports } from 'es-module-lexer'
import type { SourceMap } from 'rollup'
import type { OutputBundle, SourceMap } from 'rollup'
import type { RawSourceMap } from '@ampproject/remapping'
import convertSourceMap from 'convert-source-map'
import {
Expand Down Expand Up @@ -486,6 +486,9 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
})
}

const alreadyImported = new Set<string>()
collectImports(chunk.fileName, alreadyImported, bundle)

const s = new MagicString(code)
const rewroteMarkerStartPos = new Set() // position of the leading double quote

Expand Down Expand Up @@ -530,41 +533,24 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
url,
)

const ownerFilename = chunk.fileName
// literal import - trace direct imports and add to deps
const analyzed: Set<string> = new Set<string>()
const addDeps = (filename: string) => {
if (filename === ownerFilename) return
if (analyzed.has(filename)) return
analyzed.add(filename)
const chunk = bundle[filename]
const importedChunk = bundle[normalizedFile]
if (importedChunk) {
collectImports(normalizedFile, deps, bundle, alreadyImported)
} else {
const removedPureCssFiles =
removedPureCssFilesCache.get(config)!
const chunk = removedPureCssFiles.get(normalizedFile)
if (chunk) {
deps.add(chunk.fileName)
if (chunk.type === 'chunk') {
chunk.imports.forEach(addDeps)
// Ensure that the css imported by current chunk is loaded after the dependencies.
// So the style of current chunk won't be overwritten unexpectedly.
if (chunk.viteMetadata!.importedCss.size) {
chunk.viteMetadata!.importedCss.forEach((file) => {
deps.add(file)
})
hasRemovedPureCssChunk = true
}
} else {
const removedPureCssFiles =
removedPureCssFilesCache.get(config)!
const chunk = removedPureCssFiles.get(filename)
if (chunk) {
if (chunk.viteMetadata!.importedCss.size) {
chunk.viteMetadata!.importedCss.forEach((file) => {
deps.add(file)
})
hasRemovedPureCssChunk = true
}

s.update(expStart, expEnd, 'Promise.resolve({})')
}

s.update(expStart, expEnd, 'Promise.resolve({})')
}
}
addDeps(normalizedFile)
}

let markerStartPos = indexOfMatchInSlice(
Expand Down Expand Up @@ -718,3 +704,31 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
},
}
}

function collectImports(
startFilename: string,
deps: Set<string>,
bundle: OutputBundle,
skipFiles?: ReadonlySet<string>,
) {
// literal import - trace direct imports and add to deps
const analyzed: Set<string> = new Set<string>()
const addDeps = (filename: string) => {
if (skipFiles?.has(filename)) return
if (analyzed.has(filename)) return
analyzed.add(filename)
const chunk = bundle[filename]
if (chunk) {
deps.add(chunk.fileName)
if (chunk.type === 'chunk') {
chunk.imports.forEach(addDeps)
// Ensure that the css imported by current chunk is loaded after the dependencies.
// So the style of current chunk won't be overwritten unexpectedly.
chunk.viteMetadata!.importedCss.forEach((file) => {
deps.add(file)
})
}
}
}
addDeps(startFilename)
}