Skip to content

Commit 1d60912

Browse files
authored
Add onStart and onError callbacks, accessible rollup watchers (#668)
1 parent 5f1a6a9 commit 1d60912

3 files changed

Lines changed: 31 additions & 19 deletions

File tree

src/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import logError from './log-error';
77

88
const run = opts => {
99
microbundle(opts)
10-
.then(output => {
10+
.then(({ output }) => {
1111
if (output != null) stdout(output);
1212
if (!opts.watch) process.exit(0);
1313
})

src/index.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ export default async function microbundle(inputOptions) {
211211
}
212212

213213
if (options.watch) {
214-
const onBuild = options.onBuild;
215-
return new Promise((resolve, reject) => {
214+
const { onStart, onBuild, onError } = options;
215+
return new Promise(resolve => {
216216
stdout(
217217
blue(
218218
`Watching source, compiling to ${relative(
@@ -221,8 +221,9 @@ export default async function microbundle(inputOptions) {
221221
)}:`,
222222
),
223223
);
224-
steps.map(options => {
225-
watch(
224+
225+
const watchers = steps.reduce((acc, options) => {
226+
acc[options.inputOptions.input] = watch(
226227
Object.assign(
227228
{
228229
output: options.outputOptions,
@@ -231,10 +232,16 @@ export default async function microbundle(inputOptions) {
231232
options.inputOptions,
232233
),
233234
).on('event', e => {
234-
if (e.code === 'FATAL') {
235-
return reject(e.error);
236-
} else if (e.code === 'ERROR') {
235+
if (e.code === 'START') {
236+
if (typeof onStart === 'function') {
237+
onStart(e);
238+
}
239+
}
240+
if (e.code === 'ERROR') {
237241
logError(e.error);
242+
if (typeof onError === 'function') {
243+
onError(e);
244+
}
238245
}
239246
if (e.code === 'END') {
240247
options._sizeInfo.then(text => {
@@ -245,7 +252,11 @@ export default async function microbundle(inputOptions) {
245252
}
246253
}
247254
});
248-
});
255+
256+
return acc;
257+
}, {});
258+
259+
resolve({ watchers });
249260
});
250261
}
251262

@@ -263,14 +274,15 @@ export default async function microbundle(inputOptions) {
263274
}),
264275
);
265276

266-
return (
267-
blue(
268-
`Build "${options.name}" to ${relative(cwd, dirname(options.output)) ||
269-
'.'}:`,
270-
) +
271-
'\n ' +
272-
out.join('\n ')
273-
);
277+
return {
278+
output:
279+
blue(
280+
`Build "${options.name}" to ${relative(cwd, dirname(options.output)) ||
281+
'.'}:`,
282+
) +
283+
'\n ' +
284+
out.join('\n '),
285+
};
274286
}
275287

276288
async function getConfigFromPkgJson(cwd) {

tools/build-fixture.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ export const buildDirectory = async fixtureDir => {
6060
process.chdir(resolve(fixturePath));
6161

6262
const parsedOpts = parseScript(script);
63-
let output = '';
64-
output = await microbundle({
63+
let { output } = await microbundle({
6564
...parsedOpts,
6665
cwd: parsedOpts.cwd !== '.' ? parsedOpts.cwd : resolve(fixturePath),
6766
});
67+
output = output || '';
6868

6969
process.chdir(prevDir);
7070

0 commit comments

Comments
 (0)