forked from kapost/weeds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGulpfile.js
282 lines (236 loc) · 7.79 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* eslint-disable */
var path = require("path");
var gulp = require("gulp");
var filesToJson = require("gulp-files-to-json");
var sass = require("gulp-sass");
var svgmin = require("gulp-svgmin");
var gutil = require("gulp-util");
var hash = require("gulp-hash");
var autoprefixer = require("gulp-autoprefixer");
var webpack = require("webpack");
var _ = require("lodash");
var config = require("./config/app");
var webpackProdConfig = require("./config/webpack/prod.config")
// development only dependencies
if (process.env.NODE_ENV !== "production") {
var watch = require("gulp-watch");
var livereload = require("gulp-livereload");
var sourcemaps = require("gulp-sourcemaps");
var nodemon = require("gulp-nodemon")
var webpackDevConfig = require("./config/webpack/dev.config")
var createWebpackDevServer = require("./config/webpack/webpack-dev-server");
var webpackDevServerPort = config.port + 2;
}
var buildDirectory = "./build/"
var staticDirectory = "./app/assets/static/"
var svgDirectory = "./app/assets/svg/"
var sassDirectory = "./app/";
var sassEntry = sassDirectory + "/boot/app.scss";
// Configurations
// --------------------------------------------------------------------------------
var sassConfig = {
errLogToConsole: true,
includePaths: [
path.join(__dirname, "app"),
path.join(__dirname, "node_modules") // for vendor files through npm
]
};
var autoprefixerConfig = {
browsers: ["last 2 versions", "IE >= 10", "> 5%", "Firefox ESR"]
};
var nodemonConfig = {
script: "index.js",
watch: (config.serverRendering) ? ["app/", "config/"] : ["app/server.jsx", "app/util/serverHelpers/", "config/"],
ignore: ["**Spec.js"],
ext: "js jsx",
env: { NODE_PATH: "./app" }, // match webpack config resolve.root
execMap: {
"js": "node --harmony"
}
};
var svgoConfig = {
plugins: [
{
removeDimensions: true
}
]
}
// Helper functions
// --------------------------------------------------------------------------------
var gulpErrorHandler = function(err) {
var displayErr = gutil.colors.red(err);
gutil.log(displayErr);
gutil.beep();
};
// Custom parser for filename with directory under svg folder.
// Used by gulp-file-to-json for mapping
var filesToJsonNameParser = function(name) {
var regex = /(?:app\/assets\/svg\/)(.*)/;
var pathKey = path.basename(name, ".svg");
var dirnameMatch = regex.exec(path.dirname(name));
if (dirnameMatch != null) {
pathKey = dirnameMatch[1] + "/" + pathKey;
}
return pathKey;
}
// Tasks
// --------------------------------------------------------------------------------
// Styles
//
// Build sass files with sourcemaps, autoprefixer, and livereload.
// Livereload script is included in the dev HTML through `serverHelpers/livereloadScript`
// for livereload over network addresses (0.0.0.0)
// ----------------------------------------
gulp.task("styles-dev", function () {
gulp.src(sassEntry)
.pipe(sourcemaps.init())
.pipe(sass(sassConfig)
.on("error", gulpErrorHandler))
.pipe(autoprefixer(autoprefixerConfig))
.pipe(sourcemaps.write())
.pipe(gulp.dest(buildDirectory))
.pipe(livereload());
});
gulp.task("styles-build", function () {
return gulp.src(sassEntry)
.pipe(sass(sassConfig).on("error", gulpErrorHandler))
.pipe(autoprefixer(autoprefixerConfig))
.pipe(gulp.dest(buildDirectory));
});
gulp.task("styles-build-production", function () {
return gulp.src(sassEntry)
.pipe(sass(sassConfig).on("error", gulpErrorHandler))
.pipe(autoprefixer(autoprefixerConfig))
.pipe(hash({ hashLength: 16 }))
.pipe(gulp.dest(buildDirectory))
.pipe(hash.manifest("static-assets.json"))
.pipe(gulp.dest(buildDirectory));
});
// // watch all files and rerun build step if necessary
gulp.task("styles-watch", function() {
livereload.listen();
gulp.watch(sassDirectory + "**/*.scss", ["styles-dev"]);
});
// Static files
//
// Copies all static files to `build`.
// ----------------------------------------
gulp.task("copy-static", function() {
return gulp.src(staticDirectory + "**/*")
.pipe(gulp.dest(buildDirectory));
});
gulp.task("copy-static-production", function() {
return gulp.src(staticDirectory + "**/*")
.pipe(hash({ hashLength: 16 }))
.pipe(gulp.dest(buildDirectory))
.pipe(hash.manifest("static-assets.json"))
.pipe(gulp.dest(buildDirectory));
});
gulp.task("copy-static-watch", function() {
gulp.src(staticDirectory + "**/*")
.pipe(watch(staticDirectory + "**/*"))
.pipe(gulp.dest(buildDirectory));
});
// SVG Json File
//
// Task to generate svg/index.json file containing keys pointing to optimised svg strings
// Helpful for isomorphic requring in node and client. NOTE: this is less intelligent that
// webpack loading so don't include a bunch of files you don't need, as this will increase client
// weight. I wish `svg-loader` worked in node land.
//
// Example:
//
// { "test/icon" : "<svg width=\"364\" height=\"191\ ..." }
// ----------------------------------------
gulp.task("create-svg-json", function() {
return gulp.src(svgDirectory + "**/*.svg")
.pipe(svgmin(svgoConfig))
.pipe(filesToJson("index.json", { nameParser: filesToJsonNameParser }))
.pipe(gulp.dest(svgDirectory))
});
gulp.task("create-svg-json-watch", function() {
gulp.src(svgDirectory + "**/*.svg")
.pipe(watch(svgDirectory + "**/*.svg", function() {
gulp.start("create-svg-json");
}));
});
// Webpack
//
// Compiles all the client assets using the dev webpack config. The dev server
// takes the configured dev server and listens on the network address.
// ----------------------------------------
var webpackBuildFunction = function(webpackConfig) {
return function(callback) {
// run webpack
webpack(webpackConfig, function(err, stats) {
if (err) throw new gutil.PluginError("webpack", err);
gutil.log("[webpack]", stats.toString({
colors: true,
chunks: false
}));
if (!webpackConfig.watch) {
callback();
}
});
};
};
gulp.task("webpack", webpackBuildFunction(webpackDevConfig));
gulp.task("webpack-production", webpackBuildFunction(webpackProdConfig));
gulp.task("webpack-watch", webpackBuildFunction(
_.extend({}, webpackDevConfig, { watch: true }))
);
gulp.task("webpack-dev-server", function() {
createWebpackDevServer().listen(webpackDevServerPort, "0.0.0.0", function(err) {
if(err) throw new gutil.PluginError("webpack-dev-server", err);
});
});
// Nodemon
//
// Kick off the server with config.
// ----------------------------------------
gulp.task("nodemon", function () {
nodemon(nodemonConfig)
.on("restart", function () {
gutil.log("==> 🔄 Nodemon restarting server.");
})
});
// Task Groups
// ----------------------------------------
// remove this and put the svg watch back in tasks once we update to Gulp 4
// (with series and parallel support). We need a prebuild step as everything runs in parallel
// and webpack depends on the `create-svg-json` task. See package.json scripts
gulp.task("prebuild", [
"create-svg-json"
]);
gulp.task("default", [
"copy-static-watch",
"styles-dev",
"styles-watch",
"webpack-watch",
"nodemon"
]);
gulp.task("development-hot", [
"copy-static-watch",
"styles-dev",
"styles-watch",
"nodemon",
"webpack-dev-server"
]);
gulp.task("build", [
"copy-static",
"styles-build",
"webpack"
]);
gulp.task("build-production", [
"copy-static-production",
"styles-build-production",
"webpack-production"
]);
gulp.task("server", [
"nodemon"
]);
// Hacks
// --------------------------------------------------------------------------------
// Fix for double `control-c` exit, which fixes `npm start`
// Check on https://github.com/JacksonGariety/gulp-nodemon/pull/91 to remove in future.
process.once("SIGINT", function() { process.exit(0); });