|
| 1 | +import { RolldownOptions, Plugin, OutputOptions } from 'rolldown'; |
| 2 | +import { Context, StylesRollupPluginOptions, TaskRunnerContext } from '../../types.js'; |
| 3 | +import { getExternalsAndGlobals, getRollupOutputs, PkgJson } from '../rollup/options.js'; |
| 4 | +import { assertTaskBundleConfig } from '../../helpers/taskConfig.js'; |
| 5 | +import path from 'node:path'; |
| 6 | +import getDefaultDefineValues from '../../helpers/getDefaultDefineValues.js'; |
| 7 | +import styles from 'rollup-plugin-styler'; |
| 8 | +import image from '@rollup/plugin-image'; |
| 9 | +import autoprefixer from 'autoprefixer'; |
| 10 | +import PostcssPluginRpxToVw from 'postcss-plugin-rpx2vw'; |
| 11 | +import { visualizer } from 'rollup-plugin-visualizer'; |
| 12 | +import { JSX_RUNTIME_SOURCE } from '../../constants.js'; |
| 13 | +import { RollupOptions } from 'rollup'; |
| 14 | + |
| 15 | +export function getRolldownOptions(context: Context, taskRunnerContext: TaskRunnerContext): RolldownOptions { |
| 16 | + const { pkg, commandArgs, command, rootDir } = context; |
| 17 | + const { buildTask } = taskRunnerContext; |
| 18 | + const { name: taskName, config: taskConfig } = buildTask; |
| 19 | + |
| 20 | + assertTaskBundleConfig(taskConfig); |
| 21 | + |
| 22 | + const options: RolldownOptions = {}; |
| 23 | + |
| 24 | + const [external, globals] = getExternalsAndGlobals(taskConfig, pkg as PkgJson); |
| 25 | + |
| 26 | + options.input = taskConfig.entry; |
| 27 | + options.external = external; |
| 28 | + // TODO: should warning if output is multiple |
| 29 | + options.output = getRollupOutputs({ |
| 30 | + globals, |
| 31 | + bundleTaskConfig: taskConfig, |
| 32 | + pkg: pkg as PkgJson, |
| 33 | + mode: taskRunnerContext.mode, |
| 34 | + command, |
| 35 | + })[0] as OutputOptions; |
| 36 | + |
| 37 | + const alias = {}; |
| 38 | + Object.keys(taskConfig.alias).forEach((key) => { |
| 39 | + // Add full path for relative path alias |
| 40 | + alias[key] = taskConfig.alias[key].startsWith('.') |
| 41 | + ? path.resolve(rootDir, taskConfig.alias[key]) |
| 42 | + : taskConfig.alias[key]; |
| 43 | + }); |
| 44 | + |
| 45 | + const plugins: Plugin[] = []; |
| 46 | + |
| 47 | + options.resolve = { |
| 48 | + alias, |
| 49 | + }; |
| 50 | + options.define = { |
| 51 | + ...getDefaultDefineValues(taskRunnerContext.mode), |
| 52 | + // User define can override above. |
| 53 | + ...taskConfig.define, |
| 54 | + }; |
| 55 | + |
| 56 | + options.jsx = { |
| 57 | + mode: taskConfig.jsxRuntime ?? 'automatic', |
| 58 | + jsxImportSource: JSX_RUNTIME_SOURCE, |
| 59 | + }; |
| 60 | + |
| 61 | + const cssMinify = taskConfig.cssMinify(taskRunnerContext.mode, command); |
| 62 | + const defaultStylesOptions: StylesRollupPluginOptions = { |
| 63 | + plugins: [autoprefixer(), PostcssPluginRpxToVw], |
| 64 | + mode: 'extract', |
| 65 | + autoModules: true, |
| 66 | + minimize: typeof cssMinify === 'boolean' ? cssMinify : cssMinify.options, |
| 67 | + sourceMap: taskConfig.sourcemap, |
| 68 | + }; |
| 69 | + |
| 70 | + plugins.push( |
| 71 | + styles( |
| 72 | + (taskConfig.modifyStylesOptions ?? [(options) => options]).reduce( |
| 73 | + (prevStylesOptions, modifyStylesOptions) => modifyStylesOptions(prevStylesOptions), |
| 74 | + defaultStylesOptions, |
| 75 | + ), |
| 76 | + ) as unknown as Plugin<any>, |
| 77 | + image() as unknown as Plugin<any>, |
| 78 | + ); |
| 79 | + |
| 80 | + if (commandArgs.analyzer) { |
| 81 | + plugins.push( |
| 82 | + visualizer({ |
| 83 | + title: `Rollup Visualizer(${taskName})`, |
| 84 | + open: true, |
| 85 | + filename: `${taskName}-stats.html`, |
| 86 | + }) as unknown as Plugin<any>, |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + options.plugins = plugins; |
| 91 | + |
| 92 | + return (taskConfig.modifyRollupOptions ?? [(options) => options]).reduce( |
| 93 | + (prevOptions, modifyRollupOptions) => |
| 94 | + modifyRollupOptions(prevOptions as unknown as RollupOptions) as RolldownOptions, |
| 95 | + options, |
| 96 | + ); |
| 97 | +} |
0 commit comments