add a --bundler option, for forcing rollup or webpack

This commit is contained in:
Rich Harris
2018-08-28 17:29:14 -04:00
parent 6e2383b66b
commit 85e25d6380
8 changed files with 45 additions and 11 deletions

View File

@@ -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', <events.BuildEvent>{

View File

@@ -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
});

View File

@@ -13,7 +13,7 @@ import * as events from './interfaces';
type Opts = {
build: string,
dest: string,
basepath: string,
basepath?: string,
timeout: number | false
};

View File

@@ -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'
});

View File

@@ -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);

View File

@@ -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';

View File

@@ -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;
}

View File

@@ -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+)\)$/;