first stab at supporting Rollup (#130)

This commit is contained in:
Rich Harris
2018-08-25 12:42:27 -04:00
parent 24f2855f89
commit 9cbb8bdc33
11 changed files with 276 additions and 107 deletions

View File

@@ -5,11 +5,10 @@ import * as child_process from 'child_process';
import * as ports from 'port-authority';
import mkdirp from 'mkdirp';
import rimraf from 'rimraf';
import format_messages from 'webpack-format-messages';
import { locations } from '../config';
import { EventEmitter } from 'events';
import { create_routes, create_main_manifests, create_compilers, create_serviceworker_manifest } from '../core';
import { Compiler, Compilers } from '../core/create_compilers';
import { Compiler, Compilers, CompileResult, CompileError } from '../core/create_compilers';
import Deferred from './utils/Deferred';
import * as events from './interfaces';
@@ -49,17 +48,19 @@ class Watcher extends EventEmitter {
dest = locations.dest(),
routes = locations.routes(),
webpack = 'webpack',
rollup = 'rollup',
port = +process.env.PORT
}: {
app: string,
dest: string,
routes: string,
webpack: string,
rollup: string,
port: number
}) {
super();
this.dirs = { app, dest, routes, webpack };
this.dirs = { app, dest, routes, webpack, rollup };
this.port = port;
this.closed = false;
@@ -181,7 +182,7 @@ class Watcher extends EventEmitter {
this.deferreds.server = new Deferred();
},
result: info => {
handle_result: (result: CompileResult) => {
this.deferreds.client.promise.then(() => {
const restart = () => {
log = '';
@@ -263,11 +264,11 @@ class Watcher extends EventEmitter {
// quite difficult
},
result: info => {
fs.writeFileSync(path.join(dest, 'client_assets.json'), JSON.stringify(info.assetsByChunkName, null, ' '));
handle_result: (result: CompileResult) => {
fs.writeFileSync(path.join(dest, 'client_assets.json'), JSON.stringify(result.assetsByChunkName, null, ' '));
this.deferreds.client.fulfil();
const client_files = info.assets.map((chunk: { name: string }) => `client/${chunk.name}`);
const client_files = result.assets.map((chunk: { name: string }) => `client/${chunk.name}`);
create_serviceworker_manifest({
routes: create_routes(),
@@ -285,11 +286,7 @@ class Watcher extends EventEmitter {
watch_serviceworker = noop;
this.watch(compilers.serviceworker, {
name: 'service worker',
result: info => {
fs.writeFileSync(path.join(dest, 'serviceworker_info.json'), JSON.stringify(info, null, ' '));
}
name: 'service worker'
});
}
: noop;
@@ -336,80 +333,34 @@ class Watcher extends EventEmitter {
}
}
watch(compiler: Compiler, { name, invalid = noop, result }: {
watch(compiler: Compiler, { name, invalid = noop, handle_result = noop }: {
name: string,
invalid?: (filename: string) => void;
result: (stats: any) => void;
handle_result?: (result: CompileResult) => void;
}) {
compiler.oninvalid(invalid);
compiler.watch((err: Error, stats: any) => {
compiler.watch((err?: Error, result?: CompileResult) => {
if (err) {
this.emit('error', <events.ErrorEvent>{
type: name,
message: err.message
});
} else {
const messages = format_messages(stats);
const info = stats.toJson();
this.emit('build', {
type: name,
duration: info.time,
errors: messages.errors.map((message: string) => {
const duplicate = this.current_build.unique_errors.has(message);
this.current_build.unique_errors.add(message);
return mungeWebpackError(message, duplicate);
}),
warnings: messages.warnings.map((message: string) => {
const duplicate = this.current_build.unique_warnings.has(message);
this.current_build.unique_warnings.add(message);
return mungeWebpackError(message, duplicate);
}),
duration: result.duration,
errors: result.errors,
warnings: result.warnings
});
result(info);
handle_result(result);
}
});
}
}
const locPattern = /\((\d+):(\d+)\)$/;
function mungeWebpackError(message: string, duplicate: boolean) {
// TODO this is all a bit rube goldberg...
const lines = message.split('\n');
const file = lines.shift()
.replace('', '') // careful — there is a special character at the beginning of this string
.replace('', '')
.replace('./', '');
let line = null;
let column = null;
const match = locPattern.exec(lines[0]);
if (match) {
lines[0] = lines[0].replace(locPattern, '');
line = +match[1];
column = +match[2];
}
return {
file,
line,
column,
message: lines.join('\n'),
originalMessage: message,
duplicate
};
}
const INTERVAL = 10000;
class DevServer {
@@ -466,7 +417,7 @@ function noop() {}
function watch_dir(
dir: string,
filter: ({ path, stats }: { path: string, stats: fs.Stats }) => boolean,
filter: ({ path, stats }: { path: string, stats: fs.CompileResult }) => boolean,
callback: () => void
) {
let watch;