From 85e25d6380ad996f41e34ee595ecb889586b5ac0 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 28 Aug 2018 17:29:14 -0400 Subject: [PATCH] add a --bundler option, for forcing rollup or webpack --- src/api/build.ts | 4 +++- src/api/dev.ts | 8 +++++++- src/api/export.ts | 2 +- src/cli/build.ts | 8 ++++++-- src/cli/dev.ts | 2 +- src/cli/export.ts | 2 +- src/cli/utils/validate_bundler.ts | 21 +++++++++++++++++++++ src/core/create_compilers.ts | 9 +++++---- 8 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 src/cli/utils/validate_bundler.ts diff --git a/src/api/build.ts b/src/api/build.ts index 79289d1..152dc43 100644 --- a/src/api/build.ts +++ b/src/api/build.ts @@ -7,6 +7,7 @@ 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'; +import validate_bundler from '../cli/utils/validate_bundler'; export function build(opts: {}) { const emitter = new EventEmitter(); @@ -28,6 +29,7 @@ export function build(opts: {}) { async function execute(emitter: EventEmitter, { dest = 'build', app = 'app', + bundler, webpack = 'webpack', rollup = 'rollup', routes = 'routes' @@ -53,7 +55,7 @@ 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, rollup }); + const { client, server, serviceworker } = create_compilers(validate_bundler(bundler), { webpack, rollup }); const client_result = await client.compile(); emitter.emit('build', { diff --git a/src/api/dev.ts b/src/api/dev.ts index 44a9ef9..49fbab2 100644 --- a/src/api/dev.ts +++ b/src/api/dev.ts @@ -11,16 +11,19 @@ import { create_routes, create_main_manifests, create_compilers, create_servicew import { Compiler, Compilers, CompileResult, CompileError } from '../core/create_compilers'; import Deferred from './utils/Deferred'; import * as events from './interfaces'; +import validate_bundler from '../cli/utils/validate_bundler'; export function dev(opts) { return new Watcher(opts); } class Watcher extends EventEmitter { + bundler: string; dirs: { app: string; dest: string; routes: string; + rollup: string; webpack: string; } port: number; @@ -47,6 +50,7 @@ class Watcher extends EventEmitter { app = locations.app(), dest = locations.dest(), routes = locations.routes(), + bundler, webpack = 'webpack', rollup = 'rollup', port = +process.env.PORT @@ -54,12 +58,14 @@ class Watcher extends EventEmitter { app: string, dest: string, routes: string, + bundler?: string, webpack: string, rollup: string, port: number }) { super(); + this.bundler = validate_bundler(bundler); this.dirs = { app, dest, routes, webpack, rollup }; this.port = port; this.closed = false; @@ -157,7 +163,7 @@ class Watcher extends EventEmitter { }; // TODO watch the configs themselves? - const compilers: Compilers = create_compilers({ + const compilers: Compilers = create_compilers(this.bundler, { webpack: this.dirs.webpack, rollup: this.dirs.rollup }); diff --git a/src/api/export.ts b/src/api/export.ts index 9e80ac5..0fc877b 100644 --- a/src/api/export.ts +++ b/src/api/export.ts @@ -13,7 +13,7 @@ import * as events from './interfaces'; type Opts = { build: string, dest: string, - basepath: string, + basepath?: string, timeout: number | false }; diff --git a/src/cli/build.ts b/src/cli/build.ts index c5844f6..e1a7e87 100644 --- a/src/cli/build.ts +++ b/src/cli/build.ts @@ -1,14 +1,18 @@ import { build as _build } from '../api/build'; -import colors from 'kleur'; +import * as colors from 'kleur'; import { locations } from '../config'; +import validate_bundler from './utils/validate_bundler'; + +export function build(opts: { bundler?: string }) { + const bundler = validate_bundler(opts.bundler); -export function build() { return new Promise((fulfil, reject) => { try { const emitter = _build({ dest: locations.dest(), app: locations.app(), routes: locations.routes(), + bundler, webpack: 'webpack', rollup: 'rollup' }); diff --git a/src/cli/dev.ts b/src/cli/dev.ts index afbbc2f..ecaa4a3 100644 --- a/src/cli/dev.ts +++ b/src/cli/dev.ts @@ -5,7 +5,7 @@ import prettyMs from 'pretty-ms'; import { dev as _dev } from '../api/dev'; import * as events from '../api/interfaces'; -export function dev(opts: { port: number, open: boolean }) { +export function dev(opts: { port: number, open: boolean, bundler?: string }) { try { const watcher = _dev(opts); diff --git a/src/cli/export.ts b/src/cli/export.ts index 04aa0f9..052e9b9 100644 --- a/src/cli/export.ts +++ b/src/cli/export.ts @@ -1,5 +1,5 @@ import { exporter as _exporter } from '../api/export'; -import colors from 'kleur'; +import * as colors from 'kleur'; import prettyBytes from 'pretty-bytes'; import { locations } from '../config'; diff --git a/src/cli/utils/validate_bundler.ts b/src/cli/utils/validate_bundler.ts new file mode 100644 index 0000000..38ea550 --- /dev/null +++ b/src/cli/utils/validate_bundler.ts @@ -0,0 +1,21 @@ +import * as fs from 'fs'; + +export default function validate_bundler(bundler?: string) { + if (!bundler) { + bundler = ( + fs.existsSync('rollup') ? 'rollup' : + fs.existsSync('webpack') ? 'webpack' : + null + ); + + if (!bundler) { + throw new Error(`Could not find a 'rollup' or 'webpack' directory`); + } + } + + if (bundler !== 'rollup' && bundler !== 'webpack') { + throw new Error(`'${bundler}' is not a valid option for --bundler — must be either 'rollup' or 'webpack'`); + } + + return bundler; +} \ No newline at end of file diff --git a/src/core/create_compilers.ts b/src/core/create_compilers.ts index 210b71f..8e25432 100644 --- a/src/core/create_compilers.ts +++ b/src/core/create_compilers.ts @@ -211,8 +211,8 @@ export type Compilers = { serviceworker?: Compiler; } -export default function create_compilers({ webpack, rollup }: { webpack: string, rollup: string }): Compilers { - if (fs.existsSync(rollup)) { +export default function create_compilers(bundler: string, { webpack, rollup }: { webpack: string, rollup: string }): Compilers { + if (bundler === 'rollup') { if (!r) r = relative('rollup', process.cwd()); const sw = `${rollup}/service-worker.config.js`; @@ -224,7 +224,7 @@ export default function create_compilers({ webpack, rollup }: { webpack: string, }; } - if (fs.existsSync(webpack)) { + if (bundler === 'webpack') { if (!wp) wp = relative('webpack', process.cwd()); const sw = `${webpack}/service-worker.config.js`; @@ -236,7 +236,8 @@ export default function create_compilers({ webpack, rollup }: { webpack: string, }; } - throw new Error(`Could not find config files for rollup or webpack`); + // this shouldn't be possible... + throw new Error(`Invalid bundler option '${bundler}'`); } const locPattern = /\((\d+):(\d+)\)$/;