forked from robyth/amazeui-touch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
292 lines (252 loc) · 7.13 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* Amaze UI Touch Building Tasks
*
* @author Minwe <[email protected]>
*/
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import del from 'del';
import runSequence from 'run-sequence';
import webpack from 'webpack-stream';
import webpackConfig from './webpack.config';
import browserify from'browserify';
import watchify from'watchify';
import source from'vinyl-source-stream';
import buffer from'vinyl-buffer';
import merge from 'merge-stream';
import markedify from 'markedify';
import BS from 'browser-sync';
import pkg from './package.json';
import getMarked from './docs/_utils/getMarked';
const ENV = process.env.NODE_ENV;
const $ = gulpLoadPlugins();
const isProduction = ENV === 'production' || ENV === 'travisci';
const banner = `/** ${pkg.title} v${pkg.version} | by Amaze UI Team
* (c) ${$.util.date(Date.now(), 'UTC:yyyy')} AllMobilize, Inc., Licensed under ${pkg.license}
* ${$.util.date(Date.now(), 'isoDateTime')}
*/
`;
const paths = {
scss: 'src/scss/amazeui.touch.scss',
scssModules: 'src/scss/**/*.scss',
fonts: 'src/fonts/*',
jsEntry: 'src/js/index.js',
dist: 'dist',
};
const docsDir = 'docs/_app';
const docsPaths = {
js: `${docsDir}/js/app.js`,
styleDir: `${docsDir}/style`,
style: `${docsDir}/style/app.scss`,
dist: 'www',
ksEntry: 'kitchen-sink/app.js',
ksIndex: 'kitchen-sink/index.html',
ksDist: 'www/kitchen-sink'
};
/*
// move to package.json
const babelOptions = {
optional: ['es7.objectRestSpread'],
plugins: ['object-assign'],
};*/
const autoprefixerOptions = {
browsers: ['> 1%', 'last 2 versions', 'ie 10']
};
const replaceVersion = function() {
return $.replace('__VERSION__', pkg.version);
};
const addBanner = function() {
return $.header(banner);
};
const buildBanner = function() {
return $.header(`/*! Updated: ${$.util.date(Date.now(), 'isoDateTime')} */`);
};
gulp.task('clean', () => {
return del(['dist', 'www', 'lib']);
});
/**
* Build Amaze UI Touch
*/
gulp.task('build:clean', () => {
return del([paths.dist, 'lib']);
});
gulp.task('style:scss', () => {
return gulp.src(paths.scss)
.pipe($.sass({
outputStyle: 'expanded'
}).on('error', $.sass.logError))
.pipe($.autoprefixer(autoprefixerOptions))
.pipe(addBanner())
.pipe(gulp.dest(paths.dist))
.pipe($.if(!isProduction, gulp.dest(docsPaths.ksDist)))
.pipe($.csso())
.pipe(addBanner())
.pipe($.rename({suffix: '.min'}))
.pipe(gulp.dest(paths.dist))
.pipe($.if(isProduction, gulp.dest(docsPaths.ksDist)));
});
gulp.task('style:fonts', () => {
return gulp.src(paths.fonts)
.pipe(gulp.dest(paths.dist + '/fonts'))
.pipe(gulp.dest(docsPaths.ksDist + '/fonts'));
});
gulp.task('style:watch', () => {
gulp.watch(paths.scssModules, ['style:scss']);
});
gulp.task('style', ['style:scss', 'style:fonts']);
gulp.task('styleDev', ['style:scss', 'style:fonts', 'style:watch']);
// transform ES6 & JSX
gulp.task('build:babel', () => {
return gulp.src('src/js/**/*')
.pipe(replaceVersion())
.pipe($.babel())
.pipe(gulp.dest('lib'));
});
// 由于 browserify-shim 打包 UMD 不会自动添加 CommonJS、AMD 依赖,
// 所以使用 webpack 打包。考虑将 browserify 的工作都转移到 webpack 以减少 npm 依赖数量。
gulp.task('build:pack', () => {
return gulp.src(paths.jsEntry)
.pipe(webpack(webpackConfig))
.pipe(replaceVersion())
.pipe(addBanner())
.pipe($.rename('amazeui.touch.js'))
.pipe(gulp.dest(paths.dist))
.pipe($.uglify())
.pipe(addBanner())
.pipe($.rename({suffix: '.min'}))
.pipe(gulp.dest(paths.dist));
});
gulp.task('build', (callback) => {
runSequence(
'build:clean',
['style', 'build:babel', 'build:pack',],
callback
);
});
/**
* Build Amaze UI Touch Website
*/
gulp.task('docs:clean', () => {
return del(docsPaths.dist);
});
let bsf = (options) => {
const babelify = ['babelify'];
let transform = Array.isArray(options.transform) ?
babelify.concat(options.transform) : babelify;
return watchify(browserify({
cache: {},
packageCache: {},
entries: options.entries,
debug: !isProduction,
transform: transform,
// path map for fake `amazeui-touch` in `./kitchen-sink/`
// @see https://github.com/vigetlabs/gulp-starter/issues/17
paths: ['./kitchen-sink/'],
}));
};
let bundler = (options) => {
let stream = (
options.b.bundle()
.on('error', $.util.log.bind($.util, 'Browserify Error'))
.pipe(source('app.js'))
.pipe(buffer())
.pipe(replaceVersion())
.pipe($.if(!isProduction, gulp.dest(options.dist)))
.pipe($.size({
title: `[${options.title}]`,
showFiles: true,
}))
);
return !isProduction ? stream : stream.pipe($.uglify())
.pipe($.rename({suffix: '.min'}))
.pipe(buildBanner())
.pipe(gulp.dest(options.dist))
.pipe($.size({
showFiles: true,
title: `[${options.title}] - minified`,
}))
.pipe($.size({
showFiles: true,
gzip: true,
title: `[${options.title}] - gzipped`,
}));
};
gulp.task('docs:js', () => {
const docBundle = bsf({
entries: [docsPaths.js],
transform: [[markedify, {marked: getMarked()}], 'brfs']
});
const docsBundleOptions = {
title: 'Docs',
b: docBundle,
dist: docsPaths.dist,
};
docBundle.on('update', bundler.bind(null, docsBundleOptions))
.on('log', $.util.log);
return bundler(docsBundleOptions);
});
gulp.task('docs:style', () => {
let stream = gulp.src(docsPaths.style)
.pipe($.sass({
outputStyle: 'expanded'
}).on('error', $.sass.logError))
.pipe($.autoprefixer(autoprefixerOptions));
return !isProduction ? stream.pipe(gulp.dest(docsPaths.dist)) :
stream.pipe($.csso())
.pipe(buildBanner())
.pipe($.rename({suffix: '.min'}))
.pipe(gulp.dest(docsPaths.dist));
});
gulp.task('docs:replace', () => {
const rFrom = '__ENV__';
const rTo = isProduction ? '.min' : '';
const replaceEnv = function(options) {
return gulp.src(options.src)
.pipe($.replace(rFrom, rTo))
// replace stat code on dev
.pipe($.replace(/<!--STAT_CODE_START-->(.+)<!--STAT_CODE_END-->/g,
(match, $1) => {
return isProduction ? $1 : '';
}))
.pipe(gulp.dest(options.dist));
};
let docs = replaceEnv({
src: `${docsDir}/index.html`,
dist: docsPaths.dist,
});
let ks = replaceEnv({
src: docsPaths.ksIndex,
dist: docsPaths.ksDist,
});
return merge(docs, ks);
});
gulp.task('docs:server', () => {
let bs = BS.create();
bs.init({
server: ['www'],
open: 'external',
});
gulp.watch(`${docsPaths.dist}/**/*`, bs.reload);
gulp.watch(`${docsPaths.styleDir}/*`, ['docs:style']);
});
// kitchen-sink
gulp.task('ks', () => {
const ksBundle = bsf({
entries: [docsPaths.ksEntry],
});
const ksOptions = {
title: 'kitchen-sink',
b: ksBundle,
dist: docsPaths.ksDist,
};
ksBundle.on('update', bundler.bind(null, ksOptions))
.on('log', $.util.log);
return bundler(ksOptions);
});
gulp.task('docs', (callback) => {
runSequence('docs:clean',
['styleDev', 'docs:style', 'docs:js', 'docs:replace', 'ks'],
'docs:server',
callback);
});
gulp.task('default', ['docs']);