create a facade over webpack, to support alternative compilers

This commit is contained in:
Rich Harris
2018-08-25 09:11:45 -04:00
parent d5bf206d2a
commit 24f2855f89
4 changed files with 104 additions and 50 deletions

View File

@@ -5,6 +5,7 @@ import rimraf from 'rimraf';
import { EventEmitter } from 'events';
import minify_html from './utils/minify_html';
import { create_compilers, create_main_manifests, create_routes, create_serviceworker_manifest } from '../core';
import { Compilers, Compiler } from '../core/create_compilers';
import * as events from './interfaces';
export function build(opts: {}) {
@@ -28,6 +29,7 @@ async function execute(emitter: EventEmitter, {
dest = 'build',
app = 'app',
webpack = 'webpack',
rollup = 'rollup',
routes = 'routes'
} = {}) {
mkdirp.sync(dest);
@@ -51,9 +53,9 @@ async function execute(emitter: EventEmitter, {
// create app/manifest/client.js and app/manifest/server.js
create_main_manifests({ routes: route_objects });
const { client, server, serviceworker } = create_compilers({ webpack });
const { client, server, serviceworker } = create_compilers({ webpack, rollup });
const client_stats = await compile(client);
const client_stats = await client.compile();
emitter.emit('build', <events.BuildEvent>{
type: 'client',
// TODO duration/warnings
@@ -63,7 +65,7 @@ async function execute(emitter: EventEmitter, {
const client_info = client_stats.toJson();
fs.writeFileSync(path.join(dest, 'client_assets.json'), JSON.stringify(client_info.assetsByChunkName));
const server_stats = await compile(server);
const server_stats = await server.compile();
emitter.emit('build', <events.BuildEvent>{
type: 'server',
// TODO duration/warnings
@@ -78,7 +80,7 @@ async function execute(emitter: EventEmitter, {
client_files: client_stats.toJson().assets.map((chunk: { name: string }) => `client/${chunk.name}`)
});
serviceworker_stats = await compile(serviceworker);
serviceworker_stats = await serviceworker.compile();
emitter.emit('build', <events.BuildEvent>{
type: 'serviceworker',
@@ -86,24 +88,4 @@ async function execute(emitter: EventEmitter, {
webpack_stats: serviceworker_stats
});
}
}
function compile(compiler: any) {
return new Promise((fulfil, reject) => {
compiler.run((err: Error, stats: any) => {
if (err) {
reject(err);
process.exit(1);
}
if (stats.hasErrors()) {
console.error(stats.toString({ colors: true }));
reject(new Error(`Encountered errors while building app`));
}
else {
fulfil(stats);
}
});
});
}
}

View File

@@ -9,6 +9,7 @@ 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 Deferred from './utils/Deferred';
import * as events from './interfaces';
@@ -155,7 +156,10 @@ class Watcher extends EventEmitter {
};
// TODO watch the configs themselves?
const compilers = create_compilers({ webpack: this.dirs.webpack });
const compilers: Compilers = create_compilers({
webpack: this.dirs.webpack,
rollup: this.dirs.rollup
});
let log = '';
@@ -332,16 +336,14 @@ class Watcher extends EventEmitter {
}
}
watch(compiler: any, { name, invalid = noop, result }: {
watch(compiler: Compiler, { name, invalid = noop, result }: {
name: string,
invalid?: (filename: string) => void;
result: (stats: any) => void;
}) {
compiler.hooks.invalid.tap('sapper', (filename: string) => {
invalid(filename);
});
compiler.oninvalid(invalid);
compiler.watch({}, (err: Error, stats: any) => {
compiler.watch((err: Error, stats: any) => {
if (err) {
this.emit('error', <events.ErrorEvent>{
type: name,