Skip to content

Commit c78e47c

Browse files
committed
feat: support rolldown as an experimental engine
1 parent 33edb9c commit c78e47c

File tree

16 files changed

+605
-18
lines changed

16 files changed

+605
-18
lines changed

.changeset/giant-seahorses-call.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ice/pkg': minor
3+
---
4+
5+
feat: support rolldown as an experimental engine

packages/pkg/package.json

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

packages/pkg/src/engine/rollup/options.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { getFilenameConfig } from '../shared/filename.js';
2121
import { getTaskSwcOptions } from '../../helpers/defaultSwcConfig.js';
2222
import { assertTaskBuildableConfig } from '../../helpers/taskConfig.js';
2323

24-
interface PkgJson {
24+
export interface PkgJson {
2525
name: string;
2626
version?: string;
2727
dependencies?: Record<string, string>;
@@ -167,7 +167,14 @@ interface GetRollupOutputsOptions {
167167
mode: NodeEnvMode;
168168
command: Context['command'];
169169
}
170-
function getRollupOutputs({ globals, bundleTaskConfig, pkg, mode, command }: GetRollupOutputsOptions): OutputOptions[] {
170+
171+
export function getRollupOutputs({
172+
globals,
173+
bundleTaskConfig,
174+
pkg,
175+
mode,
176+
command,
177+
}: GetRollupOutputsOptions): OutputOptions[] {
171178
const { outputDir, vendorName = 'vendor' } = bundleTaskConfig;
172179

173180
const outputFormats = bundleTaskConfig.formats ?? [];

packages/pkg/src/helpers/taskConfig.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ export function assertTaskBuildableConfig(
77
throw new Error('Only accept bundle or transform task.');
88
}
99
}
10+
11+
export function assertTaskBundleConfig(taskConfig: TaskConfig): asserts taskConfig is BundleTaskConfig {
12+
if (taskConfig.type !== 'bundle') {
13+
throw new Error('Only accept bundle task.');
14+
}
15+
}

packages/pkg/src/tasks/bundle.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type { FSWatcher } from 'chokidar';
1818
import type { RslibConfig, Rspack, rsbuild } from '@rslib/core';
1919
import { getRollupOptions } from '../engine/rollup/options.js';
2020
import { Runner } from '../helpers/runner.js';
21+
import { RolldownOptions } from 'rolldown';
2122
import { noop } from 'es-toolkit';
2223
import consola from 'consola';
2324

@@ -28,6 +29,7 @@ export function createBundleTask(taskRunningContext: TaskRunnerContext) {
2829
export class BundleRunner extends Runner<OutputResult> {
2930
private rollupOptions?: RollupOptions;
3031
private rslibConfig?: RslibConfig;
32+
private rolldownOptions?: RolldownOptions;
3133
private engine: EngineType;
3234
private watcher: Watcher | null = null;
3335
private result: Error | OutputResult | null;
@@ -43,6 +45,8 @@ export class BundleRunner extends Runner<OutputResult> {
4345
return this.handleRollupBuild(changedFiles);
4446
case 'rslib':
4547
return this.handleRslibBuild(changedFiles);
48+
case 'rolldown':
49+
return this.handleRolldownBuild(changedFiles);
4650
}
4751
}
4852

@@ -189,6 +193,23 @@ export class BundleRunner extends Runner<OutputResult> {
189193
outputFiles: stats.assets as any,
190194
};
191195
}
196+
197+
private async handleRolldownBuild(changedFiles: WatchChangedFile[]): Promise<OutputResult> {
198+
const { context } = this;
199+
const { build } = await import('rolldown');
200+
if (!this.rolldownOptions) {
201+
const { getRolldownOptions } = await import('../engine/rolldown/options.js');
202+
this.rolldownOptions = getRolldownOptions(context.buildContext, context);
203+
}
204+
const bundle = await build(this.rolldownOptions);
205+
206+
return {
207+
taskName: context.buildTask.name,
208+
// TODO: correct type and value
209+
outputs: bundle.output as any,
210+
outputFiles: bundle.output as any,
211+
};
212+
}
192213
}
193214

194215
// Fork from https://github.com/rollup/rollup/blob/v2.79.1/src/watch/WatchEmitter.ts
@@ -300,7 +321,7 @@ async function rawBuild(rollupOptions: RollupOptions, taskRunnerContext: TaskRun
300321

301322
const bundle = await rollup.rollup(rollupOptions);
302323

303-
const buildResult = await writeFiles(rollupOutputOptions, bundle.write);
324+
const buildResult = await writeFiles(rollupOutputOptions, bundle.write.bind(bundle));
304325

305326
await bundle.close();
306327

packages/pkg/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ interface _TaskConfig {
342342
pkg?: PackageResolvedConfig;
343343
}
344344

345-
export type EngineType = 'rollup' | 'rslib';
345+
export type EngineType = 'rollup' | 'rslib' | 'rolldown';
346346

347347
export interface BundleTaskConfig extends _TaskConfig, Omit<BundleUserConfig, 'development' | 'minify' | 'formats'> {
348348
type: 'bundle';

0 commit comments

Comments
 (0)