gulpfile.js
4.13 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
var gulp=require('gulp'),
fs=require('fs'),
ftp = require('gulp-ftp'),
gutil = require('gulp-util'),
path=require('path'),
plumber = require('gulp-plumber'),
sass = require('gulp-sass'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
// md5 = require("gulp-md5"),
autoprefixer = require('gulp-autoprefixer'),
server = require('gulp-develop-server'),
minifycss = require('gulp-minify-css'),
webpack = require('webpack');
var crypto=require("crypto");
var rename = require("gulp-rename");
var clean = require('gulp-clean');
var wconfig = require('./webpack.config');
var env = process.env.NODE_ENV || 'development';
var config = JSON.parse(fs.readFileSync('./package.json').toString());
var assets_dir = 'dist/' + config.name + '/assets';
var public_dir = '../public';
var server_dir = '../server';
var dist_dir = {
js: 'dist/' + config.name + '/' + config.version,
css: 'dist/' + config.name + '/' + config.version,
image: assets_dir + '/images',
font: assets_dir + '/fonts'
};
var cdn_domain = 'http://cdn.yoho.cn/';
var md5="";//+crypto.createHash('md5').update(new Date().toString()).digest('hex');;
var ftpConfig = {
host: '218.94.75.58',
user: 'php',
pass: 'yoho9646'
};
gulp.task('default',["sass","js"]);
gulp.task('start',["default","default-watch","server"]);
gulp.task('build',["clean"],function(){
gulp.start(["buildjs","assets","static-config"]);
});//"static-config"
//预编译css
gulp.task('sass', function() {
gulp.src(['sass/index.scss','sass/login.scss'])
.pipe(plumber())
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(minifycss())
.pipe(autoprefixer({
browsers: ['last 5 versions'],
cascade: false
}))
.pipe(gulp.dest(path.join(public_dir,"dist")));
});
//预编译js
gulp.task('js',function(cb){
wconfig.output.path=path.join(public_dir,"dist");
webpack(wconfig, cb);
});
//监控css和js
gulp.task("default-watch",function(){
gulp.watch('sass/**/*.scss', ['sass']);
gulp.watch('js/**/*.js', ['js']);
});
gulp.task("clean",function(){
return gulp.src(dist_dir.js+"/", {read: false})
.pipe(clean());
});
//发布js文件
gulp.task("buildjs",["js"],function(){
gulp.src(path.join(public_dir,"dist") + '/*.js')
.pipe(uglify())
// .pipe(rename({
// suffix:md5
// }))
.pipe(gulp.dest(dist_dir.js));
});
//发布静态资源
gulp.task('assets', function() {
gulp.src(public_dir + '/img/**')
.pipe(gulp.dest(dist_dir.image));
gulp.src(public_dir + '/fonts/*')
.pipe(gulp.dest(dist_dir.font));
gulp.src(path.join(public_dir,"dist") + '/*.css')
.pipe(minifycss())
.pipe(gulp.dest(dist_dir.css));
// var str=fs.readFileSync(public_dir + '/css/all.css','utf-8');
// attr=str.replace(/(\n|\r)*/g,'').replace(/(\n)+@import url\(\'/g,function(name){
// return public_dir + '/css/';
// }).replace(/\'\)/g,'').split(';');
// gulp.src(attr)
// .pipe(concat('all.css'))
// .pipe(minifycss())
// .pipe(gulp.dest(dist_dir.css))
});
//配置静态资源文件
gulp.task("static-config",function(){
// var files = fs.readdirSync('./dist/libs');
var staticConfig={
test:{
path:'/dist'
},
preview: {
path: cdn_domain + config.name + '/' + config.version,
md5:md5
},
production: {
path: cdn_domain + config.name + '/' + config.version,
md5:md5
}
}
fs.writeFileSync('../server/staticConfig.js', "exports.staticDir = " + JSON.stringify(staticConfig));
});
//发布到CDN
gulp.task('dist', function() {
var ftpstream = ftp(ftpConfig);
return gulp.src('dist/**/')
.pipe(ftpstream)
.pipe(gutil.noop());
});
//启动服务
gulp.task('server', function() {
server.listen({
path: server_dir + '/app.js'
});
gulp.watch([
server_dir + '/app.js',
server_dir+"/{interface,adapter,config,mid,stub,util}/**/*.js",
server_dir+"/views/**/*.html"], server.restart);
});