Skip to content

Commit

Permalink
feat: add run command
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed May 13, 2020
1 parent 3e44e48 commit 5f019f3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"author": "EGOIST",
"license": "MIT",
"scripts": {
"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",
"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",
"prepublishOnly": "npm run build",
"test": "npm run build && jest"
},
Expand Down
67 changes: 32 additions & 35 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,18 @@ cli
})
.action(async (files: string[], options) => {
const { rollup, watch } = await import('rollup')
const { default: hashbangPlugin } = await import('rollup-plugin-hashbang')
const { default: esbuildPlugin } = await import('rollup-plugin-esbuild')
const { default: commonjsPlugin } = await import('@rollup/plugin-commonjs')
const { resolvePlugin } = await import('./resolve-plugin')
const { default: dtsPlugin } = await import('rollup-plugin-dts')

const getRollupConfig = ({ dts }: { dts?: boolean }) => {
return {
inputConfig: {
input: files,
plugins: [
hashbangPlugin(),
resolvePlugin({ bundle: options.bundle }),
commonjsPlugin(),
!dts &&
esbuildPlugin({
target: options.target,
watch: options.watch,
minify: options.minify,
jsxFactory: options.jsxFactory,
jsxFragment: options.jsxFragment,
}),
dts && dtsPlugin(),
].filter(Boolean),
},
outputConfig: {
dir: options.outDir,
format: options.format,
},
}
}
const rollupConfigs = [
getRollupConfig({}),
options.dts && getRollupConfig({ dts: true }),
].filter(Boolean)
const { createRollupConfigs } = await import('./')
const rollupConfigs = await createRollupConfigs(files, {
watch: options.watch,
minify: options.minify,
jsxFragment: options.jsxFragment,
jsxFactory: options.jsxFactory,
format: options.format,
target: options.target,
dts: options.dts,
bundle: options.bundle,
outDir: options.outDir,
})
if (options.watch) {
const watcher = watch(
rollupConfigs.map((config) => ({
Expand All @@ -86,6 +63,26 @@ cli
}
})

cli
.command('run <file>', 'Bundle and execute a file', {
allowUnknownOptions: true,
})
.action(async (file: string) => {
const extraArgs = process.argv.slice(process.argv.indexOf(file) + 1)
const { rollup } = await import('rollup')
const { createRollupConfigs } = await import('./')
const { runCode } = await import('./run')
const [rollupConfig] = await createRollupConfigs([file], {
outDir: 'dist',
format: 'cjs',
})
const bundle = await rollup(rollupConfig.inputConfig)
const { output } = await bundle.write(rollupConfig.outputConfig)
runCode(join('dist', output[0].fileName), {
args: extraArgs,
})
})

cli.help()

const pkgPath = join(__dirname, '../package.json')
Expand Down
54 changes: 54 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { ModuleFormat } from 'rollup'
import { Target as EsbuildTarget } from 'esbuild'
import hashbangPlugin from 'rollup-plugin-hashbang'
import esbuildPlugin from 'rollup-plugin-esbuild'
import commonjsPlugin from '@rollup/plugin-commonjs'
import dtsPlugin from 'rollup-plugin-dts'
import { resolvePlugin } from './resolve-plugin'

type Options = {
bundle?: boolean
dts?: boolean
target?: EsbuildTarget
watch?: boolean
minify?: boolean
jsxFactory?: string
jsxFragment?: string
outDir: string
format: ModuleFormat
}

export async function createRollupConfigs(files: string[], options: Options) {
const getRollupConfig = ({ dts }: { dts?: boolean }) => {
return {
inputConfig: {
input: files,
plugins: [
hashbangPlugin(),
resolvePlugin({ bundle: options.bundle }),
commonjsPlugin(),
!dts &&
esbuildPlugin({
target: options.target,
watch: options.watch,
minify: options.minify,
jsxFactory: options.jsxFactory,
jsxFragment: options.jsxFragment,
}),
dts && dtsPlugin(),
].filter(Boolean),
},
outputConfig: {
dir: options.outDir,
format: options.format,
},
}
}
const rollupConfigs = [getRollupConfig({})]

if (options.dts) {
rollupConfigs.push(getRollupConfig({ dts: true }))
}

return rollupConfigs
}
9 changes: 9 additions & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {spawn} from 'child_process'

export function runCode(filename: string, {
args
}: {args: string[]}) {
spawn('node', [filename, ...args], {
stdio: 'inherit'
})
}

0 comments on commit 5f019f3

Please sign in to comment.