-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
executable file
·163 lines (147 loc) · 4.36 KB
/
gulpfile.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
'use strict';
var config = require('./build/build.config.js');
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var runSequence = require('run-sequence');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
var pkg = require('./package');
var del = require('del');
var proxy = require('http-proxy-middleware');
var imagemin = require('gulp-imagemin');
var gulpIgnore = require('gulp-ignore');
gulp.task('copy', function() {
return gulp.src([
config.base + '/**/*',
'./src/js/mpm_webp.js',
'!' + config.base + '/*.html',
'!' + config.images,
'!' + config.js,
'!' + config.mainScss,
]).pipe(gulp.dest(config.dist))
.pipe($.size({
title: 'copy'
}));
});
gulp.task('copy:js', function() {
//uglify 报错 异常下的 js 复制
return gulp.src([
'./src/js/mpm_webp.js',
])
.pipe(gulp.dest(config.dist + '/js'))
.pipe($.size({
title: 'copy:js'
}));
});
// optimize images and put them in the dist folder
gulp.task('images', function() {
return gulp.src(config.images)
.pipe(imagemin({
progressive: true,
interlaced: true
}))
.pipe(gulp.dest(config.dist + '/img'))
.pipe($.size({
title: 'images'
}));
});
// optimize images and put them in the dist folder
gulp.task('uglify:js', function() {
return gulp.src([
config.js,
'!./src/js/mpm_webp.js'
])
.pipe($.uglify())
// .pipe($.uglify({
// mangle: true,//类型:Boolean 默认:true 是否修改变量名
// compress: true,//类型:Boolean 默认:true 是否完全压缩
// }))
.pipe(gulp.dest(config.dist + '/js'))
.pipe($.size({
showFiles: true, gzip: true, title: 'uglify-js'
}));
});
// optimize images and put them in the dist folder
gulp.task('uglify:mainScss', function() {
return gulp.src([
config.mainScss,
])
.pipe($.csso())
.pipe(gulp.dest(config.dist + '/css'))
.pipe($.size({
showFiles: true, gzip: true, title: 'uglify-css'
}));
});
//build files for development
gulp.task('build', ['clean'], function(cb) {
runSequence(['html'], cb);
});
//build files for creating a dist release
gulp.task('build:dist', ['clean'], function(cb) {
runSequence(['html','copy', 'images', 'uglify:js', 'copy:js', 'uglify:mainScss'], cb);
});
gulp.task('html', function() {
var assets = $.useref.assets({
searchPath: '{build/tmp,src}'
});
return gulp.src(config.index)
.pipe(assets)
.pipe($.sourcemaps.init())
// .pipe($.if('*.js', $.preprocess({context: { DEBUG: false }})))
.pipe($.if('*.js', $.uglify({
// mangle: false,
})))
.pipe($.if('*.css', $.csso()))
.pipe($.if(['**/*main.js', '**/*main.css'], $.header(config.banner, {
pkg: pkg
})))
.pipe($.rev())
.pipe(assets.restore())
.pipe($.useref())
.pipe($.revReplace())
.pipe($.if('*.html', $.minifyHtml({
empty: true
})))
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest(config.dist))
.pipe($.size({
title: 'html'
}));
});
//clean temporary directories
gulp.task('clean', del.bind(null, [config.dist, config.tmp]));
//default task
gulp.task('default', ['serve']); //
// 设置代理
var middleware = proxy('/apis/api/**', {
target: 'http://192.168.4.111',
changeOrigin: true,
logLevel: 'debug'
});
//run the server after having built generated files, and watch for changes
gulp.task('serve', ['build'], function() {
browserSync({
port: config.port,
ui: {
port: config.uiPort
},
startPath: 'index.html',
directory:true,
notify: false,
logPrefix: pkg.name,
server: [config.tmp, 'src'],
middleware: [middleware]
});
// browserSync.init({
// server: {
// baseDir: './src',
// port: config.uiPort,
// middleware: [middleware]
// },
// startPath: 'shopDecoration01.html?id=244',
// })
gulp.watch(config.index, reload);
gulp.watch(config.mainScss, reload);
gulp.watch(config.images, reload);
gulp.watch(config.js, reload);
});