From 5f019f38bc1f969356f7ac8bd01d9fc313b74c7c Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Wed, 13 May 2020 23:01:14 +0800 Subject: [PATCH] feat: add run command --- package.json | 2 +- src/cli.ts | 67 +++++++++++++++++++++++++--------------------------- src/index.ts | 54 ++++++++++++++++++++++++++++++++++++++++++ src/run.ts | 9 +++++++ 4 files changed, 96 insertions(+), 36 deletions(-) create mode 100644 src/index.ts create mode 100644 src/run.ts diff --git a/package.json b/package.json index e26079bc0..7546c04d7 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/cli.ts b/src/cli.ts index ce16acf41..3dd2fbf83 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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) => ({ @@ -86,6 +63,26 @@ cli } }) +cli + .command('run ', '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') diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 000000000..58e2dba92 --- /dev/null +++ b/src/index.ts @@ -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 +} diff --git a/src/run.ts b/src/run.ts new file mode 100644 index 000000000..98ce16c59 --- /dev/null +++ b/src/run.ts @@ -0,0 +1,9 @@ +import {spawn} from 'child_process' + +export function runCode(filename: string, { + args +}: {args: string[]}) { + spawn('node', [filename, ...args], { + stdio: 'inherit' + }) +} \ No newline at end of file