committed by
GitHub
6 changed files with 90 additions and 59 deletions
@ -1,10 +1,10 @@ |
|||||
const Future = require('fluture'); |
const Future = require('fluture'); |
||||
const { webpackCompile, validateWebpackConfig } = require('./utils'); |
const { compile, validate } = require('./webpack'); |
||||
|
|
||||
// build :: Object config -> Future (Array Error) Function
|
// build :: Object config -> Future (Array Error) Function
|
||||
const build = config => Future |
const build = config => Future |
||||
.of(config) |
.of(config) |
||||
.chain(validateWebpackConfig) |
.chain(validate) |
||||
.chain(webpackCompile); |
.chain(compile); |
||||
|
|
||||
module.exports = build; |
module.exports = build; |
||||
|
@ -1,19 +1,11 @@ |
|||||
const merge = require('deepmerge'); |
const merge = require('deepmerge'); |
||||
const DevServer = require('webpack-dev-server'); |
|
||||
const Future = require('fluture'); |
const Future = require('fluture'); |
||||
const { createWebpackCompiler, validateWebpackConfig } = require('./utils'); |
const { serve, validate } = require('./webpack'); |
||||
|
|
||||
// devServer :: Object config -> Future () Function
|
// server :: Object config -> Future () Function
|
||||
const devServer = config => Future |
const devServer = config => Future |
||||
.of(merge({ devServer: { host: 'localhost', port: 5000, noInfo: true } }, config)) |
.of(merge({ devServer: { host: 'localhost', port: 5000, noInfo: true } }, config)) |
||||
.chain(validateWebpackConfig) |
.chain(validate) |
||||
.chain(createWebpackCompiler) |
.chain(serve); |
||||
.chain(compiler => Future((reject, resolve) => { |
|
||||
const { devServer } = compiler.options; |
|
||||
const { host, port } = devServer; |
|
||||
const server = new DevServer(compiler, devServer); |
|
||||
|
|
||||
server.listen(port, host, () => resolve(compiler)); |
|
||||
})); |
|
||||
|
|
||||
module.exports = devServer; |
module.exports = devServer; |
||||
|
@ -1,10 +1,10 @@ |
|||||
const Future = require('fluture'); |
const Future = require('fluture'); |
||||
const { createWebpackWatcher, validateWebpackConfig } = require('./utils'); |
const { watcher, validate } = require('./webpack'); |
||||
|
|
||||
// watch :: Object config -> Future (Array Error) ()
|
// watch :: Object config -> Future (Array Error) ()
|
||||
const watch = config => Future |
const watch = config => Future |
||||
.of(config) |
.of(config) |
||||
.chain(validateWebpackConfig) |
.chain(validate) |
||||
.chain(createWebpackWatcher); |
.chain(watcher); |
||||
|
|
||||
module.exports = watch; |
module.exports = watch; |
||||
|
@ -0,0 +1,74 @@ |
|||||
|
const Future = require('fluture'); |
||||
|
const { assoc, construct, isEmpty, pathOr, pipe } = require('ramda'); |
||||
|
const webpack = require('webpack'); |
||||
|
const DevServer = construct(require('webpack-dev-server')); |
||||
|
const { toArray } = require('./utils'); |
||||
|
|
||||
|
// errors :: (Error|Array Error err -> Object stats) -> Array Error
|
||||
|
const getErrors = (err, stats) => (err ? toArray(err) : stats.toJson().errors); |
||||
|
|
||||
|
// compiler :: Object config -> Future Error Object
|
||||
|
const compiler = pipe(Future.of, Future.ap(Future.of(webpack))); |
||||
|
|
||||
|
// compile :: Object config -> Future Error Object
|
||||
|
const compile = pipe( |
||||
|
compiler, |
||||
|
Future.chain(compiler => Future((reject, resolve) => { |
||||
|
compiler.run((err, stats) => { |
||||
|
const errors = getErrors(err, stats); |
||||
|
|
||||
|
isEmpty(errors) ? resolve(stats) : reject(errors); |
||||
|
}); |
||||
|
})) |
||||
|
); |
||||
|
|
||||
|
// devServer :: Object config -> Future Error Object
|
||||
|
const devServer = pipe( |
||||
|
compiler, |
||||
|
Future.map(compiler => assoc('compiler', compiler, new DevServer(compiler, compiler.options.devServer))) |
||||
|
); |
||||
|
|
||||
|
// serve :: Object config -> Future Error Object
|
||||
|
const serve = pipe( |
||||
|
devServer, |
||||
|
Future.chain(server => Future((reject, resolve) => { |
||||
|
const { compiler } = server; |
||||
|
const { host, port } = compiler.options.devServer; |
||||
|
|
||||
|
server.listen(port, host, () => resolve(compiler)); |
||||
|
})) |
||||
|
); |
||||
|
|
||||
|
// validator :: Object config -> Future Error Object
|
||||
|
const validator = pipe(Future.of, Future.ap(Future.of(webpack.validate))); |
||||
|
|
||||
|
// validate :: Object config -> Future Error Object
|
||||
|
const validate = config => validator(config) |
||||
|
.chain(errors => (isEmpty(errors) ? |
||||
|
Future.of(config) : |
||||
|
Future.reject([new webpack.WebpackOptionsValidationError(errors)]))); |
||||
|
|
||||
|
// watch :: Object config -> Future Error Object
|
||||
|
const watcher = pipe( |
||||
|
compiler, |
||||
|
Future.chain(compiler => Future((reject, resolve) => { |
||||
|
const watchOptions = pathOr({}, ['options', 'watchOptions'], compiler); |
||||
|
|
||||
|
compiler.watch(watchOptions, (err, stats) => { |
||||
|
const errors = getErrors(err, stats); |
||||
|
|
||||
|
isEmpty(errors) ? resolve(compiler) : reject(errors); |
||||
|
}); |
||||
|
})) |
||||
|
); |
||||
|
|
||||
|
module.exports = { |
||||
|
getErrors, |
||||
|
compiler, |
||||
|
compile, |
||||
|
devServer, |
||||
|
serve, |
||||
|
validator, |
||||
|
validate, |
||||
|
watcher |
||||
|
}; |
Loading…
Reference in new issue