-
Notifications
You must be signed in to change notification settings - Fork 6
/
gulpfile.js
119 lines (115 loc) · 3.01 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
const path = require('path');
const gulp = require('gulp');
const sass = require('gulp-sass');
const babel = require('gulp-babel');
const tap = require('gulp-tap');
const webpack = require('gulp-webpack');
const es2015 = require('babel-preset-es2015');
const notify = require('gulp-notify');
const plumber = require('gulp-plumber');
const concat = require('gulp-concat');
const autoprefixer = require('gulp-autoprefixer');
const sourcemaps = require('gulp-sourcemaps');
const browserSync = require('browser-sync')
.create();
const fs = require('fs');
const proxyMiddleware = require('http-proxy-middleware');
const CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");
const uglify = require('gulp-uglify');
//const cssnano = require('gulp-cssnano');
//配置请求的地址
const proxyTable = {
'/fiveticket': {
target: 'http://100jc.net/fiveticket',
changeOrigin: true,
logLevel: 'debug'
}
}
const proxyArr = [];
Object.keys(proxyTable).forEach(function(context){
var options = proxyTable[context];
if(typeof options === 'string'){
options = {
target: options
}
}
proxyArr.push(proxyMiddleware(context, options));
});
gulp.task('browser-sync', ['sass', 'script','copy-resources','copy-html'], function(){
browserSync.init({
server: {
middleware: proxyArr
},
startPath: '/dist/?debug=true', // 打开的位置
});
//监听文件的变化
gulp.watch("css/**/*.scss", ['sass']);
gulp.watch("js/**/*.js", ['script']);
// gulp.watch("js/**/*.js", ['jsmin']);
gulp.watch("resources/**/*", ['copy-resources']);
gulp.watch("index.html", ['copy-html']);
});
gulp.task('copy-resources',function(){
return gulp.src('resources/**/*')
// .pipe(assetRev())
.pipe(gulp.dest('dist/resources/'))
.pipe(browserSync.stream());;
})
gulp.task('copy-html',function(){
return gulp.src('index.html')
// .pipe(assetRev())
.pipe(gulp.dest('dist'))
.pipe(browserSync.stream());;
})
//编译sass
gulp.task('sass', function(){
return gulp.src('css/main.scss')
.pipe(plumber({
errorHandler: notify.onError('Error: <%= error.message %>')
}))
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(autoprefixer())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('dist/css'))
.pipe(browserSync.stream());
});
//监听js
gulp.task('script', function(){
return gulp.src('./js/**/*.js')
.pipe(plumber({
errorHandler: notify.onError('Error: <%= error.message %>')
}))
.pipe(sourcemaps.init())
.pipe(webpack({
devtool: "source-map",
entry: {
app: './js/index.js'
},
output: {
filename: 'main.js'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader'
}
]
},
plugins: []
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('./dist/js/'))
});
// 压缩js
gulp.task('jsmin', ['script'], function() {
return gulp.src('./dist/js/*.js')
.pipe(plumber({
errorHandler: notify.onError('Error: <%= error.message %>')
}))
.pipe(uglify())
.pipe(gulp.dest('./dist/js/'))
.pipe(browserSync.stream());
});
gulp.task('default', ['browser-sync']);