-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
107 lines (96 loc) · 2.44 KB
/
gulpfile.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*eslint-disable */
'use strict';
var path = require('path');
var fs = require('fs');
var gulp = require('gulp');
var webpack = require('webpack');
var DeepMerge = require('deep-merge');
var nodemon = require('nodemon');
var deepmerge = DeepMerge(function(target, source, key) {
if (target instanceof Array) {
return [].concat(target, source);
}
return source;
});
var baseConfig = {
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loaders: ['babel']},
],
},
debug: process.env.NODE_ENV === 'production' ? false : true,
};
/**
* Create Webpack config object based on a base object.
* This function can be used to create different webpack builds. If you need
* to pack other artifacts, such as front-end code, this function can be used
* to create that config.
* @param {object} overrides: Object that is used to override the base config.
* @returns {object}: Webpack config object that can be used to configure Webpack.
*/
function createConfig(overrides) {
return deepmerge(baseConfig, overrides || {});
}
// Calculate external dependencies for Webpack. Webpack searches for these
// packages in the node_modules instead of packing them into the bundle.
var nodeModules = {};
fs.readdirSync('node_modules')
.forEach(function(mod) {
if (mod !== '.bin') {
nodeModules[mod] = 'commonjs ' + mod;
}
});
// Create Webpack config file for the server-side code.
var serverConfig = createConfig({
entry: './src/index.js',
target: 'node',
output: {
path: path.join(__dirname, 'build'),
filename: 'index.js',
},
node: {
__dirname: true,
__filename: true,
},
externals: nodeModules,
plugins: [
],
});
function onBuild(done) {
return function(err, stats) {
if (err) {
console.log(err);
} else {
console.log(stats.toString());
}
if (done) {
done(err);
}
};
}
// Gulp tasks definition begins.
gulp.task('build', function(done) {
webpack(serverConfig).run(onBuild(done));
});
gulp.task('build-watch', ['build'], function() {
webpack(serverConfig).watch(100, function(err, stats) {
onBuild()(err, stats);
nodemon.restart();
});
});
gulp.task('run', ['build-watch'], function() {
nodemon({
execMap: {
js: 'node',
},
script: path.join(__dirname, 'build/index'),
ignore: ['*'],
ext: 'js',
}).on('restart', function() {
console.log('Reloading Nodemon');
});
});
/*eslint-enable */