-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
67 lines (55 loc) · 1.79 KB
/
gulpfile.babel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* eslint-disable global-require */
/* eslint-disable no-console */
import del from 'del'
import gulp from 'gulp'
import dotenv from 'dotenv'
import webpack from 'webpack'
import sequence from 'run-sequence'
import { assign, attempt } from 'lodash'
import { STATIC_DIR } from './source/shared/paths'
// Load and parse .env file
const { parsed: env } = dotenv.load()
//----------------------------------------
// Webpack Helpers
//----------------------------------------
function createWebpackConfig(options) {
return require('./webpack.config')(options)
}
function createWebpackHandler(callback) {
return function webpackHandler(error, stats) {
if (error) {
console.log(error.stack || error)
if (error.details) console.log(error.details)
attempt(callback, error)
} else {
console.log(stats.toString({
children: false,
chunks: false,
colors: true
}))
const { errors, warnings } = stats.toJson()
if (stats.hasErrors()) attempt(callback, errors)
else if (stats.hasWarnings()) attempt(callback, warnings)
else attempt(callback, null, stats)
}
}
}
function bundleWithWebpack(options, callback) {
const webpackConfig = createWebpackConfig(assign({}, env, options))
const webpackHandler = createWebpackHandler(callback)
webpack(webpackConfig, webpackHandler)
}
//----------------------------------------
// Gulp Tasks
//----------------------------------------
gulp.task('clean', () => del(STATIC_DIR))
gulp.task('build:client', callback => bundleWithWebpack({
NODE_ENV: 'production',
TARGET: 'client'
}, callback))
gulp.task('build:server', callback => bundleWithWebpack({
NODE_ENV: 'production',
TARGET: 'server'
}, callback))
gulp.task('build', sequence('clean', [ 'build:server' ]))
gulp.task('default', [ 'build' ])