Skip to content

Commit 8a84782

Browse files
authored
Merge pull request #5 from rdmurphy/watch-mode
Watch mode
2 parents fb58ace + fd3d758 commit 8a84782

File tree

5 files changed

+62
-13
lines changed

5 files changed

+62
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added support for watch mode. Can be activated by passing the `--watch` in the command. When this mode is activated type declaration is _skipped_.
13+
1014
## [0.4.0] - 2019-08-19
1115

1216
### Added

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typebundle",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "A personalized build process for projects.",
55
"main": "dist/index.js",
66
"source": "src/index.js",
@@ -18,7 +18,8 @@
1818
"build:self": "node dist/cli.js \"src/{cli,index}.ts\" --target=8 --output=dist --compress --types=types",
1919
"prerelease": "npm run build",
2020
"release": "np",
21-
"test": "echo \"Error: no test specified\" && exit 0"
21+
"test": "echo \"Error: no test specified\" && exit 0",
22+
"watch": "npm run build:babel -- --watch --no-compress"
2223
},
2324
"repository": {
2425
"type": "git",

src/cli.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const mriConfig = {
1111
default: {
1212
compress: false,
1313
output: 'dist',
14+
watch: false,
1415
},
1516
};
1617

@@ -22,8 +23,16 @@ async function main(argv_: string[]) {
2223
const compress = args.compress;
2324
const nodeTarget = args.target;
2425
const typesDir = args.types;
26+
const watchBuild = args.watch;
2527

26-
await bundler({ compress, input, nodeTarget, outputDir, typesDir });
28+
await bundler({
29+
compress,
30+
input,
31+
nodeTarget,
32+
outputDir,
33+
typesDir,
34+
watchBuild,
35+
});
2736
}
2837

2938
main(process.argv).catch(console.error);

src/index.ts

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { basename, parse, resolve } from 'path';
66
import babelPresetEnv from '@babel/preset-env';
77
import babelPresetTypescript from '@babel/preset-typescript';
88
import builtinModules from 'builtin-modules';
9-
import { rollup, OutputOptions, InputOptions } from 'rollup';
9+
import { rollup, watch, OutputOptions, InputOptions } from 'rollup';
1010
import babel from 'rollup-plugin-babel';
1111
import commonjs from 'rollup-plugin-commonjs';
1212
import json from 'rollup-plugin-json';
@@ -149,6 +149,7 @@ interface BundlerOptions {
149149
nodeTarget: string;
150150
outputDir: string;
151151
typesDir?: string;
152+
watchBuild?: boolean;
152153
}
153154

154155
export async function bundler({
@@ -157,6 +158,7 @@ export async function bundler({
157158
nodeTarget,
158159
outputDir,
159160
typesDir,
161+
watchBuild,
160162
}: BundlerOptions) {
161163
// the current working directory
162164
const cwd = process.cwd();
@@ -171,11 +173,13 @@ export async function bundler({
171173
const pkgDependencies = Object.keys(pkg.dependencies || {});
172174

173175
// find all the input TypeScript files
174-
const inputs = await glob(input, { absolute: true });
176+
const inputs = await glob(input);
175177

176178
// if we have more than one input, flag this as a multi-input run
177179
const withMultipleInputs = inputs.length > 1;
178180

181+
const runs = [];
182+
179183
// loop thorugh the inputs, creating a rollup configuraion for each one
180184
for (let idx = 0; idx < inputs.length; idx++) {
181185
const input = inputs[idx];
@@ -184,7 +188,7 @@ export async function bundler({
184188
inputs.filter(e => e !== input)
185189
);
186190

187-
const { inputOptions, outputOptions } = await createRollupConfig({
191+
const options = await createRollupConfig({
188192
compress,
189193
externalDependencies,
190194
input,
@@ -194,13 +198,44 @@ export async function bundler({
194198
pkgMain: pkg.main,
195199
});
196200

197-
const bundle = await rollup(inputOptions);
198-
199-
for (let idx = 0; idx < outputOptions.length; idx++) {
200-
const output = outputOptions[idx];
201+
runs.push(options);
202+
}
201203

202-
await bundle.write(output);
203-
await createTypes({ input, output: typesDir });
204+
for (const { inputOptions, outputOptions } of runs) {
205+
if (watchBuild) {
206+
const watcher = watch(
207+
Object.assign(
208+
{
209+
output: outputOptions,
210+
watch: {
211+
exclude: 'node_modules/**',
212+
},
213+
},
214+
inputOptions
215+
)
216+
);
217+
218+
watcher.on('event', ({ code, error }) => {
219+
switch (code) {
220+
case 'FATAL':
221+
throw new Error(error);
222+
case 'ERROR':
223+
console.error(error);
224+
break;
225+
case 'END':
226+
console.log(`Successful build. (${inputOptions.input})`);
227+
break;
228+
}
229+
});
230+
} else {
231+
const bundle = await rollup(inputOptions);
232+
233+
for (let idx = 0; idx < outputOptions.length; idx++) {
234+
const output = outputOptions[idx];
235+
236+
await bundle.write(output);
237+
await createTypes({ input, output: typesDir });
238+
}
204239
}
205240
}
206241
}

0 commit comments

Comments
 (0)