Skip to content

Commit

Permalink
Rough support for watch builds
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmurphy committed Aug 20, 2019
1 parent c7798e5 commit ba23ac9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 10 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const mriConfig = {
default: {
compress: false,
output: 'dist',
watch: false,
},
};

Expand All @@ -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);
53 changes: 44 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -149,6 +149,7 @@ interface BundlerOptions {
nodeTarget: string;
outputDir: string;
typesDir?: string;
watchBuild?: boolean;
}

export async function bundler({
Expand All @@ -157,6 +158,7 @@ export async function bundler({
nodeTarget,
outputDir,
typesDir,
watchBuild,
}: BundlerOptions) {
// the current working directory
const cwd = process.cwd();
Expand All @@ -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];
Expand All @@ -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,
Expand All @@ -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 });
}
}
}
}

0 comments on commit ba23ac9

Please sign in to comment.