mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-15 20:34:44 +00:00
create a facade over webpack, to support alternative compilers
This commit is contained in:
@@ -1,29 +1,98 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import relative from 'require-relative';
|
||||
|
||||
export default function create_compilers({ webpack }: { webpack: string }) {
|
||||
const wp = relative('webpack', process.cwd());
|
||||
let r: any;
|
||||
let wp: any;
|
||||
|
||||
const serviceworker_config = try_require(path.resolve(`${webpack}/service-worker.config.js`));
|
||||
export class WebpackCompiler {
|
||||
_: any;
|
||||
|
||||
return {
|
||||
client: wp(
|
||||
require(path.resolve(`${webpack}/client.config.js`))
|
||||
),
|
||||
constructor(config: any) {
|
||||
this._ = wp(require(path.resolve(config)));
|
||||
}
|
||||
|
||||
server: wp(
|
||||
require(path.resolve(`${webpack}/server.config.js`))
|
||||
),
|
||||
oninvalid(cb: (filename: string) => void) {
|
||||
this._.hooks.invalid.tap('sapper', cb);
|
||||
}
|
||||
|
||||
serviceworker: serviceworker_config && wp(serviceworker_config)
|
||||
};
|
||||
compile() {
|
||||
return new Promise((fulfil, reject) => {
|
||||
this._.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);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
watch(cb: (err: Error, stats: any) => void) {
|
||||
this._.watch({}, cb);
|
||||
}
|
||||
}
|
||||
|
||||
function try_require(specifier: string) {
|
||||
try {
|
||||
return require(specifier);
|
||||
} catch (err) {
|
||||
if (err.code === 'MODULE_NOT_FOUND') return null;
|
||||
throw err;
|
||||
export class RollupCompiler {
|
||||
constructor(config: any) {
|
||||
|
||||
}
|
||||
|
||||
oninvalid(cb: (filename: string) => void) {
|
||||
|
||||
}
|
||||
|
||||
compile() {
|
||||
return new Promise((fulfil, reject) => {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
watch(cb: (err: Error, stats: any) => void) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export type Compiler = RollupCompiler | WebpackCompiler;
|
||||
|
||||
export type Compilers = {
|
||||
client: Compiler;
|
||||
server: Compiler;
|
||||
serviceworker?: Compiler;
|
||||
}
|
||||
|
||||
export default function create_compilers({ webpack, rollup }: { webpack: string, rollup: string }): Compilers {
|
||||
if (fs.existsSync(rollup)) {
|
||||
if (!r) r = relative('rollup', process.cwd());
|
||||
|
||||
const sw = `${rollup}/service-worker.config.js`;
|
||||
|
||||
return {
|
||||
client: new RollupCompiler(`${rollup}/client.config.js`),
|
||||
server: new RollupCompiler(`${rollup}/server.config.js`),
|
||||
serviceworker: fs.existsSync(sw) && new RollupCompiler(sw)
|
||||
};
|
||||
}
|
||||
|
||||
if (fs.existsSync(webpack)) {
|
||||
if (!wp) wp = relative('webpack', process.cwd());
|
||||
|
||||
const sw = `${webpack}/service-worker.config.js`;
|
||||
|
||||
return {
|
||||
client: new WebpackCompiler(`${webpack}/client.config.js`),
|
||||
server: new WebpackCompiler(`${webpack}/server.config.js`),
|
||||
serviceworker: fs.existsSync(sw) && new WebpackCompiler(sw)
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error(`Could not find config files for rollup or webpack`);
|
||||
}
|
||||
Reference in New Issue
Block a user