-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGulpfile.js
129 lines (115 loc) · 3.38 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict';
const gulp = require("gulp");
const gutil = require("gulp-util");
const webpack = require("webpack");
const sass = require('gulp-sass');
const cssmin = require('gulp-minify-css');
const runSequence = require('run-sequence');
const spawn = require('child_process').spawn;
const tap = require("gulp-tap");
const fs = require("fs");
const WebpackDevServer = require('webpack-dev-server');
const devConfig = require('./dev.config.js');
const prodConfig = require('./prod.config.js');
const path = require('path');
const deepcopy = require('deepcopy');
const webpackLogOptions = {
colors: true,
hash: false,
timings: true,
assets: false,
chunks: true,
chunkModules: false,
modules: false,
cached: false,
reasons: false,
source: false,
chunkOrigins: false
};
const paths = {
sassEntries: ['./src/scss/entries/**/*.scss'],
sassSrc: ['./src/scss/**/*.scss', './src/css/**/*.css'],
sassDest: './public/compiled/css'
};
function currentTime() {
var date = new Date();
var minutes = String(date.getMinutes());
var seconds = String(date.getSeconds());
minutes = (minutes.length === 1) ? '0' + minutes : minutes;
seconds = (seconds.length === 1) ? '0' + seconds : seconds;
return gutil.colors.cyan(date.getHours() + ':' + minutes + ':' + seconds);
}
function logFile(file) {
gutil.log(
gutil.colors.cyan(currentTime()),
gutil.colors.green('Wrote file:'),
gutil.colors.magenta(file.path.replace(process.cwd(), '.'))
);
}
function runWebpack(config, cb) {
return webpack(config, function (err, stats) {
if (err) {
gutil.error("[webpack] - build error", err);
cb(err);
return;
}
if (stats.hasErrors()) {
var jsonStats = stats.toJson();
gutil.log("[webpack] - stats has errors", jsonStats.errors);
cb(jsonStats);
return;
}
gutil.log("[webpack]", stats.toString(webpackLogOptions));
cb();
});
}
gulp.task("webpack", function(cb) {
const config = deepcopy(devConfig);
runWebpack(config, cb);
});
gulp.task("build", function(cb) {
const config = deepcopy(prodConfig);
runWebpack(config, cb);
});
gulp.task('webpack-dev-server', function () {
const compiler = webpack(devConfig);
const server = new WebpackDevServer(compiler, {
hot: true,
contentBase: 'public',
stats: webpackLogOptions,
historyApiFallback: true,
publicPath: devConfig.output.publicPath,
headers: {
'Access-Control-Allow-Origin': '*'
}
});
server.listen(8901, "localhost", function (err) {
if (err) {
throw new gutil.PluginError("webpack-dev-server", err);
}
// Server listening
gutil.log("[webpack-dev-server]", "Listening on localhost:8901");
});
});
// Sass
gulp.task('sass', function () {
return gulp.src(paths.sassEntries)
.pipe(sass({
errLogToConsole: true,
sourceComments: 'map'
}))
.pipe(cssmin())
.pipe(gulp.dest(paths.sassDest))
.pipe(tap(logFile));
});
gulp.task('dev-javascript', function (cb) {
gulp.watch(["src/**/*"], ["webpack"]);
gulp.watch(paths.sassSrc, ['sass']);
runSequence(['webpack', 'webpack-dev-server', 'sass'], cb);
});
gulp.task('build-prod', function (cb) {
runSequence(['build', 'sass'], cb);
});
gulp.task('default', function (cb) {
runSequence('dev-javascript', cb);
});