forked from microsoft/TypeScript-Website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
334 lines (285 loc) · 11.5 KB
/
gulpfile.js
File metadata and controls
334 lines (285 loc) · 11.5 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// @ts-check
// Generated on 2015-05-16 using generator-jekyllized 0.7.3
"use strict";
const ghPages = require("gh-pages");
const path = require("path");
const gulp = require("gulp");
const shell = require("gulp-shell");
const sass = require("gulp-sass");
const autoprefixer = require("gulp-autoprefixer");
const size = require("gulp-size");
const imagemin = require("gulp-imagemin");
const changed = require("gulp-changed");
const useref = require("gulp-useref");
const gulpIf = require("gulp-if");
const revAll = require("gulp-rev-all");
const revReplace = require("gulp-rev-replace");
const uglify = require("gulp-uglify");
const minifyCss = require("gulp-minify-css");
const htmlmin = require("gulp-htmlmin");
const jshint = require("gulp-jshint")
// "del" is used to clean out directories and such
const del = require("del");
// Used to grab the filename of the docs to use in the doc headers
const tap = require('gulp-tap');
// BrowserSync isn"t a gulp package, and needs to be loaded manually
const browserSync = require("browser-sync");
// TypeScript compiler
const tsc = require("gulp-typescript");
// Need a command for reloading webpages using BrowserSync
const reload = browserSync.reload;
// Deletes the directory that is used to serve the site during development
gulp.task("clean:dev", del.bind(null, ["serve"]));
// Deletes the directory that the optimized site is output to
gulp.task("clean:prod", del.bind(null, ["site"]));
//Deletes both directories above...
gulp.task("clean", function (cb) {
del(["serve","site"], cb);
});
gulp.task("docs", function () {
const endrawLiquidTag = Buffer.from("\n\n{% endraw %}");
gulp.src(["./TypeScript-Handbook/pages/**/*.md"], { base: "./TypeScript-Handbook/pages" })
.pipe(tap(function (file) {
const relativeFilePath = path.relative(path.join(file.cwd, file.base), file.path).replace(/\\/g, "/");
let directoryName = relativeFilePath.substring(0, relativeFilePath.lastIndexOf("/"));
if (directoryName === "tutorials") {
// tutorials were published to the root directory, so to avoid breaking links for published files
// treat this folder as if it is on the root.
directoryName = "";
}
const baseFileName = path.basename(relativeFilePath, ".md");
const relativeFileName = (directoryName ? directoryName + "/" : "") + baseFileName;
const outputFileName = relativeFileName.toLowerCase().replace(/\s+|\.|'/g, "-") + ".html";
const preamble = [
"---",
"title: " + baseFileName,
"layout: docs",
"permalink: /docs/handbook/" + outputFileName,
"---",
"{% raw %}"
].join("\n");
file.contents = Buffer.concat([
Buffer.from(preamble),
file.contents,
endrawLiquidTag
]);
}))
.pipe(gulp.dest("./src/_docs/handbook"));
});
// gulp.task("handbook-templates", function () {
// return gulp.src("TypeScript-Handbook/pages/declaration files/templates/**")
// .pipe(gulp.dest("./src/_docs/handbook/templates"));
// });
// Runs the build command for Jekyll to compile the site locally
// This will build the site with the production settings
gulp.task("jekyll:dev", shell.task("bundle exec jekyll build -I"));
gulp.task("jekyll-rebuild", ["jekyll:dev"], function () {
reload;
});
// Almost identical to the above task, but instead we load in the build configuration
// that overwrites some of the settings in the regular configuration so that you
// don"t end up publishing your drafts or future posts
gulp.task("jekyll:prod", ["docs"], shell.task("bundle exec jekyll build --config _config.yml,_config.build.yml"));
// Compiles the SASS files and moves them into the "assets/stylesheets" directory
gulp.task("styles", function () {
// Looks at the style.scss file for what to include and creates a style.css file
return gulp.src("src/assets/scss/style.scss")
.pipe(sass())
// AutoPrefix your CSS so it works between browsers
.pipe(autoprefixer("last 1 version", { cascade: true }))
// Directory your CSS file goes to
.pipe(gulp.dest("src/assets/stylesheets/"))
.pipe(gulp.dest("serve/assets/stylesheets/"))
// Outputs the size of the CSS file
.pipe(size({title: "styles"}))
// Injects the CSS changes to your browser since Jekyll doesn"t rebuild the CSS
.pipe(reload({stream: true}));
});
// Updates the file: src/_data/urls.json
gulp.task("automate:download_urls", shell.task("node scripts/updateURLs.js"));
gulp.task("automate:download_search_assets", shell.task("node scripts/downloadSearchAssets.js"));
gulp.task("handbook-images", function() {
return gulp.src("./TypeScript-Handbook/assets/images/**")
.pipe(changed("site/assets/images"))
.pipe(imagemin({
// Lossless conversion to progressive JPGs
progressive: true,
// Interlace GIFs for progressive rendering
interlaced: true
}))
.pipe(gulp.dest("site/assets/images"))
.pipe(size({title: "images"}));
});
// Optimizes the images that exists
gulp.task("images", function () {
return gulp.src("src/assets/images/**")
.pipe(changed("site/assets/images"))
.pipe(imagemin({
// Lossless conversion to progressive JPGs
progressive: true,
// Interlace GIFs for progressive rendering
interlaced: true
}))
.pipe(gulp.dest("site/assets/images"))
.pipe(size({title: "images"}));
});
// Copy over fonts to the "site" directory
gulp.task("fonts", function () {
return gulp.src("src/assets/fonts/**")
.pipe(gulp.dest("site/assets/fonts"))
.pipe(size({ title: "fonts" }));
});
// Copy over scripts ignored when minifing...
gulp.task("scripts", ["sample-script", "playground", "examples"], function () {
// Better hope nothing is wrong.
});
// Copy over the sample scripts to the "site" directory
// gulp.task("examples", function () {
// return gulp.src("src/examples/*.ts")
// .pipe(gulp.dest("site/examples/"))
// .pipe(size({ title: "examples" }));
// });
// Copy over the sample scripts to the "site" directory
gulp.task("sample-script", function () {
return gulp.src("src/samples/*.js")
.pipe(gulp.dest("site/samples/"))
.pipe(size({ title: "sample-script" }));
});
// Copy over the playground scripts to the "site" directory
gulp.task("playground", function () {
return gulp.src("src/play/public/**/*")
.pipe(gulp.dest("site/play"))
.pipe(size({ title: "playground" }));
});
gulp.task("playground:dev", function () {
return gulp.src("src/play/public/**/*")
.pipe(gulp.dest("serve/play"))
.pipe(size({ title: "playground" }));
});
// Copy CNANE file to the "site" directory
gulp.task("cname", function() {
return gulp.src("src/CNAME")
.pipe(gulp.dest("site"));
});
// Copy web.config file to the "site" directory
gulp.task("webconfig", function() {
return gulp.src("src/Web.config")
.pipe(gulp.dest("site"));
});
// Copy xml and txt files to the "site" directory
gulp.task("copy", function () {
return gulp.src(["serve/*.txt", "serve/*.xml"])
.pipe(gulp.dest("site"))
.pipe(size({ title: "xml & txt" }))
});
// Optimizes all the CSS, HTML and concats the JS etc
gulp.task("html", ["styles"], function () {
const assets = useref.assets({searchPath: "serve"});
return gulp.src("serve/**/*.html")
.pipe(assets)
// Concatenate JavaScript files and preserve important comments
.pipe(gulpIf("/\.js$/", uglify({ preserveComments: "some" })))
//.pipe(gulpIf("*.js", uglify({preserveComments: "some"})))
// Minify CSS
.pipe(gulpIf("/\.css$/b", minifyCss()))
//.pipe(gulpIf("*.css", minifyCss()))
// Start cache busting the files
.pipe(revAll({ ignore: [".eot", ".svg", ".ttf", ".woff",".png",".jpg"] }))
.pipe(assets.restore())
// Conctenate your files based on what you specified in _layout/header.html
.pipe(useref())
// Replace the asset names with their cache busted names
.pipe(revReplace())
// Minify HTML
.pipe(gulpIf("/\.html$/b", htmlmin({
removeComments: true,
removeCommentsFromCDATA: true,
removeCDATASectionsFromCDATA: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true
})))
// Send the output to the correct folder
.pipe(gulp.dest("site"))
.pipe(size({title: "optimizations"}));
});
gulp.task("deploy", function(cb) {
const now = new Date();
// Support deploying via a GitHub Actions token
const isActions = process.env.GITHUB_TOKEN;
const deployOptions = {
repo: "https://github.com/Microsoft/TypeScript-Website.git",
branch: "SITE-STAGING",
message: `Update ${now.toISOString()}.`,
};
if (isActions) {
deployOptions.repo = `https://${process.env.GITHUB_TOKEN}@github.com/Microsoft/TypeScript-Website.git`;
deployOptions.user = {
name: process.env.GITHUB_ACTOR,
email: process.env.GITHUB_ACTOR + '@users.noreply.github.com'
}
}
// Deploys your optimized site, you can change the settings in the html task if you want to
ghPages.publish(path.join(process.cwd(), "site"), deployOptions, cb);
});
// Run JS Lint against your JS
gulp.task("jslint", function () {
gulp.src("./serve/assets/javascript/*.js")
// Checks your JS code quality against your .jshintrc file
.pipe(jshint(".jshintrc"))
.pipe(jshint.reporter());
});
// Runs "jekyll doctor" on your site to check for errors with your configuration
// and will check for URL errors a well
gulp.task("doctor", shell.task("jekyll doctor"));
// Runs the script to generate the TOC for the playground examples
gulp.task("examples", ["examples:toc"], function() {
gulp.src(["Examples/**/*"]).pipe(gulp.dest("site/ex"))
gulp.src(["Examples/**/*"]).pipe(gulp.dest("serve/ex"))
});
gulp.task("examples:toc", shell.task("node Examples/scripts/generateTOC.js"))
// BrowserSync will serve our site on a local server for us and other devices to use
// It will also auto-reload across all devices as well as keep the viewport synchronized
// between them.
gulp.task("serve:dev", ["docs", "styles", "jekyll:dev", "playground:dev", "examples"], function () {
browserSync({
notify: true,
// tunnel: "",
server: {
baseDir: "serve"
}
});
});
// These tasks will look for files that change while serving and will auto-regenerate or
// reload the website accordingly. Update or add other files you need to be watched.
gulp.task("watch", function () {
gulp.watch(["src/**/*.md", "src/**/*.html", "src/**/*.xml", "src/**/*.txt", "src/**/*.js", "!src/play/**/*"], ["jekyll-rebuild"]);
gulp.watch(["serve/assets/stylesheets/*.css"], reload);
gulp.watch(["src/assets/scss/**/*.scss"], ["styles"]);
gulp.watch(["src/play/**/*"], ["playground:dev"]);
gulp.watch(["Examples/**/*"], ["examples"]);
});
// Serve the site after optimizations to see that everything looks fine
gulp.task("serve:prod", function () {
browserSync({
notify: false,
// tunnel: true,
server: {
baseDir: "site"
}
});
});
// Default task, run when just writing "gulp" in the terminal
gulp.task("default", ["serve:dev", "watch"]);
// Checks your CSS, JS and Jekyll for errors
gulp.task("check", ["jslint", "doctor"], function () {
// Better hope nothing is wrong.
});
// Builds the site but doesn't serve it to you
gulp.task("build", ["jekyll:prod", "styles", "examples"], function () {});
// Builds your site with the "build" command and then runs all the optimizations on
// it and outputs it to "./site"
gulp.task("publish", ["build"], function () {
gulp.start("automate:download_urls", "automate:download_search_assets", "html", "copy", "images", "handbook-images", "fonts", "scripts", "cname", "webconfig", "playground");
});