Skip to content

Commit

Permalink
feat: migrate to rolldown
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkz committed Jan 22, 2025
1 parent 9a1019d commit 9293e8c
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 72 deletions.
15 changes: 6 additions & 9 deletions build/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { type BuildConfig, configs } from './config.ts';
import { entries } from './entries.ts';
import { rollupOptions } from './rollup.ts';
import { parseArgs } from 'node:util';
import { rollup } from 'rollup';
import { watch } from 'rollup';
import { rolldown, watch } from 'rolldown';

const { values: args } = parseArgs({
options: {
Expand All @@ -26,9 +25,9 @@ if (!args.dev) {
for (const config of configs) {
console.log('build', config);
const { inputOptions, outputOptions } = await rollupOptions(config);
const bundle = await rollup(inputOptions);
bundle.write(outputOptions);
bundle.close();
const bundle = await rolldown(inputOptions);
await bundle.write(outputOptions);
await bundle.close();
}
} else {
const { inputOptions, outputOptions } = await rollupOptions(
Expand All @@ -46,17 +45,15 @@ if (!args.dev) {
...inputOptions,
output: outputOptions,
watch: {
buildDelay: 1000,
clearScreen: false,
exclude: ['node_modules/**', 'dist/**'],
},
});
watcher.on('event', (event) => {
if (event.code === 'BUNDLE_END') {
event.result.close();
// event.result.close();
} else if (event.code === 'ERROR') {
console.log(event.error);
event.result?.close();
// event.result?.close();
}
});
}
123 changes: 60 additions & 63 deletions build/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ import { entries } from './entries.ts';
import devServer from './plugins/dev-server/index.ts';
import generateTemplate from './plugins/generate-template.ts';
import { readJson, ensureValue, findMatchNote } from './utils.ts';
import alias from '@rollup/plugin-alias';
import commonjs from '@rollup/plugin-commonjs';
import nodePolyfills from '@rolldown/plugin-node-polyfills';
import html from '@rollup/plugin-html';
import json from '@rollup/plugin-json';
import nodeResolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import url from '@rollup/plugin-url';
import virtual from '@rollup/plugin-virtual';
import { dataToEsm } from '@rollup/pluginutils';
import autoprefixer from 'autoprefixer';
Expand All @@ -18,13 +13,13 @@ import fs from 'node:fs/promises';
import path from 'node:path';
import type {
InputOptions,
OutputAsset,
OutputChunk,
OutputOptions,
} from 'rollup';
import nodePolyfills from 'rollup-plugin-polyfill-node';
Plugin,
} from 'rolldown';
import { replacePlugin, aliasPlugin } from 'rolldown/experimental';
import postcss from 'rollup-plugin-postcss';
import { swc, minify } from 'rollup-plugin-swc3';
import { minify } from 'rollup-plugin-swc3';
// import { visualizer } from 'rollup-plugin-visualizer';
import tailwindcss from 'tailwindcss';

Expand All @@ -44,6 +39,13 @@ export async function rollupOptions(config: BuildConfig, dev?: boolean) {
.then(JSON.parse);
return {
input: 'entry',
jsx: {
mode: 'automatic',
factory: 'h',
fragment: 'Fragment',
importSource: 'preact',
},
treeshake: true,
plugins: [
virtual({
'at/options': dataToEsm({
Expand All @@ -55,14 +57,21 @@ export async function rollupOptions(config: BuildConfig, dev?: boolean) {
'at/virtual/field': `export {default as AnkiField} from '${path.resolve(import.meta.dirname, config.field === 'markdown' ? '../src/features/markdown/field.tsx' : '../src/components/native-field.tsx')}'`,
'at/virtual/extract-tf-items': `export {extractItems} from '${path.resolve(import.meta.dirname, config.field === 'markdown' ? '../src/features/tf/extract-markdown-items.ts' : '../src/features/tf/extract-native-items.ts')}'`,
}),
replace({
'process.env.NODE_ENV': JSON.stringify(
envValue('production', 'development'),
),
preventAssignment: true,
}),
json(),
alias({
replacePlugin(
{
'process.env.NODE_ENV': JSON.stringify(
envValue('production', 'development'),
),
'import.meta.env': '({})',
'import.meta.env.MODE': JSON.stringify(
envValue('production', 'development'),
),
},
{
preventAssignment: true,
},
),
aliasPlugin({
entries: [
{
find: 'lodash/isPlainObject',
Expand All @@ -81,53 +90,41 @@ export async function rollupOptions(config: BuildConfig, dev?: boolean) {
{ find: 'react/jsx-runtime', replacement: 'preact/jsx-runtime' },
].filter(Boolean),
}),
nodeResolve({
extensions: ['.mjs', '.js', '.json', '.ts', '.tsx'],
}),
commonjs(),
nodePolyfills(),
swc({
jsc: {
target: 'es5',
parser: {
tsx: true,
syntax: 'typescript',
},
transform: {
react: {
pragma: 'h',
pragmaFrag: 'Fragment',
importSource: 'preact',
runtime: 'automatic',
},
},
minify: {
compress: true,
new Proxy(
postcss({
extract: true,
plugins: [
autoprefixer(),
tailwindcss(),
envValue(
cssnano({
preset: [
'default',
{
discardComments: {
removeAll: true,
},
},
],
}),
false,
),
].filter(Boolean),
}),
{
get(target, p) {
const value = Reflect.get(target, p);
if (p === 'generateBundle') {
return function (...args: any[]) {
this.getModuleInfo = this.getModuleInfo.bind(this);
return value.apply(this, args);
} as Plugin['generateBundle'];
}
return value;
},
},
minify: true,
}),
postcss({
extract: true,
plugins: [
autoprefixer(),
tailwindcss(),
envValue(
cssnano({
preset: [
'default',
{
discardComments: {
removeAll: true,
},
},
],
}),
false,
),
].filter(Boolean),
}),
url(),
),
// visualizer(),
html({
fileName: `front.html`,
Expand All @@ -146,7 +143,7 @@ window.atDefaultOptions =
</script>
`;
frontHtml += `<div data-at-version="${packageJson.version}" id="at-root"></div>`;
frontHtml += `<style>${(files?.css as OutputAsset[])?.map(({ source }) => source).join('')}</style>`;
frontHtml += `<style>${(files?.css as { source: string }[])?.map(({ source }) => source).join('')}</style>`;
frontHtml += `
<div id="at-fields" style="display:none;">
${buildFields()}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@changesets/cli": "^2.27.10",
"@eslint/js": "^9.9.1",
"@koa/router": "^13.1.0",
"@rolldown/plugin-node-polyfills": "^1.0.0",
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-html": "^1.0.3",
Expand Down Expand Up @@ -52,6 +53,7 @@
"postcss": "^8.4.44",
"prettier": "^3.3.3",
"prettier-plugin-tailwindcss": "^0.6.6",
"rolldown": "1.0.0-beta.2-commit.2666b23",
"rollup": "^4.21.2",
"rollup-plugin-polyfill-node": "^0.13.0",
"rollup-plugin-postcss": "^4.0.2",
Expand Down
Loading

0 comments on commit 9293e8c

Please sign in to comment.