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

perf: reduce preload marker markup size #14550

Merged
merged 15 commits into from
Oct 10, 2023
Prev Previous commit
Next Next commit
fix: allow runtime references
  • Loading branch information
gajus committed Oct 7, 2023
commit c6aaabab0642312ea92b6ec431fb16d124772432
45 changes: 25 additions & 20 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,16 +450,21 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
const s = new MagicString(code)
const rewroteMarkerStartPos = new Set() // position of the leading double quote

const fileDeps: string[] = []
const addFileDep = (url: string): number => {
const index = fileDeps.indexOf(url)
if (index !== -1) {
return index
type FileDep = {
url: string
runtime: boolean
};

const fileDeps: FileDep[] = []
const addFileDep = (url: string, runtime: boolean = false): number => {
const index = fileDeps.findIndex((dep) => dep.url === url)
if (index === -1) {
return fileDeps.push({url, runtime}) - 1
} else {
return fileDeps.push(url) - 1
return index
}
}
const getFileDep = (index: number) => {
const getFileDep = (index: number): FileDep => {
const fileDep = fileDeps[index];
if (!fileDep) {
throw new Error(`Cannot find file dep at index ${index}`)
Expand Down Expand Up @@ -552,7 +557,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
? modulePreload === false
? // CSS deps use the same mechanism as module preloads, so even if disabled,
// we still need to pass these deps to the preload helper in dynamic imports.
[...deps].filter((d) => getFileDep(d).endsWith('.css'))
[...deps].filter((d) => getFileDep(d).url.endsWith('.css'))
: [...deps]
: []

Expand All @@ -569,10 +574,10 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
const cssDeps: number[] = []
const otherDeps: number[] = []
for (const dep of depsArray) {
;(getFileDep(dep).endsWith('.css') ? cssDeps : otherDeps).push(dep)
;(getFileDep(dep).url.endsWith('.css') ? cssDeps : otherDeps).push(dep)
}
resolvedDeps = [
...resolveDependencies(normalizedFile, otherDeps.map((otherDep) => getFileDep(otherDep)), {
...resolveDependencies(normalizedFile, otherDeps.map((otherDep) => getFileDep(otherDep).url), {
hostId: file,
hostType: 'js',
}).map((otherDep) => addFileDep(otherDep)),
Expand All @@ -584,26 +589,26 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {

renderedDeps = resolvedDeps.map((dep: number) => {
const replacement = toOutputFilePathInJS(
getFileDep(dep),
getFileDep(dep).url,
'asset',
chunk.fileName,
'js',
config,
toRelativePath,
)
const replacementString =
typeof replacement === 'string'
? JSON.stringify(replacement)
: replacement.runtime

return addFileDep(replacementString);
if (typeof replacement === 'string') {
return addFileDep(replacement);
}

return addFileDep(replacement.runtime, true);
})
} else {
renderedDeps = depsArray.map((d) =>
// Don't include the assets dir if the default asset file names
// are used, the path will be reconstructed by the import preload helper
optimizeModulePreloadRelativePaths
? addFileDep(toRelativePath(getFileDep(d), file))
? addFileDep(toRelativePath(getFileDep(d).url, file))
: d,
)
}
Expand All @@ -618,12 +623,12 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
}
}

const fileDepsCode = `[${fileDeps.map((fileDep) => fileDep.runtime ? fileDep.url : JSON.stringify(fileDep.url)).join(',')}]`;

s.append(`\
function __viteMapDep(indexes) {
gajus marked this conversation as resolved.
Show resolved Hide resolved
if (!__viteMapDep.viteFileDeps) {
__viteMapDep.viteFileDeps = ${JSON.stringify(
fileDeps,
)}
__viteMapDep.viteFileDeps = ${fileDepsCode}
}
return indexes.map((i) => __viteMapDep.viteFileDeps[i])
}`)
Expand Down
2 changes: 1 addition & 1 deletion playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe.runIf(isBuild)('build tests', () => {
const map = findAssetFile(/after-preload-dynamic.*\.js\.map/)
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
{
"mappings": "k2BAAA,OAAO,2BAAuB,EAAC,sEAE/B,QAAQ,IAAI,uBAAuB",
"mappings": "k2BAAA,OAAO,2BAAuB,EAAC,qBAE/B,QAAQ,IAAI,uBAAuB",
"sources": [
"../../after-preload-dynamic.js",
],
Expand Down