Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rolldown cli first implementation (#610)
* feat: rolldown cli first implementation * fix: switch to pnpm * chore: add node test files to ls-lint ignore * fix: add config loading * refactor: lazy jiti instance * refactor: error management * fix: move to packages/rolldown * fix: define return type of load config * fix: add more test case * fix: add config path normalization * fix: WIP building ... * chore: add ls lint * chore: add test case file to ls-lint ignore * chore: revert git ignore * fix: lint errors * fix: spell * fix: build implementation and test fixtures * fix: add cli dir to dist files * refactor to ts --------- Co-authored-by: Yunfei <[email protected]>
- Loading branch information
Showing
28 changed files
with
285 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,8 @@ | |
"tinybench", | ||
"transpiling", | ||
"treeshake", | ||
"consola", | ||
"citty", | ||
"underfin", | ||
"UNKEYED", | ||
"vite", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env node | ||
import('../dist/cli.mjs') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const ERR_UNSUPPORTED_CONFIG_FORMAT = | ||
"Unsupported config format. please use '.js', '.mjs' and '.ts' format" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import path from 'node:path' | ||
import { ERR_UNSUPPORTED_CONFIG_FORMAT } from './errors.js' | ||
import { RolldownConfigExport } from '../types/rolldown-config-export.js' | ||
|
||
/** | ||
* @typedef {import('../rollup').RollupOptions} RollupOptions | ||
*/ | ||
|
||
/** | ||
* Load a rolldown configuration file | ||
*/ | ||
export async function loadConfig( | ||
configPath: string, | ||
): Promise<RolldownConfigExport | undefined> { | ||
if (!isSupportedFormat(configPath)) { | ||
throw new Error(ERR_UNSUPPORTED_CONFIG_FORMAT) | ||
} | ||
return import(configPath).then((config) => config.default) | ||
} | ||
|
||
/** | ||
* Check whether the configuration file is supported | ||
*/ | ||
function isSupportedFormat(configPath: string): boolean { | ||
const ext = path.extname(configPath) | ||
return /\.(js|mjs)$/.test(ext) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
import { RolldownOutput } from './objects/rolldown-output' | ||
import type { InputOptions, RolldownPlugin } from './options/input-options' | ||
import type { OutputOptions } from './options/output-options' | ||
import type { RolldownOptions } from './types/rolldown-options' | ||
import { defineConfig } from './utils/define-config' | ||
|
||
export { rolldown, experimental_scan } from './rolldown' | ||
|
||
interface RollupOptions extends InputOptions { | ||
// This is included for compatibility with config files but ignored by rollup.rollup | ||
output?: OutputOptions | OutputOptions[] | ||
} | ||
export { defineConfig } | ||
|
||
// export types from rolldown | ||
export type { | ||
RollupOptions, | ||
RolldownOptions, | ||
RolldownOptions as RollupOptions, | ||
RolldownOutput, | ||
RolldownOutput as RollupOutput, | ||
InputOptions, | ||
OutputOptions, | ||
RolldownPlugin as Plugin, | ||
RolldownOutput as RollupOutput, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { RolldownOptions } from './rolldown-options' | ||
|
||
export type RolldownConfigExport = RolldownOptions | RolldownOptions[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { InputOptions } from '../options/input-options' | ||
import { OutputOptions } from '../options/output-options' | ||
|
||
export interface RolldownOptions extends InputOptions { | ||
// This is included for compatibility with config files but ignored by `rolldown.rolldown` | ||
output?: OutputOptions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { RolldownConfigExport } from '../types/rolldown-config-export' | ||
|
||
export function defineConfig( | ||
config: RolldownConfigExport, | ||
): RolldownConfigExport { | ||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { describe, test, assert } from 'vitest' | ||
import path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import { loadConfig } from '../../src/cli/utils' | ||
import { ERR_UNSUPPORTED_CONFIG_FORMAT } from '../../src/cli/errors' | ||
import { expect } from 'vitest' | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
|
||
describe('loadConfig', () => { | ||
const RE_ERR = RegExp(ERR_UNSUPPORTED_CONFIG_FORMAT) | ||
|
||
test('js', async () => { | ||
const config = await loadConfig( | ||
path.resolve(__dirname, 'fixtures/rolldown.config.js'), | ||
) | ||
assert.deepEqual(config, { input: 'src/index.js' }) | ||
}) | ||
|
||
test('mjs', async () => { | ||
const config = await loadConfig( | ||
path.resolve(__dirname, 'fixtures/rolldown.config.mjs'), | ||
) | ||
assert.deepEqual(config, [ | ||
{ input: 'src/app1/index.js' }, | ||
{ input: 'src/app2/index.js' }, | ||
]) | ||
}) | ||
|
||
test('cjs', async () => { | ||
await expect( | ||
loadConfig(path.resolve(__dirname, 'fixtures/rolldown.config.cjs')), | ||
).rejects.toThrowError(RE_ERR) | ||
}) | ||
|
||
test('other format', async () => { | ||
await expect( | ||
loadConfig(path.resolve(__dirname, 'fixtures/rolldown.config.json')), | ||
).rejects.toThrowError(RE_ERR) | ||
}) | ||
|
||
test('not found file', async () => { | ||
await expect( | ||
loadConfig(path.join(__dirname, 'fixtures/rollup.config.js')), | ||
).rejects.toThrowError(Error) | ||
}) | ||
}) |
24 changes: 24 additions & 0 deletions
24
packages/rolldown/test/cli/fixtures/basic/rolldown.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
|
||
export default { | ||
// input: 'src/index.js', | ||
input: path.resolve(__dirname, 'src/index.js'), | ||
output: [ | ||
{ | ||
// dir: 'build', | ||
// file: 'build/bundle.js', | ||
dir: path.resolve(__dirname, 'build'), | ||
file: path.resolve(__dirname, 'build/bundle.js'), | ||
}, | ||
], | ||
resolve: { | ||
conditionNames: ['import'], | ||
alias: { | ||
// modules: 'src/modules', | ||
modules: path.resolve(__dirname, 'src/modules'), | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function add(a, b) { | ||
return a + b | ||
} | ||
|
||
export function sub(a, b) { | ||
return a - b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { add, sub } from './calc' | ||
import { say } from 'modules/say' | ||
|
||
console.log(add(1, 2)) // 3 | ||
console.log(sub(1, 2)) // -1 | ||
|
||
say('hello world!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function say(msg) { | ||
console.log(msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
input: 'src/index.js', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
input: 'src/index.js', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"input": "src/index.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default [ | ||
{ | ||
input: 'src/app1/index.js', | ||
}, | ||
{ | ||
input: 'src/app2/index.js', | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { transform } from './transform' | ||
|
||
export default { | ||
input: 'src/index.ts', | ||
plugins: [ | ||
{ | ||
name: 'test-plugin', | ||
transform, | ||
}, | ||
], | ||
} |
Oops, something went wrong.