forked from Polymer/old-docs-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
329 lines (289 loc) · 10.5 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
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
/*
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
'use strict';
// let gulp = require('gulp');
let gulp = require('gulp-help')(require('gulp'));
let $ = require('gulp-load-plugins')();
let matter = require('gulp-gray-matter');
let styleMod = require('gulp-style-modules');
let cssslam = require('css-slam');
let argv = require('yargs').argv;
let browserSync = require('browser-sync').create();
let del = require('del');
let fs = require('fs');
let markdownIt = require('markdown-it')({
html: true,
highlight: (code, lang) => {
let highlightjs = require('highlight.js')
if (lang && highlightjs.getLanguage(lang)) {
try {
return highlightjs.highlight(lang, code).value;
} catch (__) { console.log(__) }
} else {
try {
return highlightjs.highlightAuto(code).value;
} catch (__) { console.log(__) }
}
return ''; // use external default escaping
}
});
let markdownItAttrs = require('markdown-it-attrs');
let merge = require('merge-stream');
let path = require('path');
let runSequence = require('run-sequence');
let toc = require('toc');
let AUTOPREFIXER_BROWSERS = ['last 2 versions', 'ios 8', 'Safari 8'];
markdownIt.use(markdownItAttrs);
// keep markdownIt from escaping template markup.
markdownIt.normalizeLink = function(link) { return link; }
markdownIt.validateLink = function(link) { return true; }
function minifyHtml() {
return $.minifyHtml({quotes: true, empty: true, spare: true});
}
function uglifyJS() {
return $.uglify({preserveComments: 'some'});
}
function license() {
return $.license('BSD2', {
organization: 'The Polymer Project Authors. All rights reserved.',
tiny: true
});
}
// reload is a noop unless '--reload' cmd line arg is specified.
let reload = function() {
return new require('stream').PassThrough({objectMode: true});
}
if (argv.reload) {
reload = browserSync.reload;
}
function createReloadServer() {
browserSync.init({
notify: true,
open: !!argv.open,
proxy: 'localhost:8080' // proxy serving through app engine.
});
}
gulp.task('style', 'Compile sass, autoprefix, and minify CSS', function() {
let sassOpts = {
precision: 10,
outputStyle: 'expanded',
onError: console.error.bind(console, 'Sass error:')
};
return gulp.src('app/sass/**/*.scss')
.pipe($.changed('dist/css'))
.pipe($.sass(sassOpts))
.pipe($.autoprefixer(AUTOPREFIXER_BROWSERS))
.pipe($.cssmin()) // Minify and add license
.pipe(license())
.pipe(gulp.dest('dist/css'))
});
// gulp.task('style:modules', 'Wrap CSS in Polymer style modules', function() {
// return gulp.src('node_modules/highlight.js/styles/github.css')
// .pipe($.rename({basename: 'syntax-color'}))
// .pipe($.autoprefixer(AUTOPREFIXER_BROWSERS))
// .pipe(styleMod({
// //filename: 'syntax-color',
// // moduleId: function(file) {
// // return 'syntax-color';//path.basename(file.path, path.extname(file.path)) + '-css';
// // }
// }))
// .pipe(gulp.dest('dist/css'))
// });
gulp.task('images', 'Optimize images', function() {
return gulp.src('app/images/**/*')
.pipe($.changed('dist/images'))
.pipe($.imagemin({
progressive: true,
interlaced: true,
svgoPlugins: [{convertTransform: false}]
}))
.pipe(gulp.dest('dist/images'));
});
function convertMarkdownToHtml(file, templateName) {
let data = file.data;
data.file = file;
data.content = markdownIt.render(file.content); // Markdown -> HTML.
data.title = data.title || '';
data.subtitle = data.subtitle || '';
// If there is a table of contents, toc-ify it. Otherwise, wrap the
// original markdown content anyway, so that we can style it.
if (data.content.match(/<!--\s*toc\s*-->/gi)) {
// Leave a trailing opening <div class="article-wrapper"><article> in the TOC, so that we can wrap the original
// markdown content into a div, for styling
data.content = toc.process(data.content, {
header: '<h<%= level %><%= attrs %> id="<%= anchor %>" class="has-permalink"><%= header %></h<%= level %>>',
TOC: '<div class="details-wrapper"><details id="toc"><summary>Contents</summary><%= toc %></details></div><div class="article-wrapper"><article>',
openUL: '<ul data-depth="<%= depth %>">',
closeUL: '</ul>',
openLI: '<li data-level="H<%= level %>"><a href="#<%= anchor %>"><%= text %></a>',
closeLI: '</li>',
tocMax: 3,
anchor: function(header, attrs) {
// if we have an ID attribute, use that, otherwise
// use the default slug
var id = attrs.match(/(?:^|\s+)id="([^"]*)"/)
return id ? id[1] : toc.anchor(header);
}
}) + '</article></div>';
} else {
data.content = '<div class="article-wrapper"><article>' + data.content + '</article></div>';
}
$.util.replaceExtension(file, '.html'); // file.md -> file.html
let tmpl = fs.readFileSync(templateName);
let renderTemplate = $.util.template(tmpl);
return renderTemplate(data);
}
gulp.task('md:docs', 'Docs markdown -> HTML conversion. Syntax highlight and TOC generation', function() {
return gulp.src([
'app/**/*.md',
'!app/1.0/blog/*.md',
'!app/{bower_components,elements,images,js,sass}/**',
], {base: 'app/'})
.pipe(matter(function(file) { // pull out front matter data.
return convertMarkdownToHtml(file, 'templates/page.template');
}))
.pipe($.rename({extname: '.html'}))
.pipe(gulp.dest('dist'));
});
gulp.task('md:blog', 'Blog markdown -> HTML conversion. Syntax highlight and TOC generation', function() {
return gulp.src([
'app/1.0/blog/*.md',
], {base: 'app/'})
.pipe(matter(function(file) { // pull out front matter data.
return convertMarkdownToHtml(file, 'templates/blog.template');
}))
.pipe($.rename({extname: '.html'}))
.pipe(gulp.dest('dist'));
});
// // Minify html
// gulp.task('html', function() {
// gulp.src('app/index.html')
// //.pipe($.changed('dist/index.html'))
// .pipe(minifyHtml())
// .pipe(gulp.dest('dist'));
// });
gulp.task('jshint', 'Lint JS', function() {
return gulp.src([
'gruntfile.js',
'app/js/**/*.js',
'app/elements/**/*.js',
'app/elements/**/*.html'
])
.pipe($.changed('dist/js'))
.pipe($.jshint.extract()) // Extract JS from .html files
.pipe($.jshint({esnext: true}))
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.if(!browserSync.active, $.jshint.reporter('fail')));
});
gulp.task('js', 'Minify JS to dist/', ['jshint'], function() {
return gulp.src(['app/js/**/*.js'])
.pipe(uglifyJS()) // Minify js output
.pipe(gulp.dest('dist/js'));
});
gulp.task('vulcanize', 'Vulcanize elements to dist/', function() {
return gulp.src('app/elements/elements.html')
// .pipe($.changed('dist/elements'))
.pipe($.vulcanize({
stripComments: true,
inlineCss: true,
inlineScripts: true
}))
.pipe($.crisper()) // Separate HTML/JS into separate files.
.pipe($.if('*.html', minifyHtml())) // Minify html output
.pipe($.if('*.html', cssslam.gulp())) // Minify css in HTML output
.pipe($.if('*.js', uglifyJS())) // Minify js output
.pipe($.if('*.js', license()))
.pipe(gulp.dest('dist/elements'));
});
gulp.task('vulcanize-demos', 'vulcanize demos', function() {
return gulp.src('app/1.0/homepage/*/index.html', {base: 'app/1.0/homepage'})
.pipe($.vulcanize({
stripComments: true,
inlineCss: true,
inlineScripts: true
}))
.pipe($.crisper()) // Separate HTML/JS into separate files.
.pipe($.if('*.html', minifyHtml())) // Minify html output
.pipe($.if('*.html', cssslam.gulp())) // Minify css in HTML output
.pipe($.if('*.js', uglifyJS())) // Minify js output
.pipe($.if('*.js', license()))
.pipe(gulp.dest('dist/1.0/homepage'));
});
gulp.task('copy', 'Copy site files (polyfills, templates, etc.) to dist/', function() {
let app = gulp.src([
'*',
'app/manifest.json',
'!{README.md,package.json,gulpfile.js,test_runner.py}',
], {nodir: true})
.pipe(gulp.dest('dist'));
let docs = gulp.src([
'app/**/*.html',
'app/**/nav.yaml',
'app/**/blog.yaml',
'app/**/authors.yaml',
'!app/{bower_components,elements}/**',
'!app/1.0/homepage/**',
], {base: 'app/'})
.pipe(gulp.dest('dist'));
let gae = gulp.src([
'{templates,lib}/**/*'
])
.pipe(gulp.dest('dist'));
let bower = gulp.src([
'app/bower_components/webcomponentsjs/webcomponents*.js'
], {base: 'app/'})
.pipe(gulp.dest('dist'));
let highlight = gulp.src([
'node_modules/highlight.js/lib/*'
])
.pipe(gulp.dest('dist/bower_components/highlight'));
let summit = gulp.src([
'app/summit/**/*',
'app/summit/*',
], {base: 'app'})
.pipe(gulp.dest('dist'));
let bower_summit = gulp.src([
'app/bower_components/webcomponentsjs/webcomponents*.js'
], {base: 'app/'})
.pipe(gulp.dest('dist/summit'));
return merge(app, docs, gae, bower, highlight, summit, bower_summit);
});
gulp.task('watch', 'Watch files for changes', function() {
createReloadServer();
gulp.watch('app/sass/**/*.scss', ['style', reload]);
gulp.watch('app/elements/**/*', ['vulcanize', reload]);
gulp.watch('app/js/*.js', ['js', reload]);
gulp.watch('app/1.0/blog/*.md', ['md:blog', reload]);
gulp.watch('app/**/*.md', ['md:docs', reload]);
gulp.watch(['templates/*.html', 'app/**/*.html'], ['copy', reload]);
// Watch for changes to server itself.
gulp.watch('*.py', function(files) {
gulp.src('*.py').pipe(gulp.dest('dist'));
reload();
});
gulp.watch('*.{yaml,yml}', function(files) {
gulp.src('*.{yml,yaml}').pipe(gulp.dest('dist'));
reload();
});
}, {
options: {
'reload': 'Reloads browser tab when watched files change',
'open': 'Opens a browser tab when launched'
}
});
gulp.task('clean', 'Remove dist/ and other built files', function() {
return del(['dist', 'app/css']);
});
// Default task. Build the dest dir.
gulp.task('default', 'Build site', ['clean', 'jshint'], function(done) {
runSequence(
['style', 'images', 'vulcanize', 'vulcanize-demos', 'js'],
'copy', 'md:docs', 'md:blog',
done);
});