forked from pypi/warehouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.babel.js
274 lines (227 loc) · 8.57 KB
/
Gulpfile.babel.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
import brotli from "gulp-brotli";
import del from "del";
import gulp from "gulp";
import gulpBatch from "gulp-batch";
import gulpCSSNano from "gulp-cssnano";
import gulpImage from "gulp-image";
import gulpSass from "gulp-sass";
import gulpSequence from "gulp-sequence";
import gulpUglify from "gulp-uglify/minifier";
import gulpWatch from "gulp-watch";
import gulpWebpack from "webpack-stream";
import gzip from "gulp-gzip";
import manifest from "gulp-rev-all";
import manifestClean from "gulp-rev-napkin";
import named from "vinyl-named";
import path from "path";
import sourcemaps from "gulp-sourcemaps";
import * as uglify from "uglify-js";
import webpack from "webpack";
// Configure where our files come from, where they get saved too, and what path
// they are served from.
let staticPrefix = "warehouse/static/";
let distPath = path.join(staticPrefix, "dist");
let publicPath = "/static/";
// Configure webpack so that it compiles all of our javascript into a bundle.
let webpackConfig = {
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loaders: [
{ loader: "babel", query: { presets: ["es2015"] } },
],
},
],
},
plugins: [
new webpack.ProvidePlugin({
"fetch": "imports?this=>global!exports?global.fetch!whatwg-fetch",
}),
new webpack.optimize.DedupePlugin(),
],
// We tell it to use an inline source map, but only so that it can be loaded
// by gulp-sourcemaps later on. It will actually be written out to disk and
// *NOT* inlined when all is said and done.
devtool: "inline-source-map",
output: {
publicPath: publicPath + "js/",
filename: "[name].js",
chunkFilename: "chunks/[chunkhash].js",
},
resolve: {
modules: [ path.resolve(staticPrefix, "js"), "node_modules" ],
alias: {
"clipboard": "clipboard/dist/clipboard",
},
},
};
gulp.task("dist:js", () => {
let files = [
path.join(staticPrefix, "js", "warehouse", "index.js"),
];
return gulp.src(files)
// .pipe(named(() => { return "warehouse"; }))
.pipe(named((file) => {
// Get the filename that is relative to our js directory
let relPath = path.relative(
path.join(staticPrefix, "js"),
file.path
);
// If this is our main application, then we want to call it
// just "warehouse", otherwise, we'll use whatever the real
// name is.
if (relPath == "warehouse/index.js") { return "warehouse"; }
else { return path.parse(relPath).name; }
}))
.pipe(gulpWebpack(webpackConfig, webpack))
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(gulpUglify(
// We don't care about IE6-8 so there's no reason to have
// uglify contain to maintain compatability for it.
{compress: { screw_ie8: true }, mangle: { screw_ie8: true }},
// We pass in our own uglify instance rather than allow
// gulp-uglify to use it's own. This makes the usage slightly
// more complicated, however it means that we have explicit
// control over the exact version of uglify-js used.
uglify
))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(path.join(distPath, "js")));
});
gulp.task("dist:css", () => {
let sassPath = path.join(staticPrefix, "sass");
return gulp.src(path.join(sassPath, "warehouse.scss"))
.pipe(sourcemaps.init())
.pipe(
gulpSass({ includePaths: [sassPath] })
.on("error", gulpSass.logError))
.pipe(gulpCSSNano({
safe: true,
discardComments: {removeAll: true},
}))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(path.join(distPath, "css")));
});
gulp.task("dist:font-awesome:css", () => {
let fABasePath = path.dirname(require.resolve("font-awesome/package.json")); // eslint-disable-line no-undef
let fACSSPath = path.resolve(fABasePath, "css", "font-awesome.css");
return gulp.src(fACSSPath)
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(gulpCSSNano({
safe: true,
discardComments: {removeAll: true},
}))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(path.join(distPath, "css")));
});
gulp.task("dist:font-awesome:fonts", () => {
let fABasePath = path.dirname(require.resolve("font-awesome/package.json")); // eslint-disable-line no-undef
let faFontPath = path.resolve(fABasePath, "fonts", "*.*");
return gulp.src(faFontPath)
.pipe(gulp.dest(path.join(distPath, "fonts")));
});
gulp.task("dist:font-awesome",
["dist:font-awesome:css", "dist:font-awesome:fonts"]);
gulp.task("dist:images", () => {
return gulp.src(path.join(staticPrefix, "images", "**", "*"))
.pipe(gulpImage({
"svgo": false, // SVGO is currently broken.
}))
.pipe(gulp.dest(path.join(distPath, "images")));
});
gulp.task("dist:manifest", () => {
let paths = [
// Cachebust our CSS files and the source maps for them.
path.join(distPath, "css", "*.css"),
path.join(distPath, "css", "*.map"),
// Cachebust our Font files.
path.join(distPath, "fonts", "*"),
// Cachebust our JS files and the source maps for them.
path.join(distPath, "js", "*.js"),
path.join(distPath, "js", "*.map"),
// Cachebust our Image files.
path.join(distPath, "images", "*"),
];
let m = new manifest({ fileNameManifest: "manifest.json" });
return gulp.src(paths, { base: distPath })
.pipe(m.revision())
.pipe(gulp.dest(distPath))
.pipe(manifestClean({ verbose: false }))
.pipe(m.manifestFile())
.pipe(gulp.dest(distPath));
});
gulp.task("dist:compress:gz", () => {
return gulp.src(path.join(distPath, "**", "*"))
.pipe(gzip({
skipGrowingFiles: true,
gzipOptions: { level: 9, memLevel: 9 },
}))
.pipe(gulp.dest(distPath));
});
gulp.task("dist:compress:br:generic", () => {
let paths = [
path.join(distPath, "fonts", "*.otf"),
path.join(distPath, "fonts", "*.woff"),
path.join(distPath, "fonts", "*.woff2"),
path.join(distPath, "fonts", "*.ttf"),
path.join(distPath, "fonts", "*.eot"),
path.join(distPath, "fonts", "*.svg"),
path.join(distPath, "images", "*.png"),
path.join(distPath, "images", "*.svg"),
path.join(distPath, "images", "*.ico"),
];
return gulp.src(paths, { base: distPath })
.pipe(brotli.compress({skipLarger: true, mode: 0, quality: 11}))
.pipe(gulp.dest(distPath));
});
gulp.task("dist:compress:br:text", () => {
let paths = [
path.join(distPath, "css", "*.css"),
path.join(distPath, "css", "*.map"),
path.join(distPath, "js", "*.js"),
path.join(distPath, "js", "*.map"),
path.join(distPath, "manifest.json"),
];
return gulp.src(paths, { base: distPath })
.pipe(brotli.compress({skipLarger: true, mode: 1, quality: 11}))
.pipe(gulp.dest(distPath));
});
gulp.task(
"dist:compress:br",
["dist:compress:br:generic", "dist:compress:br:text"]
);
gulp.task("dist:compress", ["dist:compress:gz", "dist:compress:br"]);
gulp.task("dist", (cb) => {
return gulpSequence(
// Ensure that we have a good clean base to start out with, by blowing away
// any previously built files.
"clean",
// Build all of our static assets.
["dist:font-awesome", "dist:css", "dist:js"],
// We have this here, instead of in the list above even though there is no
// ordering dependency so that all of it's output shows up together which
// makes it easier to read.
"dist:images",
// This has to be on it's own, and it has to be one of the last things we do
// because otherwise we won't catch all of the files in the revisioning
// process.
"dist:manifest",
// Finally, once we've done everything else, we'll compress everything that
// we've gotten.
"dist:compress"
)(cb);
});
gulp.task("clean", () => { return del(distPath); });
gulp.task("watch", ["dist"], () => {
let watchPaths = [
path.join(staticPrefix, "**", "*"),
path.join("!" + distPath, "**", "*"),
];
gulpWatch(
watchPaths,
gulpBatch((_, done) => { gulp.start("dist", done); })
);
});
gulp.task("default", ["dist"]);