-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
128 lines (109 loc) · 3.51 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
"use strict";
var gulp = require('gulp'),
gutil = require("gulp-util"),
filter = require('gulp-filter'),
replace = require('gulp-replace'),
autoprefixer = require('gulp-autoprefixer'),
jshint = require('gulp-jshint'),
react = require('gulp-react'),
cachebust = new require('gulp-cachebust')(),
fs = require('fs-extra'),
minifyCSS = require('gulp-minify-css'),
sass = require('gulp-ruby-sass'),
webpack = require("webpack"),
webpackBuild = require('./webpack.config'),
webpackDev = require('./webpack.config.dev'),
notifier = require('node-notifier');
var paths = {
build: 'build/',
public: 'public/',
sass: 'style/main.scss',
server: ['package.json', 'app.js', '*.jsx', 'cachebuster.js', 'components/**/*.jsx']
};
var pkg = require('./package.json');
function notifyError(err) {
if (!err) { return; }
gutil.log(err.message);
gutil.beep();
notifier.notify({
title: 'Building ' + pkg.name,
message: err.message
});
}
// Build for production
gulp.task('build', ['clean', 'sass', 'webpack', 'copy', 'bust'], function () {
notifier.notify({
icon: null,
contentImage: __dirname + '/public/images/favicon.png',
title: pkg.name,
sound: 'Glass',
message: 'Build: done.',
open: 'file://' + __dirname + '/' + paths.build
});
gutil.log('[build] Run `./scripts/prod` to test the built app.');
});
gulp.task('sass', function () {
var filterCSS = filter('**/*.css');
return gulp.src(paths.sass)
.pipe(sass())
.on('error', notifyError)
.pipe(filterCSS)
.pipe(autoprefixer())
.on('error', notifyError)
.pipe(filterCSS.restore())
.pipe(gulp.dest(paths.public + 'css'));
});
// Clean build directory
gulp.task('clean', function (callback) {
fs.remove(paths.build, callback);
});
// create chunks and uglify with webpack
gulp.task('webpack', ['clean'], function (callback) {
webpack(webpackBuild, function (err, stats) {
if (err) return notifyError(err);
gutil.log("[webpack]", stats.toString({
colors: true,
hash: false,
timings: false,
assets: true,
chunks: false,
chunkModules: false,
modules: false,
children: true
}));
callback();
});
});
// Copy the app
gulp.task('copy', ['copy:server', 'copy:public']);
// copy server files
gulp.task('copy:server', ['clean'], function() {
return gulp.src(paths.server, { base: '.' })
.pipe(gulp.dest(paths.build));
});
// copy public
gulp.task('copy:public', ['clean', 'sass'], function() {
var src = [paths.public + '**/*', '!**/*.map'];
var filterCSS = filter('**/*.css');
return gulp.src(src, { base: '.' })
.pipe(filterCSS)
.pipe(minifyCSS({keepBreaks:true}))
.pipe(filterCSS.restore())
.pipe(gulp.dest(paths.build));
});
// cache busters
var bustSrc =
gulp.task('bust', ['bust:collect', 'bust:replace']);
// collect resources for cache busting
gulp.task('bust:collect', ['sass', 'webpack', 'copy'], function () {
var src = [].concat(paths.public + '**/*');
return gulp.src(src, { cwd: paths.build, base: paths.build + paths.public })
.pipe(cachebust.resources());
});
// replace collected resources
gulp.task('bust:replace', ['bust:collect'], function () {
gutil.log("[bust:replace]", 'Busting ' + Object.keys(cachebust.mappings).length + ' asset(s)...');
return gulp.src(paths.server, { cwd: paths.build, base: paths.build })
.pipe(cachebust.references())
.pipe(gulp.dest(paths.build));
});