-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.js
111 lines (93 loc) · 3.11 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
108
109
110
111
var path = require('path'),
gulp = require('gulp'),
gutil = require('gulp-util'),
webpack = require('webpack'),
babel = require('gulp-babel'),
os = require('os'),
_ = require("underscore"),
del = require('del'),
open = require('open'),
webpackDevServer = require('webpack-dev-server');
var work_path = process.cwd();
var config = require(path.join(work_path, 'webpack.config.js'));
function getPublicIP() {
var publicIP = "127.0.0.1"; //fallbck ip
var ifaces = os.networkInterfaces();
var address = _.flatten(_.values(ifaces));
address = _.filter(address, function(ifObj) {
return ifObj.family == "IPv4" && ifObj.address != "127.0.0.1";
});
if (address.length > 0) {
publicIP = address[0].address;
}
return publicIP;
}
var host = getPublicIP();
gulp.task('clean', function(cb) {
del(['build', 'lib']).then(function() {
cb();
})
});
gulp.task('start', function(cb) {
var buildFirstTime = true;
var webpackConfig = config.dev();
var compiler = webpack(webpackConfig);
var devHost = "http://" + host + ":" + webpackConfig.port;
compiler.plugin('done', stats => {
if (stats.hasErrors()) {
console.log(stats.toString({ colors: true }));
} else if (buildFirstTime) { //只有第一次启动start的时候才执行
//只有第一次启动start的时候才执行
buildFirstTime = false;
cb && cb();
// listening
gutil.log("[webpack-dev-server]", gutil.colors.magenta(devHost));
gutil.log("[webpack-dev-server]", "To stop service, press [Ctrl + C] ..");
if (typeof process.send === 'function') {
process.send({ start: 'done' });
}
}else{
console.log('\n编译成功,请刷新千牛或者刷新浏览器');
}
});
var server = new webpackDevServer(compiler, {
hot: false,
inline: true,
quiet: true,
publicPath: webpackConfig.output.publicPath,
headers: { 'Access-Control-Allow-Origin': '*' },
contentBase: path.resolve(__dirname, './')
}).listen(webpackConfig.port, '0.0.0.0', function(err) {
if (err) {
throw new gutil.PluginError("webpack-dev-server", err)
}
});
});
gulp.task('build:dist', ['clean'], function(cb) {
var webpackConfig = config.prod();
var compiler = webpack(webpackConfig, function(err, stats) {
if (err) {
gutil.log(err);
}
gutil.log(stats.toString({
colors: true,
chunks: false
}));
});
compiler.plugin('done', stats => {
if (stats.hasErrors()) {
console.log(stats.toString({ colors: true }));
}
cb && cb();
if (typeof process.send === 'function') {
process.send({ build: 'done' });
}
});
});
gulp.task('build:lib', ['clean'], function() {
return gulp.src('src/**/*.js?(x)')
.pipe(babel())
.pipe(gulp.dest('lib'));
});
gulp.task('default', ['start']);
gulp.task('build', ['build:lib', 'build:dist']);