gulpfile.js
7.26 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
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
/**
* Yohobuy 构建脚本
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/4/25
*/
'use strict';
const gulp = require('gulp');
const gutil = require('gulp-util');
const ftp = require('gulp-ftp');
const postcss = require('gulp-postcss');
const sourcemaps = require('gulp-sourcemaps');
const cssnano = require('gulp-cssnano');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const webpackConfig = require('./webpack.config.js');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const Dashboard = require('webpack-dashboard');
const DashboardPlugin = require('webpack-dashboard/plugin');
const env = {
dev: Symbol('development'),
pro: Symbol('production')
};
const config = require('../package.json');
const ftpConfig = {
host: '218.94.75.58',
user: 'php',
pass: 'yoho9646'
};
const distRoot = `dist/${config.name}`;
const dist = {
js: `${distRoot}/${config.version}`,
css: `${distRoot}/${config.version}`,
assets: `${distRoot}/assets`,
img: `${distRoot}/assets/img`,
font: `${distRoot}/assets/font`
};
/**
* postcss plugins for both dev and pro
* @parem et Symbol
*/
const postcssPlugin = (et) => {
let sprites = {
spritesmith: {
padding: 2
},
filterBy(file) {
// base64 的图片没有 url 过滤掉
if (file.url) {
return Promise.resolve();
}
return Promise.reject();
},
groupBy(file) {
let group = file.url.split('/')[1];
group = group === '' ? 'yo' : group;
file.retina = true;
return group ? Promise.resolve(group) : Promise.reject(group);
}
},
assets,
plugins;
// assets & sprites config in both dev and pro
if (et === env.pro) {
assets = {
loadPaths: [dist.img, dist.font],
relativeTo: dist.css
};
Object.assign(sprites, {
basePath: dist.img,
stylesheetPath: dist.css,
spritePath: dist.img
});
} else if (et === env.dev) {
assets = {
loadPaths: ['img/', 'font/'],
relativeTo: 'css/'
};
Object.assign(sprites, {
basePath: 'img/',
stylesheetPath: 'css/',
spritePath: 'img/'
});
}
plugins = [
require('precss'),
require('postcss-sprites').default(sprites),
require('postcss-assets')(assets),
require('postcss-calc'),
require('postcss-pxtorem')({
rootValue: 40,
unitPrecision: 5, // 保留5位小数字
minPixelValue: 2, // 小于 2 时,不转换
selectorBlackList: [], // 选择器黑名单,可以使用正则
propWhiteList: [] // 属性名称为空,表示替换所有属性的值
}),
require('autoprefixer')({
browsers: ['> 1%']
}),
// 可选
require('postcss-use')({
modules: ['postcss-clearfix', 'postcss-crip', 'postcss-short', 'postcss-center', 'postcss-position']
})
];
if (et === env.pro) {
plugins.push(require('postcss-cachebuster')({
imagesPath: `/${dist.img}`,
cssPath: `/${dist.css}`
}));
}
return plugins;
};
// default
gulp.task('default', ['postcss-dev', 'postcss-watch', 'webpack-dev-server']);
// ge
gulp.task('ge', ['postcss', 'webpack']);
// dist
gulp.task('dist', ['ge'], () => {
const ftpstream = ftp(ftpConfig);
return gulp.src('dist/**/')
.pipe(ftpstream)
.pipe(gutil.noop());
});
// postcss compile in dev
gulp.task('postcss-dev', () => {
return gulp.src('scss/index.css')
.pipe(sourcemaps.init())
.pipe(postcss(postcssPlugin(env.dev), {
parser: require('postcss-scss')
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('css/'));
});
// postcss file watch
gulp.task('postcss-watch', () => {
gulp.watch('scss/**/*.css', ['postcss-dev']);
});
// copy assets
gulp.task('assets', ['img', 'font']);
// copy img
gulp.task('img', () => {
return gulp.src('img/**/*')
.pipe(gulp.dest(dist.img));
});
// copy font
gulp.task('font', () => {
return gulp.src('font/*')
.pipe(gulp.dest(dist.font));
});
// postcss compile in pro
gulp.task('postcss', ['assets'], () => {
return gulp.src('scss/index.css')
.pipe(postcss(postcssPlugin(env.pro), {
parser: require('postcss-scss')
}))
.pipe(cssnano({
safe: true
}))
.pipe(gulp.dest(dist.css));
});
// webpack dev server
gulp.task('webpack-dev-server', () => {
const useDashboard = process.platform === 'darwin' || process.platform === 'linux';
const devConfig = Object.assign({}, webpackConfig, {
devtool: '#inline-source-map',
vue: {
postcss: {
plugins: postcssPlugin(env.dev),
options: {
parser: require('postcss-scss')
}
},
autoprefixer: false,
loaders: {
css: ExtractTextPlugin.extract(['css?-url&sourceMap'])
}
}
});
devConfig.output.publicPath = 'http://localhost:5004/';
// 开发环境插件
devConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
if (useDashboard) {
const dashboard = new Dashboard(); // webpackDashboardPlugin
devConfig.plugins.push(new DashboardPlugin(dashboard.setData));
}
devConfig.entry.libs.unshift(`webpack-dev-server/client?${devConfig.output.publicPath}`, 'webpack/hot/dev-server');
new WebpackDevServer(webpack(devConfig), {
contentBase: '.',
publicPath: devConfig.output.publicPath,
hot: true,
inline: true,
quiet: useDashboard,
stats: {
colors: true
},
headers: {
'Access-Control-Allow-Origin': '*'
}
}).listen(5004, '0.0.0.0', (err) => {
if (err) {
throw new gutil.PluginError('webpack-dev-server', err);
}
gutil.log('[webpack-serve]', devConfig.output.publicPath);
});
});
// webpack compile in pro
gulp.task('webpack', ['assets'], (done) => {
const proConfig = Object.assign({}, webpackConfig, {
vue: {
postcss: {
plugins: postcssPlugin(env.pro),
options: {
parser: require('postcss-scss')
}
},
autoprefixer: false,
loaders: {
css: ExtractTextPlugin.extract(['css?-url'])
}
}
});
proConfig.output.path = dist.js;
// TODO: 生产环境加载的插件独立出来
proConfig.plugins.push(new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}));
proConfig.plugins.push(new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}));
webpack(proConfig, (err, stats) => {
if (err) {
throw new gutil.PluginError('webpack', err);
}
gutil.log('[webpack compile]:', stats.endTime - stats.startTime, 'ms');
done();
});
});