forked from evolution-ui/evolution-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
268 lines (239 loc) · 10.5 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
'use strict';
//
// Dependencies
//
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import autoprefixer from 'autoprefixer';
import browserSync from 'browser-sync';
import del from 'del';
import path from 'path';
import runSequence from 'run-sequence';
import webpack from 'webpack-stream';
import webpackConfig from './webpack.config.babel.js';
//
// Configuration
//
const plugins = gulpLoadPlugins();
const config = {
source: 'source',
sourceAssets: 'source/assets',
sourceComponents: 'source/components',
dev: '.dev',
devAssets: '.dev/assets',
dist: 'dist',
docs: 'docs',
docsAssets: 'docs/assets',
docsSite: 'docs/_site',
docsComponents: 'docs/_components',
browsers: 'last 2 versions',
env: {
production: !!plugins.util.env.production,
docs: !!plugins.util.env.docs,
docsDeploy: !!plugins.util.env.docsDeploy
}
};
const postcssPlugins = [
autoprefixer(config.browsers)
];
//
// Tasks
//
// Default
// The default Gulp task is to initialize a server for local development
gulp.task('default', ['serve']);
// Serve
// Initialize a server for local development with browser-sync and watch files
// for changes
gulp.task('serve', ['build'], () => {
// initialize browser-sync for .dev|docs/
browserSync.init({
server: {
baseDir: config.env.docs ? `${config.docsSite}` : config.dev,
reloadOnRestart: true,
open: false
}
});
// watch for file changes and inject|reload
gulp.watch(`${config.source}/**/*.scss`, ['styles']);
gulp.watch([`${config.sourceAssets}/scripts/**/*.js`, `${config.sourceComponents}/**/*.js`], ['scripts']);
gulp.watch(`${config.devAssets}/scripts/*.js`).on('change', browserSync.reload);
gulp.watch(`${config.sourceComponents}/**/*.html`, ['html', browserSync.reload]);
if (config.env.docs) {
gulp.watch([
`!${config.docsSite}/**/*`,
`${config.docs}/**/*.{html,markdown,md,yml,json,txt,xml}`,
`!${config.docs}/_config.yml`,
`!${config.docs}/_config.prod.yml`,
`${config.docsComponents}/**/*`,
`${config.docs}/_data/**/*`,
`${config.docs}/_includes/**/*`,
`${config.docs}/_layouts/**/*`,
`${config.docs}/_plugins/**/*`,
`${config.docs}/_posts/**/*`,
`${config.docs}/assets/**/*`
], ['jekyll', browserSync.reload]);
}
});
// Build
// Sequence of tasks to run in order to build the project
gulp.task('build', callback => {
runSequence(
'clean',
[
'scripts',
'styles'
],
[
'html',
'images',
'audio'
],
...(config.env.docs ? ['jekyll'] : config.env.docsDeploy ? ['jekyll:prod'] : []),
...(config.env.docs || config.env.docsDeploy ? ['jekyll:minify-styles'] : []),
callback
);
});
// Clean
// Remove generated folders and files
gulp.task('clean', () => {
return del.sync([
config.dist,
config.dev,
config.docsComponents,
`${config.env.docsDeploy ? '' : '!'}${config.docsAssets}/images/evolution-ui`,
`${config.env.docsDeploy ? '' : '!'}${config.docsAssets}/audio/evolution-ui`,
`${config.docsAssets}/scripts/evolution-ui`,
`${config.docsAssets}/styles/evolution-ui`
]);
});
// Scripts
// Use webpack (with babel-loader) to transpile ES6, bundle modules,
// and copy/paste results to the correct location(s)
gulp.task('scripts', ['scripts:lint'], () => {
return gulp.src(`${config.sourceAssets}/scripts/index.js`)
.pipe(webpack(webpackConfig)) // bundle js modules
.pipe(plugins.concat('evolution-ui.js')) // rename compiled file
.pipe(gulp.dest(`${config.devAssets}/scripts/evolution-ui`)) // save to dev
.pipe(gulp.dest(config.dist)) // save to dist
.pipe(plugins.uglify()) // minify js
.pipe(plugins.rename({suffix: '.min'})) // add .min suffix
.pipe(gulp.dest(`${config.devAssets}/scripts/evolution-ui`)) // save minified file to dev
.pipe(gulp.dest(`${config.docsAssets}/scripts/evolution-ui`)) // save minified file to docs
.pipe(gulp.dest(config.dist)); // save minified file to dist
});
// Scripts: Lint
// Lint JS scripts to ensure they follow rules specified in .eslintrc
gulp.task('scripts:lint', () => {
return gulp.src(`${config.sourceComponents}/**/*.js`)
.pipe(plugins.eslint())
.pipe(plugins.eslint.format())
.pipe(plugins.eslint.failAfterError());
});
// Styles
// Compile Sass and copy/paste results to the correct location(s)
gulp.task('styles', () => {
return gulp.src(`${config.sourceAssets}/styles/main.scss`)
.pipe(plugins.sass().on('error', plugins.sass.logError)) // compile sass
.pipe(plugins.postcss(postcssPlugins)) // run through postcss processors (autoprefixer)
.pipe(plugins.rename({basename: 'evolution-ui'})) // rename compiled file
.pipe(gulp.dest(`${config.devAssets}/styles/evolution-ui`)) // save to dev
.pipe(gulp.dest(config.dist)) // save to dist
.pipe(browserSync.reload({stream: true})) // inject changes into browser
.pipe(plugins.cssmin()) // minify compiled file
.pipe(plugins.rename({suffix: '.min'})) // add .min suffix
.pipe(gulp.dest(`${config.devAssets}/styles/evolution-ui`)) // save to dev
.pipe(gulp.dest(`${config.docsAssets}/styles/evolution-ui`)) // save minified file to docs
.pipe(gulp.dest(config.dist)) // save minified file to dist
.pipe(browserSync.reload({stream: true})); // inject changes into browser
});
// HTML
// Sequence of HTML-related tasks
gulp.task('html', callback => {
runSequence(
'html:copy-to-dev',
'html:copy-to-docs',
callback
);
});
// HTML: Copy to Docs
// Rename and copy/paste raw HTML files to the /docs directory
gulp.task('html:copy-to-docs', () => {
return gulp.src([`${config.sourceComponents}/**/*.html`])
.pipe(plugins.changed(config.docsComponents)) // only transform changed files (faster)
.pipe(plugins.rename(file => { // prepend "directory name + hyphen" to filename
file.base = '/';
file.basename = file.dirname + '-' + file.basename;
}))
.pipe(plugins.flatten()) // flatten directory structure of HTML files
.pipe(gulp.dest(config.docsComponents)); // save to docs
});
// HTML: Copy to Dev
// Wrap raw HTML files in appropriate tags and copy/paste results to the .dev/
// directory so they can be served by browser-sync
gulp.task('html:copy-to-dev', () => {
// Wrap each HTML partial with root elements and include compiled assets
// Use minified versions if running "production mode"
const prefix = '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Evolution UI</title><link href="https://fonts.googleapis.com/css?family=Roboto+Slab:100,300,400" rel="stylesheet"><link href="https://fonts.googleapis.com/css?family=Roboto:400,400i,700,700i" rel="stylesheet"><link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"><script src="https://use.fontawesome.com/118670e9e2.js"></script><link rel="stylesheet" href="/assets/styles/evolution-ui/evolution-ui' + (config.env.production ? '.min' : '') + '.css"></head><body>';
const suffix = '<script src="/assets/scripts/evolution-ui/evolution-ui' + (config.env.production ? '.min' : '') + '.js"></script></body></html>';
return gulp.src(`${config.sourceComponents}/**/*.html`)
.pipe(plugins.changed(config.dev)) // only transform changed files (faster)
.pipe(plugins.frontMatter({property: 'frontMatter', remove: true})) // remove yml front matter
.pipe(plugins.insert.prepend(prefix)) // add prefix to source html file
.pipe(plugins.insert.append(suffix)) // add suffix to source html file
.pipe(gulp.dest(config.dev)); // save to dev
});
// Images
// Copy/paste images to the correct location(s), and optimize them for
// production builds
gulp.task('images', () => {
let stream = gulp.src(`${config.sourceComponents}/**/*.{png,gif,jpg,jpeg,svg}`);
if (config.env.docsDeploy || config.env.docs) { // optimize images for docs|docsDeploy
const docsDest = `${config.docsAssets}/images/evolution-ui`;
stream.pipe(plugins.changed(docsDest)) // only transform changed files (faster)
.pipe(plugins.imagemin()) // call imagemin only for docs and docsDeploy
.pipe(gulp.dest(docsDest)); // save to docs
} else {
const devDest = `${config.devAssets}/images/evolution-ui`;
stream.pipe(plugins.changed(devDest))
.pipe(gulp.dest(devDest)); // save to dev
}
return stream;
});
// Audio
// Copy/paste audio files to the correct location(s)
gulp.task('audio', () => {
let stream = gulp.src(`${config.sourceComponents}/**/*.{wav,mp3,aiff,flac,ogg,wma,webm}`);
if (config.env.docsDeploy || config.env.docs) { // optimize images for docs|docsDeploy
const docsDest = `${config.docsAssets}/audio/evolution-ui`;
stream.pipe(plugins.changed(docsDest)) // only transform changed files (faster)
.pipe(gulp.dest(docsDest)); // save to docs
} else {
const devDest = `${config.devAssets}/audio/evolution-ui`;
stream.pipe(plugins.changed(devDest))
.pipe(gulp.dest(devDest)); // save to dev
}
return stream;
});
// Jekyll
// Build Jekyll files for the /docs website
gulp.task('jekyll', plugins.shell.task([
`jekyll build --source=${config.docs} --destination=${config.docsSite} --config=${config.docs}/_config.yml`
]));
gulp.task('jekyll:prod', plugins.shell.task([
`jekyll build --source=${config.docs} --destination=${config.docsSite} --config=${config.docs}/_config.yml${`,${config.docs}/_config.prod.yml`}`
]));
// Jekyll: Minify Styles
// Minify the resulting CSS from each Sass compilation via Jekyll
gulp.task('jekyll:minify-styles', () => {
return gulp.src(`${config.docsSite}/assets/styles/main.css`)
.pipe(plugins.postcss(postcssPlugins)) // run through postcss processors (autoprefixer)
.pipe(plugins.cssmin()) // minify compiled file
.pipe(plugins.rename({suffix: '.min'})) // add .min suffix
.pipe(gulp.dest(`${config.docsSite}/assets/styles`)) // save minified file to dist
});
// Deploy
gulp.task('deploy', ['build'], function () {
return gulp.src(`${config.docsSite}/**/*`)
.pipe(plugins.ghPages());
});