Skip to content

Commit 5f019f3

Browse files
committed
feat: add run command
1 parent 3e44e48 commit 5f019f3

File tree

4 files changed

+96
-36
lines changed

4 files changed

+96
-36
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"author": "EGOIST",
1010
"license": "MIT",
1111
"scripts": {
12-
"build:esbuild": "rm -rf dist && esbuild src/cli.ts --platform=node --outdir=dist --format=cjs --target=es2018 --external:rollup --external:rollup-plugin-esbuild --external:typescript --bundle && chmod +x dist/cli.js",
12+
"build": "rm -rf dist && esbuild src/cli.ts --platform=node --outdir=dist --format=cjs --target=es2018 --external:rollup --external:rollup-plugin-esbuild --external:typescript --bundle && chmod +x dist/cli.js",
1313
"prepublishOnly": "npm run build",
1414
"test": "npm run build && jest"
1515
},

src/cli.ts

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,18 @@ cli
2727
})
2828
.action(async (files: string[], options) => {
2929
const { rollup, watch } = await import('rollup')
30-
const { default: hashbangPlugin } = await import('rollup-plugin-hashbang')
31-
const { default: esbuildPlugin } = await import('rollup-plugin-esbuild')
32-
const { default: commonjsPlugin } = await import('@rollup/plugin-commonjs')
33-
const { resolvePlugin } = await import('./resolve-plugin')
34-
const { default: dtsPlugin } = await import('rollup-plugin-dts')
35-
36-
const getRollupConfig = ({ dts }: { dts?: boolean }) => {
37-
return {
38-
inputConfig: {
39-
input: files,
40-
plugins: [
41-
hashbangPlugin(),
42-
resolvePlugin({ bundle: options.bundle }),
43-
commonjsPlugin(),
44-
!dts &&
45-
esbuildPlugin({
46-
target: options.target,
47-
watch: options.watch,
48-
minify: options.minify,
49-
jsxFactory: options.jsxFactory,
50-
jsxFragment: options.jsxFragment,
51-
}),
52-
dts && dtsPlugin(),
53-
].filter(Boolean),
54-
},
55-
outputConfig: {
56-
dir: options.outDir,
57-
format: options.format,
58-
},
59-
}
60-
}
61-
const rollupConfigs = [
62-
getRollupConfig({}),
63-
options.dts && getRollupConfig({ dts: true }),
64-
].filter(Boolean)
30+
const { createRollupConfigs } = await import('./')
31+
const rollupConfigs = await createRollupConfigs(files, {
32+
watch: options.watch,
33+
minify: options.minify,
34+
jsxFragment: options.jsxFragment,
35+
jsxFactory: options.jsxFactory,
36+
format: options.format,
37+
target: options.target,
38+
dts: options.dts,
39+
bundle: options.bundle,
40+
outDir: options.outDir,
41+
})
6542
if (options.watch) {
6643
const watcher = watch(
6744
rollupConfigs.map((config) => ({
@@ -86,6 +63,26 @@ cli
8663
}
8764
})
8865

66+
cli
67+
.command('run <file>', 'Bundle and execute a file', {
68+
allowUnknownOptions: true,
69+
})
70+
.action(async (file: string) => {
71+
const extraArgs = process.argv.slice(process.argv.indexOf(file) + 1)
72+
const { rollup } = await import('rollup')
73+
const { createRollupConfigs } = await import('./')
74+
const { runCode } = await import('./run')
75+
const [rollupConfig] = await createRollupConfigs([file], {
76+
outDir: 'dist',
77+
format: 'cjs',
78+
})
79+
const bundle = await rollup(rollupConfig.inputConfig)
80+
const { output } = await bundle.write(rollupConfig.outputConfig)
81+
runCode(join('dist', output[0].fileName), {
82+
args: extraArgs,
83+
})
84+
})
85+
8986
cli.help()
9087

9188
const pkgPath = join(__dirname, '../package.json')

src/index.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { ModuleFormat } from 'rollup'
2+
import { Target as EsbuildTarget } from 'esbuild'
3+
import hashbangPlugin from 'rollup-plugin-hashbang'
4+
import esbuildPlugin from 'rollup-plugin-esbuild'
5+
import commonjsPlugin from '@rollup/plugin-commonjs'
6+
import dtsPlugin from 'rollup-plugin-dts'
7+
import { resolvePlugin } from './resolve-plugin'
8+
9+
type Options = {
10+
bundle?: boolean
11+
dts?: boolean
12+
target?: EsbuildTarget
13+
watch?: boolean
14+
minify?: boolean
15+
jsxFactory?: string
16+
jsxFragment?: string
17+
outDir: string
18+
format: ModuleFormat
19+
}
20+
21+
export async function createRollupConfigs(files: string[], options: Options) {
22+
const getRollupConfig = ({ dts }: { dts?: boolean }) => {
23+
return {
24+
inputConfig: {
25+
input: files,
26+
plugins: [
27+
hashbangPlugin(),
28+
resolvePlugin({ bundle: options.bundle }),
29+
commonjsPlugin(),
30+
!dts &&
31+
esbuildPlugin({
32+
target: options.target,
33+
watch: options.watch,
34+
minify: options.minify,
35+
jsxFactory: options.jsxFactory,
36+
jsxFragment: options.jsxFragment,
37+
}),
38+
dts && dtsPlugin(),
39+
].filter(Boolean),
40+
},
41+
outputConfig: {
42+
dir: options.outDir,
43+
format: options.format,
44+
},
45+
}
46+
}
47+
const rollupConfigs = [getRollupConfig({})]
48+
49+
if (options.dts) {
50+
rollupConfigs.push(getRollupConfig({ dts: true }))
51+
}
52+
53+
return rollupConfigs
54+
}

src/run.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {spawn} from 'child_process'
2+
3+
export function runCode(filename: string, {
4+
args
5+
}: {args: string[]}) {
6+
spawn('node', [filename, ...args], {
7+
stdio: 'inherit'
8+
})
9+
}

0 commit comments

Comments
 (0)