This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
118 lines (103 loc) · 2.91 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
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import opn from 'opn';
import baseConfig from './webpack.config.babel';
import exampleConfig from './webpack.config.example.babel';
const {webpackConfig, ip, port} = exampleConfig;
const $ = gulpLoadPlugins();
function compiler(config, callback) {
const compiler = webpack(config);
// run webpack
compiler.run((err, stats) => {
if (err) {
throw new $.util.PluginError('webpack', err);
}
$.util.log('[webpack]', stats.toString({
colors: true
}));
if (callback) {
return callback();
}
});
}
//清理临时和打包目录
gulp.task('clean', () => {
return gulp.src(['dist', 'lib'])
.pipe($.clean({force: true}));
});
gulp.task('build:lib', () => {
return gulp.src('src/**/*')
.pipe($.babel({
presets: ['es2015']
}))
.pipe(gulp.dest('lib'));
});
// 用webpack 打包编译
/*eslint-disable camelcase*/
gulp.task('webpack', () => {
let config = Object.create(baseConfig);
config.plugins = [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
];
compiler(config, () => {
let config = Object.create(baseConfig);
config.plugins = [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
}),
new webpack.optimize.UglifyJsPlugin({
compressor: {
screw_ie8: true,
warnings: false
}
})
];
config.output.filename = '[name].min.js';
compiler(config);
})
});
//先清理再打包
gulp.task('build:pre', ['build:lib', 'webpack']);
gulp.task('build', ['clean'], () => {
gulp.start('build:pre');
});
//gitbook
gulp.task('gitbook:clean', () => {
return gulp.src(['docs/_book'])
.pipe($.clean({force: true}));
});
gulp.task('gitbook:install', () => {
return $.run('cd docs && gitbook install').exec();
});
gulp.task('gitbook:serve', ['gitbook:install'], () => {
return $.run('cd docs && gitbook serve').exec();
});
gulp.task('gitbook:build', ['gitbook:clean', 'gitbook:install'], () => {
return $.run('cd docs && gitbook build').exec();
});
//example
gulp.task('example', () => {
// Start a webpack-dev-server
const compiler = webpack(webpackConfig);
new WebpackDevServer(compiler, webpackConfig.devServer)
.listen(port, ip, (err) => {
if (err) {
throw new $.util.PluginError('webpack-dev-server', err);
}
// Server listening
$.util.log('[webpack-dev-server]', `http://${ip}:${port}/`);
// keep the server alive or continue?
opn(port === '80' ? `http://${ip}` : `http://${ip}:${port}/`, {app: 'chrome'});
});
});
//默认任务
gulp.task('default', () => {
gulp.start('build');
gulp.start('gitbook:build');
});