Skip to content

Commit fa62ad7

Browse files
committed
refactor: extract resolveRollupOptions
1 parent 8119f8d commit fa62ad7

File tree

1 file changed

+108
-102
lines changed

1 file changed

+108
-102
lines changed

packages/vite/src/node/build.ts

Lines changed: 108 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -524,25 +524,12 @@ function resolveConfigToBuild(
524524
)
525525
}
526526

527-
/**
528-
* Build an App environment, or a App library (if libraryOptions is provided)
529-
**/
530-
async function buildEnvironment(
531-
environment: BuildEnvironment,
532-
): Promise<RollupOutput | RollupOutput[] | RollupWatcher> {
527+
function resolveRollupOptions(environment: Environment) {
533528
const { root, packageCache, build: options } = environment.config
534529
const libOptions = options.lib
535530
const { logger } = environment
536531
const ssr = environment.config.consumer === 'server'
537532

538-
logger.info(
539-
colors.cyan(
540-
`vite v${VERSION} ${colors.green(
541-
`building ${ssr ? `SSR bundle ` : ``}for ${environment.config.mode}...`,
542-
)}`,
543-
),
544-
)
545-
546533
const resolve = (p: string) => path.resolve(root, p)
547534
const input = libOptions
548535
? options.rollupOptions.input ||
@@ -608,96 +595,116 @@ async function buildEnvironment(
608595
environment.name === 'ssr' &&
609596
environment.getTopLevelConfig().ssr?.target === 'webworker'
610597

611-
let bundle: RollupBuild | undefined
612-
let startTime: number | undefined
613-
try {
614-
const buildOutputOptions = (output: OutputOptions = {}): OutputOptions => {
615-
// @ts-expect-error See https://github.com/vitejs/vite/issues/5812#issuecomment-984345618
616-
if (output.output) {
617-
logger.warn(
618-
`You've set "rollupOptions.output.output" in your config. ` +
619-
`This is deprecated and will override all Vite.js default output options. ` +
620-
`Please use "rollupOptions.output" instead.`,
621-
)
622-
}
623-
if (output.file) {
624-
throw new Error(
625-
`Vite does not support "rollupOptions.output.file". ` +
626-
`Please use "rollupOptions.output.dir" and "rollupOptions.output.entryFileNames" instead.`,
627-
)
628-
}
629-
if (output.sourcemap) {
630-
logger.warnOnce(
631-
colors.yellow(
632-
`Vite does not support "rollupOptions.output.sourcemap". ` +
633-
`Please use "build.sourcemap" instead.`,
634-
),
635-
)
636-
}
598+
const buildOutputOptions = (output: OutputOptions = {}): OutputOptions => {
599+
// @ts-expect-error See https://github.com/vitejs/vite/issues/5812#issuecomment-984345618
600+
if (output.output) {
601+
logger.warn(
602+
`You've set "rollupOptions.output.output" in your config. ` +
603+
`This is deprecated and will override all Vite.js default output options. ` +
604+
`Please use "rollupOptions.output" instead.`,
605+
)
606+
}
607+
if (output.file) {
608+
throw new Error(
609+
`Vite does not support "rollupOptions.output.file". ` +
610+
`Please use "rollupOptions.output.dir" and "rollupOptions.output.entryFileNames" instead.`,
611+
)
612+
}
613+
if (output.sourcemap) {
614+
logger.warnOnce(
615+
colors.yellow(
616+
`Vite does not support "rollupOptions.output.sourcemap". ` +
617+
`Please use "build.sourcemap" instead.`,
618+
),
619+
)
620+
}
637621

638-
const format = output.format || 'es'
639-
const jsExt =
640-
(ssr && !isSsrTargetWebworkerEnvironment) || libOptions
641-
? resolveOutputJsExtension(
642-
format,
643-
findNearestPackageData(root, packageCache)?.data.type,
644-
)
645-
: 'js'
646-
return {
647-
dir: outDir,
648-
// Default format is 'es' for regular and for SSR builds
649-
format,
650-
exports: 'auto',
651-
sourcemap: options.sourcemap,
652-
name: libOptions ? libOptions.name : undefined,
653-
hoistTransitiveImports: libOptions ? false : undefined,
654-
// es2015 enables `generatedCode.symbols`
655-
// - #764 add `Symbol.toStringTag` when build es module into cjs chunk
656-
// - #1048 add `Symbol.toStringTag` for module default export
657-
generatedCode: 'es2015',
658-
entryFileNames: ssr
659-
? `[name].${jsExt}`
660-
: libOptions
661-
? ({ name }) =>
662-
resolveLibFilename(
663-
libOptions,
664-
format,
665-
name,
666-
root,
667-
jsExt,
668-
packageCache,
669-
)
670-
: path.posix.join(options.assetsDir, `[name]-[hash].${jsExt}`),
671-
chunkFileNames: libOptions
672-
? `[name]-[hash].${jsExt}`
622+
const format = output.format || 'es'
623+
const jsExt =
624+
(ssr && !isSsrTargetWebworkerEnvironment) || libOptions
625+
? resolveOutputJsExtension(
626+
format,
627+
findNearestPackageData(root, packageCache)?.data.type,
628+
)
629+
: 'js'
630+
return {
631+
dir: outDir,
632+
// Default format is 'es' for regular and for SSR builds
633+
format,
634+
exports: 'auto',
635+
sourcemap: options.sourcemap,
636+
name: libOptions ? libOptions.name : undefined,
637+
hoistTransitiveImports: libOptions ? false : undefined,
638+
// es2015 enables `generatedCode.symbols`
639+
// - #764 add `Symbol.toStringTag` when build es module into cjs chunk
640+
// - #1048 add `Symbol.toStringTag` for module default export
641+
generatedCode: 'es2015',
642+
entryFileNames: ssr
643+
? `[name].${jsExt}`
644+
: libOptions
645+
? ({ name }) =>
646+
resolveLibFilename(
647+
libOptions,
648+
format,
649+
name,
650+
root,
651+
jsExt,
652+
packageCache,
653+
)
673654
: path.posix.join(options.assetsDir, `[name]-[hash].${jsExt}`),
674-
assetFileNames: libOptions
675-
? `[name].[ext]`
676-
: path.posix.join(options.assetsDir, `[name]-[hash].[ext]`),
677-
inlineDynamicImports:
678-
output.format === 'umd' ||
679-
output.format === 'iife' ||
680-
(isSsrTargetWebworkerEnvironment &&
681-
(typeof input === 'string' || Object.keys(input).length === 1)),
682-
...output,
683-
}
655+
chunkFileNames: libOptions
656+
? `[name]-[hash].${jsExt}`
657+
: path.posix.join(options.assetsDir, `[name]-[hash].${jsExt}`),
658+
assetFileNames: libOptions
659+
? `[name].[ext]`
660+
: path.posix.join(options.assetsDir, `[name]-[hash].[ext]`),
661+
inlineDynamicImports:
662+
output.format === 'umd' ||
663+
output.format === 'iife' ||
664+
(isSsrTargetWebworkerEnvironment &&
665+
(typeof input === 'string' || Object.keys(input).length === 1)),
666+
...output,
684667
}
668+
}
685669

686-
// resolve lib mode outputs
687-
const outputs = resolveBuildOutputs(
688-
options.rollupOptions.output,
689-
libOptions,
690-
logger,
691-
)
692-
const normalizedOutputs: OutputOptions[] = []
670+
// resolve lib mode outputs
671+
const outputs = resolveBuildOutputs(
672+
options.rollupOptions.output,
673+
libOptions,
674+
logger,
675+
)
693676

694-
if (Array.isArray(outputs)) {
695-
for (const resolvedOutput of outputs) {
696-
normalizedOutputs.push(buildOutputOptions(resolvedOutput))
697-
}
698-
} else {
699-
normalizedOutputs.push(buildOutputOptions(outputs))
700-
}
677+
if (Array.isArray(outputs)) {
678+
rollupOptions.output = outputs.map(buildOutputOptions)
679+
} else {
680+
rollupOptions.output = buildOutputOptions(outputs)
681+
}
682+
683+
return rollupOptions
684+
}
685+
686+
/**
687+
* Build an App environment, or a App library (if libraryOptions is provided)
688+
**/
689+
async function buildEnvironment(
690+
environment: BuildEnvironment,
691+
): Promise<RollupOutput | RollupOutput[] | RollupWatcher> {
692+
const { logger, config } = environment
693+
const { root, build: options } = config
694+
const ssr = config.consumer === 'server'
695+
696+
logger.info(
697+
colors.cyan(
698+
`vite v${VERSION} ${colors.green(
699+
`building ${ssr ? `SSR bundle ` : ``}for ${environment.config.mode}...`,
700+
)}`,
701+
),
702+
)
703+
704+
let bundle: RollupBuild | undefined
705+
let startTime: number | undefined
706+
try {
707+
const rollupOptions = resolveRollupOptions(environment)
701708

702709
// watch file changes with rollup
703710
if (options.watch) {
@@ -724,7 +731,6 @@ async function buildEnvironment(
724731
const { watch } = await import('rollup')
725732
const watcher = watch({
726733
...rollupOptions,
727-
output: normalizedOutputs,
728734
watch: {
729735
...options.watch,
730736
chokidar: resolvedChokidarOptions,
@@ -754,13 +760,13 @@ async function buildEnvironment(
754760
bundle = await rollup(rollupOptions)
755761

756762
const res: RollupOutput[] = []
757-
for (const output of normalizedOutputs) {
763+
for (const output of arraify(rollupOptions.output!)) {
758764
res.push(await bundle[options.write ? 'write' : 'generate'](output))
759765
}
760766
logger.info(
761767
`${colors.green(`✓ built in ${displayTime(Date.now() - startTime)}`)}`,
762768
)
763-
return Array.isArray(outputs) ? res : res[0]
769+
return Array.isArray(rollupOptions.output) ? res : res[0]
764770
} catch (e) {
765771
enhanceRollupError(e)
766772
clearLine()

0 commit comments

Comments
 (0)