Skip to content

Commit

Permalink
refactor to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Mar 21, 2024
1 parent 3032023 commit 7d8fa5b
Show file tree
Hide file tree
Showing 32 changed files with 203 additions and 323 deletions.
2 changes: 0 additions & 2 deletions .ls-lint.yml
Expand Up @@ -13,8 +13,6 @@ ignore:
- '**/*.test.{js,mjs,cjs,ts,cts,mts}'
- packages/rollup-tests/test # Copied from rollup repo
- 'crates/rolldown/tests/fixtures/**/_test.js' # `_test.js` is a special file, which will be treat as the entry file while executing the build output of fixture.
- packages/rolldown/build.config.ts # convention name for using unbuild
- packages/rolldown/vitest.config.ts
- web/docs/.vitepress/cache # cache files generated by vitepress
# FIXME: should not ignore following folders
- web
2 changes: 0 additions & 2 deletions cspell.json
Expand Up @@ -100,8 +100,6 @@
"transpiling",
"treeshake",
"consola",
"jiti",
"JITI",
"citty",
"underfin",
"UNKEYED",
Expand Down
@@ -1,9 +1,6 @@
import { performance } from 'node:perf_hooks'
import * as rolldown from 'rolldown'
import { defineConfig } from 'rolldown'

const start = performance.now()

const build = await rolldown.rolldown({
export default defineConfig({
input: './index.js',
resolve: {
// This needs to be explicitly set for now because oxc resolver doesn't
Expand All @@ -12,7 +9,3 @@ const build = await rolldown.rolldown({
conditionNames: ['import'],
},
})

await build.write()

console.log(`bundled in ${(performance.now() - start).toFixed(2)}ms`)
2 changes: 1 addition & 1 deletion packages/rolldown/bin/cli.js
@@ -1,2 +1,2 @@
#!/usr/bin/env node
import('../cli/main.js')
import('../dist/cli.mjs')
14 changes: 11 additions & 3 deletions packages/rolldown/build.config.ts
Expand Up @@ -5,13 +5,21 @@ import nodePath from 'node:path'
import { globSync } from 'glob'

export default defineBuildConfig({
entries: ['./src/index'],
entries: [
'./src/index',
{
builder: 'rollup',
input: './src/cli/index',
name: 'cli',
},
],
clean: true,
declaration: true, // generate .d.ts files
externals: [/rolldown-binding\..*\.node/, /@rolldown\/binding-.*/],
rollup: {
emitCJS: true,
cjsBridge: true,
inlineDependencies: true,
},
hooks: {
'build:done'(_ctx) {
Expand All @@ -25,8 +33,8 @@ export default defineBuildConfig({
// Move the binary file to dist
binaryFiles.forEach((file) => {
const fileName = nodePath.basename(file)
console.log('Copying', file, 'to ./dist')
nodeFs.copyFileSync(file, `./dist/${fileName}`)
console.log('Copying', file, 'to ./dist/shared')
nodeFs.copyFileSync(file, `./dist/shared/${fileName}`)
})
},
},
Expand Down
74 changes: 0 additions & 74 deletions packages/rolldown/cli/build.js

This file was deleted.

70 changes: 0 additions & 70 deletions packages/rolldown/cli/config.js

This file was deleted.

46 changes: 0 additions & 46 deletions packages/rolldown/cli/main.js

This file was deleted.

8 changes: 2 additions & 6 deletions packages/rolldown/package.json
Expand Up @@ -21,7 +21,6 @@
"bin": {
"rolldown": "./bin/cli.js"
},
"type": "module",
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -67,14 +66,11 @@
"armv7-linux-androideabi"
]
},
"dependencies": {
"citty": "^0.1.6",
"consola": "^3.2.3",
"jiti": "^1.21.0"
},
"devDependencies": {
"@napi-rs/cli": "^3.0.0-alpha.43",
"citty": "^0.1.6",
"colorette": "^2.0.20",
"consola": "^3.2.3",
"glob": "^10.3.10",
"rollup": "^4.12.1",
"type-fest": "^4.12.0",
Expand Down
26 changes: 26 additions & 0 deletions packages/rolldown/src/cli/commands/bundle.ts
@@ -0,0 +1,26 @@
import { performance } from 'node:perf_hooks'
import consola from 'consola'
import { colors } from 'consola/utils'
import { RolldownOptions, rolldown } from '../../index'
import { RolldownConfigExport } from '../../types/rolldown-config-export'
import { arraify } from '../../utils'

export async function bundle(configExport: RolldownConfigExport) {
const options = arraify(configExport)

for (const option of options) {
await bundleInner(option)
}
}

async function bundleInner(options: RolldownOptions) {
const start = performance.now()

const build = await rolldown(options)

const _output = await build.write(options?.output)

consola.log(
`Finished in ${colors.bold((performance.now() - start).toFixed(2))} ms`,
)
}
File renamed without changes.
52 changes: 52 additions & 0 deletions packages/rolldown/src/cli/index.ts
@@ -0,0 +1,52 @@
import { defineCommand, runMain, showUsage } from 'citty'
import consola from 'consola'
import process from 'node:process'
import path from 'node:path'
import pkgJson from '../../package.json' assert { type: 'json' }
import { loadConfig } from './utils.js'
import { bundle } from './commands/bundle'

const main = defineCommand({
meta: {
name: 'rolldown',
version: pkgJson.version,
description: pkgJson.description,
},
args: {
config: {
type: 'string',
alias: 'c',
description:
'Use this config file (if argument is used but value is unspecified, defaults to rolldown.config.js)',
},
help: {
type: 'boolean',
alias: 'h',
description: 'Show this help message',
},
},
async run(ctx) {
if (ctx.args.help) {
await showUsage(ctx.cmd)
return
}
const cwd = process.cwd()
let configPath
if (ctx.args.config) {
configPath = path.resolve(cwd, ctx.args.config)
} else {
configPath = path.resolve(cwd, 'rolldown.config.js')
}

const config = await loadConfig(configPath)

if (!config) {
consola.error(`No configuration found at ${configPath}`)
process.exit(1)
}

await bundle(config)
},
})

runMain(main)

0 comments on commit 7d8fa5b

Please sign in to comment.