Skip to content

build: use rolldown #13599

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

Draft
wants to merge 5 commits into
base: rolldown
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
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "node scripts/build.js",
"build-rollup": "node scripts/build-with-rollup.js",
"build-dts": "node scripts/build-types.js",
"build-dts-tsc": "tsc -p tsconfig.build.json --noCheck && rollup -c rollup.dts.config.js",
"build-dts-tsc": "tsc -p tsconfig.build.json --noCheck && rolldown -c rolldown.dts.config.js",
"clean": "rimraf --glob packages/*/dist temp .eslintcache",
"size": "run-s \"size-*\" && node scripts/usage-size.js",
"size-global": "node scripts/build.js vue runtime-dom -f global -p --size",
Expand Down Expand Up @@ -94,19 +94,20 @@
"markdown-table": "^3.0.4",
"marked": "13.0.3",
"npm-run-all2": "^7.0.1",
"oxc-parser": "^0.35.0",
"oxc-transform": "^0.35.0",
"oxc-parser": "^0.76.0",
"oxc-transform": "^0.76.0",
"oxc-minify": "^0.76.0",
"picocolors": "^1.1.1",
"prettier": "^3.3.3",
"pretty-bytes": "^6.1.1",
"pug": "^3.0.3",
"puppeteer": "~23.3.0",
"rimraf": "^6.0.1",
"rolldown": "0.14.0-snapshot-d5e797b-20241114003621",
"rolldown": "1.0.0-beta.24",
"rollup": "^4.25.0",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-esbuild": "^6.1.1",
"rollup-plugin-polyfill-node": "^0.13.0",
"rolldown-plugin-dts": "0.13.13",
"semver": "^7.6.3",
"serve": "^14.2.4",
"serve-handler": "^6.1.6",
Expand Down
911 changes: 695 additions & 216 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

31 changes: 23 additions & 8 deletions rollup.dts.config.js → rolldown.dts.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import assert from 'node:assert/strict'
import { parseSync } from 'oxc-parser'
import { existsSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'
import MagicString from 'magic-string'
import dts from 'rollup-plugin-dts'
import { dts } from 'rolldown-plugin-dts'
import { createRequire } from 'node:module'
import { fileURLToPath } from 'node:url'
import path from 'node:path'

if (!existsSync('temp/packages')) {
console.warn(
Expand All @@ -12,24 +15,37 @@ if (!existsSync('temp/packages')) {
process.exit(1)
}

const require = createRequire(import.meta.url)
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const packagesDir = path.resolve(__dirname, 'packages')

const packages = readdirSync('temp/packages')
const targets = process.env.TARGETS ? process.env.TARGETS.split(',') : null
const targetPackages = targets
? packages.filter(pkg => targets.includes(pkg))
: packages

function resolveExternal(packageName) {
const pkg = require(`${packagesDir}/${packageName}/package.json`)
return [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
]
}

export default targetPackages.map(
/** @returns {import('rollup').RollupOptions} */
/** @returns {import('rolldown').BuildOptions} */
pkg => {
return {
input: `./temp/packages/${pkg}/src/index.d.ts`,
output: {
file: `packages/${pkg}/dist/${pkg}.d.ts`,
format: 'es',
},
external: resolveExternal(pkg),
plugins: [dts(), patchTypes(pkg), ...(pkg === 'vue' ? [copyMts()] : [])],
onwarn(warning, warn) {
// during dts rollup, everything is externalized by default
// during dts rolldown, everything is externalized by default
if (
warning.code === 'UNRESOLVED_IMPORT' &&
!warning.exporter?.startsWith('.')
Expand All @@ -43,23 +59,22 @@ export default targetPackages.map(
)

/**
* Patch the dts generated by rollup-plugin-dts
* Patch the dts generated by rolldown-plugin-dts
* 1. Convert all types to inline exports
* and remove them from the big export {} declaration
* otherwise it gets weird in vitepress `defineComponent` call with
* "the inferred type cannot be named without a reference"
* 2. Append custom augmentations (jsx, macros)
*
* @param {string} pkg
* @returns {import('rollup').Plugin}
* @returns {import('rolldown').Plugin}
*/
function patchTypes(pkg) {
return {
name: 'patch-types',
renderChunk(code, chunk) {
const s = new MagicString(code)
const { program: ast, errors } = parseSync(code, {
sourceFilename: 'x.d.ts',
const { program: ast, errors } = parseSync('x.d.ts', code, {
sourceType: 'module',
})

Expand Down Expand Up @@ -201,7 +216,7 @@ function patchTypes(pkg) {
* two separate declaration files, so we need to copy vue.d.ts to vue.d.mts
* upon build.
*
* @returns {import('rollup').Plugin}
* @returns {import('rolldown').Plugin}
*/
function copyMts() {
return {
Expand Down
12 changes: 6 additions & 6 deletions scripts/build-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'node:fs'
import path from 'node:path'
import glob from 'fast-glob'
import { isolatedDeclaration } from 'oxc-transform'
import { rollup } from 'rollup'
import { rolldown } from 'rolldown'
import picocolors from 'picocolors'

if (fs.existsSync('temp/packages')) {
Expand Down Expand Up @@ -43,16 +43,16 @@ if (errs) {
write(path.join('temp', 'oxc-iso-decl-errors.txt'), errs)
}

console.log('bundling dts with rollup-plugin-dts...')
console.log('bundling dts with rolldown-plugin-dts...')

// bundle with rollup-plugin-dts
const rollupConfigs = (await import('../rollup.dts.config.js')).default
// bundle with rolldown-plugin-dts
const rolldownConfigs = (await import('../rolldown.dts.config.js')).default

start = performance.now()

await Promise.all(
rollupConfigs.map(c =>
rollup(c).then(bundle => {
rolldownConfigs.map(c =>
rolldown(c).then(bundle => {
return bundle.write(c.output).then(() => {
console.log(picocolors.gray('built: ') + picocolors.blue(c.output.file))
})
Expand Down
11 changes: 6 additions & 5 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,15 @@ async function buildAll(targets) {
if (configs) {
all.push(
Promise.all(
configs.map(c =>
rolldown(c).then(bundle => {
configs.map(c => {
return rolldown(c).then(bundle => {
// @ts-expect-error
return bundle.write(c.output).then(() => {
// @ts-expect-error
return path.join('packages', t, 'dist', c.output.file)
return c.output.file
})
}),
),
})
}),
).then(files => {
files.forEach(f => {
count++
Expand Down
39 changes: 15 additions & 24 deletions scripts/create-rolldown-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import pico from 'picocolors'
import polyfillNode from '@rolldown/plugin-node-polyfills'
import { entries } from './aliases.js'
import { inlineEnums } from './inline-enums.js'
import { minify as minifySwc } from '@swc/core'
import { minify as minifyOxc } from 'oxc-minify'

const require = createRequire(import.meta.url)
const __dirname = fileURLToPath(new URL('.', import.meta.url))
Expand Down Expand Up @@ -59,32 +59,32 @@ export function createConfigsForPackage({
/** @type {Record<PackageFormat, import('rolldown').OutputOptions>} */
const outputConfigs = {
'esm-bundler': {
file: `${name}.esm-bundler.js`,
file: resolve(`dist/${name}.esm-bundler.js`),
format: 'es',
},
'esm-browser': {
file: `${name}.esm-browser.js`,
file: resolve(`dist/${name}.esm-browser.js`),
format: 'es',
},
cjs: {
file: `${name}.cjs.js`,
file: resolve(`dist/${name}.cjs.js`),
format: 'cjs',
},
global: {
file: `${name}.global.js`,
file: resolve(`dist/${name}.global.js`),
format: 'iife',
},
// runtime-only builds, for main "vue" package only
'esm-bundler-runtime': {
file: `${name}.runtime.esm-bundler.js`,
file: resolve(`dist/${name}.runtime.esm-bundler.js`),
format: 'es',
},
'esm-browser-runtime': {
file: `${name}.runtime.esm-browser.js`,
file: resolve(`dist/${name}.runtime.esm-browser.js`),
format: 'es',
},
'global-runtime': {
file: `${name}.runtime.global.js`,
file: resolve(`dist/${name}.runtime.global.js`),
format: 'iife',
},
}
Expand Down Expand Up @@ -125,8 +125,6 @@ export function createConfigsForPackage({
process.exit(1)
}

output.dir = resolve('dist')

const isProductionBuild = /\.prod\.js$/.test(String(output.file) || '')
const isBundlerESMBuild = /esm-bundler/.test(format)
const isBrowserESMBuild = /esm-browser/.test(format)
Expand Down Expand Up @@ -317,7 +315,6 @@ export function createConfigsForPackage({
resolve: {
alias: entries,
},
// @ts-expect-error rollup's Plugin type incompatible w/ rolldown's vendored Plugin type
plugins: [
...(localDev ? [] : [enumPlugin]),
...resolveReplace(),
Expand All @@ -339,7 +336,7 @@ export function createConfigsForPackage({

function createProductionConfig(/** @type {PackageFormat} */ format) {
return createConfig(format, {
file: `${name}.${format}.prod.js`,
file: resolve(`dist/${name}.${format}.prod.js`),
format: outputConfigs[format].format,
})
}
Expand All @@ -354,21 +351,15 @@ export function createConfigsForPackage({
},
[
{
name: 'swc-minify',
async renderChunk(contents, _, { format }) {
const { code } = await minifySwc(contents, {
module: format === 'es',
format: {
comments: false,
},
name: 'oxc-minify',
async renderChunk(contents, _, { file }) {
// @ts-expect-error
const { code } = await minifyOxc(file, contents, {
mangle: true,
compress: {
ecma: 2016,
pure_getters: true,
target: 'es2016',
},
safari10: true,
mangle: true,
})
// swc removes banner
return { code: banner + code, map: null }
},
},
Expand Down
45 changes: 26 additions & 19 deletions scripts/inline-enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ function evaluate(exp) {
return new Function(`return ${exp}`)()
}

/**
* @param {import('oxc-parser').Expression | import('oxc-parser').PrivateIdentifier} exp
* @returns { exp is import('oxc-parser').StringLiteral | import('oxc-parser').NumericLiteral }
*/
function isStringOrNumberLiteral(exp) {
return (
exp.type === 'Literal' &&
(typeof exp.value === 'string' || typeof exp.value === 'number')
)
}

// this is called in the build script entry once
// so the data can be shared across concurrent Rollup processes
export function scanEnums() {
Expand All @@ -65,9 +76,7 @@ export function scanEnums() {
for (const relativeFile of files) {
const file = path.resolve(process.cwd(), relativeFile)
const content = readFileSync(file, 'utf-8')
const res = parseSync(content, {
// plugins: ['typescript'],
sourceFilename: file,
const res = parseSync(file, content, {
sourceType: 'module',
})

Expand Down Expand Up @@ -99,9 +108,16 @@ export function scanEnums() {
/** @type {Array<EnumMember>} */
const members = []

for (let i = 0; i < decl.members.length; i++) {
const e = decl.members[i]
const key = e.id.type === 'Identifier' ? e.id.name : e.id.value
for (let i = 0; i < decl.body.members.length; i++) {
const e = decl.body.members[i]
const key =
e.id.type === 'Identifier'
? e.id.name
: e.id.type === 'Literal'
? e.id.value
: ''
if (key === '') continue

const fullKey = /** @type {const} */ (`${id}.${key}`)
const saveValue = (/** @type {string | number} */ value) => {
// We need allow same name enum in different file.
Expand All @@ -120,23 +136,17 @@ export function scanEnums() {
if (init) {
/** @type {string | number} */
let value
if (
init.type === 'StringLiteral' ||
init.type === 'NumericLiteral'
) {
if (isStringOrNumberLiteral(init)) {
value = init.value
}
// e.g. 1 << 2
else if (init.type === 'BinaryExpression') {
const resolveValue = (
/** @type {import('@babel/types').Expression | import('@babel/types').PrivateName} */ node,
/** @type {import('oxc-parser').Expression | import('oxc-parser').PrivateIdentifier} */ node,
) => {
assert.ok(typeof node.start === 'number')
assert.ok(typeof node.end === 'number')
if (
node.type === 'NumericLiteral' ||
node.type === 'StringLiteral'
) {
if (isStringOrNumberLiteral(node)) {
return node.value
} else if (
node.type === 'MemberExpression' ||
Expand All @@ -163,10 +173,7 @@ export function scanEnums() {
}${resolveValue(init.right)}`
value = evaluate(exp)
} else if (init.type === 'UnaryExpression') {
if (
init.argument.type === 'StringLiteral' ||
init.argument.type === 'NumericLiteral'
) {
if (isStringOrNumberLiteral(init.argument)) {
const exp = `${init.operator}${init.argument.value}`
value = evaluate(exp)
} else {
Expand Down
Loading
Loading