-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgulpfile.js
More file actions
203 lines (186 loc) · 5.3 KB
/
gulpfile.js
File metadata and controls
203 lines (186 loc) · 5.3 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
'use strict';
const fs = require('fs');
const path = require('path');
const del = require('del');
const runSequence = require('run-sequence');
const through = require('through2');
const swig = require('swig');
const gulp = require('gulp');
const ghPagesDeploy = require('gulp-gh-pages');
const marked = require('marked');
const highlightjs = require('highlight.js');
const webpack = require('gulp-webpack');
const gulpLoadPlugins = require('gulp-load-plugins');
const pkg = require('./package.json');
const webpackConfig = require('./webpack.config.js');
const connect = require('gulp-connect');
const $ = gulpLoadPlugins();
const hostedLibsUrlPrefix = 'https://localhost';
const AUTOPREFIXER_BROWSERS = [
'ie >= 9',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
];
gulp.task('scripts',/* ['lint'],*/ () => {
return gulp.src('./src/index.js')
.pipe(webpack(webpackConfig))
.pipe(gulp.dest('./dist'));
});
// Optimize Images
gulp.task('images', () => {
return gulp.src('src/**/*.{svg,png,jpg}')
.pipe($.flatten())
.pipe($.cache($.imagemin({
progressive: true,
interlaced: true
})))
.pipe(gulp.dest('dist/images'))
.pipe($.size({title: 'images'}));
});
/**
* Generates an HTML file based on a template and file metadata.
*/
swig.setDefaults({ cache: false });
function applyTemplate() {
return through.obj((file, enc, cb) => {
const data = {
site: {},
page: file.page,
content: file.contents.toString()
};
const templateFile = path.join(
__dirname, 'docs', '_templates', `${file.page.layout}.html`);
const tpl = swig.compileFile(templateFile);
file.contents = new Buffer(tpl(data));
cb(null, file);
});
}
function getMarkedConfig(taskName) {
let renderer = new marked.Renderer();
renderer.code = function(code, lang) {
let demo = '';
if (lang === 'html_demo') {
lang = 'html';
demo = `<div class="html_demo">${code}</div>`;
}
lang = typeof lang === 'string' && highlightjs.getLanguage(lang) ? lang : false;
code = lang ? highlightjs.highlight(lang, code).value : highlightjs.highlightAuto(code).value;
return `${demo}<pre><code class="hljs ${lang||''}">${code}</code></pre>`;
};
return { renderer };
}
/**
* Generates an index.html file for each README in MDL/src directory.
*/
gulp.task('components', () => {
return gulp.src('src/**/README.md', {base: 'src'})
// Add basic front matter.
.pipe($.header('---\nlayout: component\ninclude_prefix: ../../\n---\n\n'))
.pipe($.frontMatter({
property: 'page',
remove: true
}))
.pipe($.marked(getMarkedConfig('components')))
.pipe((() => {
return through.obj((file, enc, cb) => {
file.page.component = file.relative.split('/')[0];
cb(null, file);
});
})())
.pipe(applyTemplate())
.pipe($.rename(path => path.basename = 'index'))
.pipe(gulp.dest('dist/components'));
});
/**
* Generates an HTML file for each md file in _pages directory.
*/
gulp.task('pages', ['components'], () => {
return gulp.src('docs/_pages/*.md')
.pipe($.frontMatter({
property: 'page',
remove: true
}))
.pipe($.marked(getMarkedConfig('pages')))
.pipe(applyTemplate())
.pipe($.replace('$$version$$', pkg.version))
.pipe($.replace('$$hosted_libs_prefix$$', hostedLibsUrlPrefix))
.pipe($.rename(path => {
if (path.basename !== 'index') {
path.dirname = path.basename;
path.basename = 'index';
}
}))
.pipe(gulp.dest('dist'));
});
/**
* Copies assets from MDL and _assets directory.
*/
gulp.task('assets', () => {
return gulp.src([
'docs/_assets/**/*',
'node_modules/highlight.js/lib/highlight.js',
'node_modules/highlight.js/styles/github-gist.css',
])
.pipe($.if(/\.js/i, $.replace('$$version$$', pkg.version)))
.pipe($.if(/\.js/i, $.replace('$$hosted_libs_prefix$$', hostedLibsUrlPrefix)))
.pipe($.if(/\.(svg|jpg|png)$/i, $.imagemin({
progressive: true,
interlaced: true
})))
.pipe($.if(/\.css/i, $.autoprefixer(AUTOPREFIXER_BROWSERS)))
.pipe($.if(/\.css/i, $.csso()))
.pipe($.if(/\.js/i, $.uglify({
preserveComments: 'some',
sourceRoot: '.',
sourceMapIncludeSources: true
})))
.pipe(gulp.dest('dist/assets'));
});
/**
* Defines the list of resources to watch for changes.
*/
function watch() {
gulp.watch(['src/**/*.js'], ['scripts']);
gulp.watch(['src/**/*.{svg,png,jpg}'], ['images']);
gulp.watch(['src/**/README.md'], ['pages']);
gulp.watch(['docs/**/*'], ['pages', 'assets']);
}
// Clean Output Directory
gulp.task('clean', () => del(['dist', '.publish']));
// Build Production Files, the Default Task
gulp.task('default', ['clean'], cb => {
runSequence(
['scripts'],
cb);
});
// Build production files and microsite
gulp.task('build', ['clean'], cb => {
runSequence(
['scripts'],
['assets', 'pages', 'images'],
cb);
});
/**
* Serves the landing page from "out" directory.
*/
gulp.task('serve', () => {
connect.server({
root: 'dist',
port: 3000,
livereload: true
});
watch();
});
/**
* publish the docs from "dist" directory to gh-pages branch.
*/
gulp.task('publish:docs', ['build'], () => {
return gulp.src("dist/**/*")
.pipe(ghPagesDeploy())
});