diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fe849a7..fe471e45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Added support for watch mode. Can be activated by passing the `--watch` in the command. When this mode is activated type declaration is _skipped_. + ## [0.4.0] - 2019-08-19 ### Added diff --git a/package-lock.json b/package-lock.json index 18a04517..f7c878e0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "typebundle", - "version": "0.3.0", + "version": "0.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index fdf9ff09..ba8e2b0f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typebundle", - "version": "0.3.0", + "version": "0.4.0", "description": "A personalized build process for projects.", "main": "dist/index.js", "source": "src/index.js", @@ -18,7 +18,8 @@ "build:self": "node dist/cli.js \"src/{cli,index}.ts\" --target=8 --output=dist --compress --types=types", "prerelease": "npm run build", "release": "np", - "test": "echo \"Error: no test specified\" && exit 0" + "test": "echo \"Error: no test specified\" && exit 0", + "watch": "npm run build:babel -- --watch --no-compress" }, "repository": { "type": "git", diff --git a/src/cli.ts b/src/cli.ts index 2b71c804..dd3fa439 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -11,6 +11,7 @@ const mriConfig = { default: { compress: false, output: 'dist', + watch: false, }, }; @@ -22,8 +23,16 @@ async function main(argv_: string[]) { const compress = args.compress; const nodeTarget = args.target; const typesDir = args.types; + const watchBuild = args.watch; - await bundler({ compress, input, nodeTarget, outputDir, typesDir }); + await bundler({ + compress, + input, + nodeTarget, + outputDir, + typesDir, + watchBuild, + }); } main(process.argv).catch(console.error); diff --git a/src/index.ts b/src/index.ts index 0d90bc0f..fb3ad083 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import { basename, parse, resolve } from 'path'; import babelPresetEnv from '@babel/preset-env'; import babelPresetTypescript from '@babel/preset-typescript'; import builtinModules from 'builtin-modules'; -import { rollup, OutputOptions, InputOptions } from 'rollup'; +import { rollup, watch, OutputOptions, InputOptions } from 'rollup'; import babel from 'rollup-plugin-babel'; import commonjs from 'rollup-plugin-commonjs'; import json from 'rollup-plugin-json'; @@ -149,6 +149,7 @@ interface BundlerOptions { nodeTarget: string; outputDir: string; typesDir?: string; + watchBuild?: boolean; } export async function bundler({ @@ -157,6 +158,7 @@ export async function bundler({ nodeTarget, outputDir, typesDir, + watchBuild, }: BundlerOptions) { // the current working directory const cwd = process.cwd(); @@ -171,11 +173,13 @@ export async function bundler({ const pkgDependencies = Object.keys(pkg.dependencies || {}); // find all the input TypeScript files - const inputs = await glob(input, { absolute: true }); + const inputs = await glob(input); // if we have more than one input, flag this as a multi-input run const withMultipleInputs = inputs.length > 1; + const runs = []; + // loop thorugh the inputs, creating a rollup configuraion for each one for (let idx = 0; idx < inputs.length; idx++) { const input = inputs[idx]; @@ -184,7 +188,7 @@ export async function bundler({ inputs.filter(e => e !== input) ); - const { inputOptions, outputOptions } = await createRollupConfig({ + const options = await createRollupConfig({ compress, externalDependencies, input, @@ -194,13 +198,44 @@ export async function bundler({ pkgMain: pkg.main, }); - const bundle = await rollup(inputOptions); - - for (let idx = 0; idx < outputOptions.length; idx++) { - const output = outputOptions[idx]; + runs.push(options); + } - await bundle.write(output); - await createTypes({ input, output: typesDir }); + for (const { inputOptions, outputOptions } of runs) { + if (watchBuild) { + const watcher = watch( + Object.assign( + { + output: outputOptions, + watch: { + exclude: 'node_modules/**', + }, + }, + inputOptions + ) + ); + + watcher.on('event', ({ code, error }) => { + switch (code) { + case 'FATAL': + throw new Error(error); + case 'ERROR': + console.error(error); + break; + case 'END': + console.log(`Successful build. (${inputOptions.input})`); + break; + } + }); + } else { + const bundle = await rollup(inputOptions); + + for (let idx = 0; idx < outputOptions.length; idx++) { + const output = outputOptions[idx]; + + await bundle.write(output); + await createTypes({ input, output: typesDir }); + } } } }