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: use prepareRollupOptimizerRun instead of prepareEsbuildOptimizerRun #6

Closed
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
114 changes: 48 additions & 66 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fsp from 'node:fs/promises'
import path from 'node:path'
import { promisify } from 'node:util'
import { performance } from 'node:perf_hooks'
import type { RollupOutput } from 'rollup';
import type { RollupOutput } from 'rollup'
import rollup from 'rollup'
import colors from 'picocolors'
import type { BuildContext, BuildOptions as EsbuildBuildOptions } from 'esbuild'
Expand Down Expand Up @@ -597,84 +597,71 @@ export function runOptimizeDeps(

const start = performance.now()

const preparedRun = prepareEsbuildOptimizerRun(
const preparedRun = prepareRollupOptimizerRun(
resolvedConfig,
depsInfo,
ssr,
processingCacheDir,
optimizerContext,
)

const runResult = preparedRun.then(({ context, idToExports }) => {
function disposeContext() {
return context?.dispose().catch((e) => {
config.logger.error('Failed to dispose esbuild context', { error: e })
})
}
if (!context || optimizerContext.cancelled) {
disposeContext()
const runResult = preparedRun.then(({ build, idToExports }) => {
if (!build || optimizerContext.cancelled) {
return cancelledResult
}

return context
.rebuild()
return build()
.then((result) => {
const meta = result.metafile!

// the paths in `meta.outputs` are relative to `process.cwd()`
// TODO: Make sure the paths in `meta.outputs` are relative to `process.cwd()`
const processingCacheDirOutputPath = path.relative(
process.cwd(),
processingCacheDir,
)

for (const id in depsInfo) {
const output = esbuildOutputFromId(
meta.outputs,
id,
processingCacheDir,
)

const { exportsData, ...info } = depsInfo[id]
addOptimizedDepInfo(metadata, 'optimized', {
...info,
// We only need to hash the output.imports in to check for stability, but adding the hash
// and file path gives us a unique hash that may be useful for other things in the future
fileHash: getHash(
metadata.hash +
depsInfo[id].file +
JSON.stringify(output.imports),
),
browserHash: metadata.browserHash,
// After bundling we have more information and can warn the user about legacy packages
// that require manual configuration
needsInterop: needsInterop(
config,
ssr,
id,
idToExports[id],
output,
),
})
}

for (const o of Object.keys(meta.outputs)) {
if (!o.match(jsMapExtensionRE)) {
const id = path
.relative(processingCacheDirOutputPath, o)
.replace(jsExtensionRE, '')
const file = getOptimizedDepPath(id, resolvedConfig, ssr)
if (
!findOptimizedDepInfoInRecord(
metadata.optimized,
(depInfo) => depInfo.file === file,
)
) {
addOptimizedDepInfo(metadata, 'chunks', {
id,
file,
needsInterop: false,
for (const chunk of result.output) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vite using esbuild metafile.outputs to collect optimized metadata, here using RollupOutput instead of it. Rolldown also need implement this, track here rolldown/rolldown#185

if (chunk.type === 'chunk' && chunk.isEntry) {
if (chunk.isEntry) {
const id = chunk.facadeModuleId!

const { exportsData, ...info } = depsInfo[id]
addOptimizedDepInfo(metadata, 'optimized', {
...info,
// We only need to hash the output.imports in to check for stability, but adding the hash
// and file path gives us a unique hash that may be useful for other things in the future
fileHash: getHash(
metadata.hash +
depsInfo[id].file +
JSON.stringify(chunk.imports),
),
browserHash: metadata.browserHash,
// After bundling we have more information and can warn the user about legacy packages
// that require manual configuration
needsInterop: needsInterop(
config,
ssr,
id,
idToExports[id],
chunk,
),
})
} else {
const id = path
.relative(processingCacheDirOutputPath, chunk.fileName)
.replace(jsExtensionRE, '')
const file = getOptimizedDepPath(id, resolvedConfig, ssr)
if (
!findOptimizedDepInfoInRecord(
metadata.optimized,
(depInfo) => depInfo.file === file,
)
) {
addOptimizedDepInfo(metadata, 'chunks', {
id,
file,
needsInterop: false,
browserHash: metadata.browserHash,
})
}
}
}
}
Expand All @@ -694,9 +681,6 @@ export function runOptimizeDeps(
}
throw e
})
.finally(() => {
return disposeContext()
})
})

runResult.catch(() => {
Expand All @@ -706,8 +690,6 @@ export function runOptimizeDeps(
return {
async cancel() {
optimizerContext.cancelled = true
const { context } = await preparedRun
await context?.cancel()
cleanUp()
},
result: runResult,
Expand Down
Loading