|
|
@ -1,16 +1,16 @@ |
|
|
|
const { join, isAbsolute } = require('path'); |
|
|
|
const { EventEmitter } = require('events'); |
|
|
|
const DevServer = require('webpack-dev-server'); |
|
|
|
const webpack = require('webpack'); |
|
|
|
const Config = require('webpack-chain'); |
|
|
|
const ora = require('ora'); |
|
|
|
const merge = require('deepmerge'); |
|
|
|
const { defaultTo } = require('ramda'); |
|
|
|
const { defaultTo, identity } = require('ramda'); |
|
|
|
const requireMiddleware = require('./requireMiddleware'); |
|
|
|
const build = require('./webpack/build'); |
|
|
|
const develop = require('./webpack/develop'); |
|
|
|
const watch = require('./webpack/watch'); |
|
|
|
|
|
|
|
const normalizePath = (path, root) => (isAbsolute(path) ? path : join(root, path)); |
|
|
|
|
|
|
|
class Neutrino extends EventEmitter { |
|
|
|
module.exports = class Neutrino extends EventEmitter { |
|
|
|
constructor(options = {}) { |
|
|
|
super(); |
|
|
|
|
|
|
@ -37,27 +37,6 @@ class Neutrino extends EventEmitter { |
|
|
|
return requireMiddleware(middleware, this.options); |
|
|
|
} |
|
|
|
|
|
|
|
handleErrors(err, stats) { |
|
|
|
if (err) { |
|
|
|
console.error(err.stack || err); |
|
|
|
|
|
|
|
if (err.details) { |
|
|
|
console.error(err.details); |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
const jsonStats = stats.toJson(); |
|
|
|
|
|
|
|
if (jsonStats.errors.length) { |
|
|
|
jsonStats.errors.map(err => console.error(err)); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
getWebpackConfig() { |
|
|
|
return this.config.toConfig(); |
|
|
|
} |
|
|
@ -67,18 +46,18 @@ class Neutrino extends EventEmitter { |
|
|
|
} |
|
|
|
|
|
|
|
build(args) { |
|
|
|
return this.runCommand('build', args, () => this.builder()); |
|
|
|
return this.runCommand('build', args, build); |
|
|
|
} |
|
|
|
|
|
|
|
start(args) { |
|
|
|
return this.runCommand('start', args, () => (this.getWebpackOptions().devServer ? this.devServer() : this.watcher())); |
|
|
|
return this.runCommand('start', args, this.getWebpackConfig().devServer ? develop : watch); |
|
|
|
} |
|
|
|
|
|
|
|
test(args) { |
|
|
|
return this.runCommand('test', args); |
|
|
|
} |
|
|
|
|
|
|
|
runCommand(command, args = {}, fn) { |
|
|
|
runCommand(command, args = {}, fn = identity) { |
|
|
|
process.env.NODE_ENV = defaultTo({ |
|
|
|
build: 'production', |
|
|
|
start: 'development', |
|
|
@ -87,74 +66,7 @@ class Neutrino extends EventEmitter { |
|
|
|
|
|
|
|
return this |
|
|
|
.emitForAll(`pre${command}`, args) |
|
|
|
.then(fn) |
|
|
|
.then(() => fn(this.getWebpackConfig())) |
|
|
|
.then(() => this.emitForAll(command, args)); |
|
|
|
} |
|
|
|
|
|
|
|
devServer() { |
|
|
|
return new Promise((resolve) => { |
|
|
|
const starting = ora('Starting development server').start(); |
|
|
|
const config = this.getWebpackOptions(); |
|
|
|
const protocol = config.devServer.https ? 'https' : 'http'; |
|
|
|
const host = config.devServer.host || 'localhost'; |
|
|
|
const port = config.devServer.port || 5000; |
|
|
|
|
|
|
|
config.devServer.noInfo = true; |
|
|
|
|
|
|
|
const compiler = webpack(config); |
|
|
|
const server = new DevServer(compiler, config.devServer); |
|
|
|
const building = ora('Waiting for initial build to finish').start(); |
|
|
|
|
|
|
|
process.on('SIGINT', resolve); |
|
|
|
server.listen(port, host, () => { |
|
|
|
starting.succeed(`Development server running on: ${protocol}://${host}:${port}`); |
|
|
|
compiler.plugin('compile', () => { |
|
|
|
building.text = 'Source changed, re-compiling'; |
|
|
|
building.start(); |
|
|
|
}); |
|
|
|
compiler.plugin('done', () => building.succeed('Build completed')); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
watcher() { |
|
|
|
return new Promise((resolve) => { |
|
|
|
const building = ora('Waiting for initial build to finish').start(); |
|
|
|
const config = this.getWebpackOptions(); |
|
|
|
const compiler = webpack(config); |
|
|
|
const watcher = compiler.watch(config.watchOptions || {}, (err, stats) => { |
|
|
|
building.succeed('Build completed'); |
|
|
|
this.handleErrors(err, stats); |
|
|
|
}); |
|
|
|
|
|
|
|
process.on('SIGINT', () => watcher.close(resolve)); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
builder() { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
const config = this.getWebpackOptions(); |
|
|
|
const compiler = webpack(config); |
|
|
|
|
|
|
|
// eslint-disable-next-line consistent-return
|
|
|
|
compiler.run((err, stats) => { |
|
|
|
const failed = this.handleErrors(err, stats); |
|
|
|
|
|
|
|
if (failed) { |
|
|
|
return reject(); |
|
|
|
} |
|
|
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(stats.toString({ |
|
|
|
colors: true, |
|
|
|
chunks: false, |
|
|
|
children: false |
|
|
|
})); |
|
|
|
|
|
|
|
resolve(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = Neutrino; |
|
|
|