Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate to rolldown #69

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { rollupOptions } from './rollup.ts';
import { configMatch } from './utils.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 Down Expand Up @@ -43,9 +42,9 @@ if (!args.dev) {
dev: false,
e2e: args.e2e ?? false,
});
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 @@ -66,17 +65,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();
}
});
}
133 changes: 59 additions & 74 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 { 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 tailwindcss from 'tailwindcss';

const packageJson = JSON.parse(
Expand All @@ -35,7 +30,7 @@ const packageJson = JSON.parse(

export async function rollupOptions(
config: BuildConfig,
{ dev = false, e2e = false }: { dev?: boolean; e2e?: boolean } = {},
{ dev = false }: { dev?: boolean; e2e?: boolean } = {},
) {
async function buildInputOptions() {
const i18nMap = await fs
Expand All @@ -50,6 +45,13 @@ export async function rollupOptions(
.then(JSON.parse);
return {
input: 'entry',
jsx: {
mode: 'automatic',
factory: 'h',
fragment: 'Fragment',
importSource: 'preact',
},
treeshake: true,
plugins: [
virtual({
'at/options': dataToEsm({
Expand All @@ -61,14 +63,21 @@ export async function rollupOptions(
'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 @@ -87,65 +96,41 @@ export async function rollupOptions(
{ 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,
},
experimental: {
plugins: e2e
? []
: [
[
'@swc/plugin-react-remove-properties',
{
properties: ['^data-testid$'],
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 @@ -164,7 +149,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 @@ -26,6 +26,7 @@
"@eslint/js": "^9.9.1",
"@koa/router": "^13.1.0",
"@playwright/test": "^1.49.1",
"@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 @@ -63,6 +64,7 @@
"postcss": "^8.5.1",
"prettier": "^3.3.3",
"prettier-plugin-tailwindcss": "^0.6.6",
"rolldown": "1.0.0-beta.3-commit.d9529e7",
"rollup": "^4.21.2",
"rollup-plugin-polyfill-node": "^0.13.0",
"rollup-plugin-postcss": "^4.0.2",
Expand Down
Loading
Loading