UNPKG

1.57 kBJavaScriptView Raw
1const webpack = require('webpack')
2const config = require('../config/webpack.config')
3const nodemon = require('nodemon')
4const once = require('ramda').once
5const path = require('path')
6
7const options = {
8 env: process.env.NODE_ENV || 'production'
9}
10
11class Transformer {
12 mutate (fn) {
13 fn(this)
14 return this
15 }
16}
17
18
19class Lectro extends Transformer{
20 constructor(webpackConfig){
21 super();
22 this.webpackConfig = config(options)
23 }
24
25 build() {
26 return new Promise((resolve, reject) => {
27 webpack(this.webpackConfig, function (err, stats) {
28 if (err) {
29 console.log(err)
30 return
31 }
32
33 console.log(stats.toString({
34 colors: true,
35 chunks: false
36 }))
37 })
38 })
39 }
40
41 dev() {
42 if (process.env.NODE_ENV === 'development') {
43 const serverCompiler = webpack(this.webpackConfig)
44
45 const startServer = () => {
46 const serverPaths = Object
47 .keys(serverCompiler.options.entry)
48 .map(entry => path.join(serverCompiler.options.output.path, `${entry}.js`))
49 nodemon({ script: serverPaths[0], watch: serverPaths, nodeArgs: process.argv.slice(2) })
50 .on('quit', process.exit)
51 }
52
53 const startServerOnce = once((err, stats) => {
54 if (err) return
55 startServer()
56 })
57
58 serverCompiler.watch(this.webpackConfig.watchOptions || {}, startServerOnce)
59 }
60 }
61
62 extend(fn) {
63 return this.mutate(self => {
64 self.webpackConfig = fn(this.webpackConfig, options, webpack)
65 })
66 }
67}
68
69module.exports = Lectro