Skip to content

Commit

Permalink
fix: fix optimize test
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Dec 13, 2023
1 parent 49f5dd3 commit 46e9bee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
1 change: 1 addition & 0 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export function rolldownDepPlugin(
}

if (id.startsWith(browserExternalNamespace)) {
const path = id.slice(browserExternalNamespace.length)
if (config.isProduction) {
return {
code: 'module.exports = {}',
Expand Down
59 changes: 29 additions & 30 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,41 +613,36 @@ export function runOptimizeDeps(

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

for (const chunk of result.output) {
if (chunk.type === 'chunk') {
if (chunk.isEntry) {
const { exportsData, file, id, ...info } = Object.values(
depsInfo,
).find(
// One chunk maybe corresponding multiply entry
const deps = Object.values(depsInfo).filter(
(d) =>
d.src === path.join(process.cwd(), chunk.facadeModuleId!),
)!
addOptimizedDepInfo(metadata, 'optimized', {
id,
file,
...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 + file + JSON.stringify(chunk.modules),
),
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,
)
for (const { exportsData, file, id, ...info } of deps) {
addOptimizedDepInfo(metadata, 'optimized', {
id,
idToExports[id],
chunk,
),
})
file,
...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 + file + JSON.stringify(chunk.modules),
),
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 = chunk.fileName.replace(jsExtensionRE, '')
const file = getOptimizedDepPath(id, resolvedConfig, ssr)
Expand Down Expand Up @@ -808,12 +803,16 @@ async function prepareRollupOptimizerRun(
})

async function build() {
// TODO platform
const bundle = await rolldown.rolldown({
input: Object.keys(flatIdDeps),
external,
// logLevel: 'warn',
plugins,
resolve: {
mainFields: ['module', 'main'],
aliasFields: [['browser']],
// extensions: ['.js', '.css']
},
...rollupOptions,
})
return await bundle.write({
Expand Down
5 changes: 3 additions & 2 deletions playground/optimize-deps/__tests__/optimize-deps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,9 @@ test('name file limit is 170 characters', async () => {
const onlyTheFilePart = stripFolderPart.split('.')[0]
expect(onlyTheFilePart).toHaveLength(170)

const text = await content.text()
expect(text).toMatch(/import\s+("[^"]+")/)
// the test should be exclude, the chunk content shouldn't import runtime module
// const text = await content.text()
// expect(text).toMatch(/import\s+("[^"]+")/)
}
})

Expand Down

0 comments on commit 46e9bee

Please sign in to comment.