-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
151 lines (138 loc) · 4.23 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
"use strict"
const fs = require('fs');
const gutil = require('gulp-util');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config.js');
const gulp = require('gulp');
const postcss = require('postcss');
const gulp_postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer'); // 浏览器前缀
const mqpacker = require('css-mqpacker'); // MQ 包装器
const cssimport = require('postcss-import'); // css import
const csswring = require('csswring'); // css minify
const nested = require('postcss-nested'); // 支持css嵌套
const rename = require('gulp-rename'); // 文件重命名
const webpackDevServer = require('webpack-dev-server');
const clean = require('gulp-clean');
const GulpSSH = require('gulp-ssh');
const plumber = require('gulp-plumber');
const sourcemaps = require('gulp-sourcemaps');
const gulpif = require('gulp-if');
const os = require('os');
const env = process.env.NODE_ENV || 'development';
const dev = (env == 'development');
const hostConfig = {
host: '118.89.47.166',
port: 22,
username: 'root',
privateKey: fs.readFileSync( os.homedir() + '/.ssh/id_rsa')
}
var gulpSSH = new GulpSSH({
ignoreErrors: false,
sshConfig: hostConfig
})
/**
* ssh publish
*/
gulp.task('publish', () => {
return gulp.src(['dest/**/*'])
.pipe( gulpSSH.dest('/data/tsj/blog') )
});
gulp.task('clean-server', () => {
return gulpSSH.exec(['cd /data/tsj/blog/ && rm -rf *']);
});
gulp.task('publish-datas', () => {
return gulp.src(['datas/**/*'])
.pipe( gulpSSH.dest('/data/tsj/blog/services/datas') )
});
gulp.task('publish-posts', () => {
return gulp.src(['posts/**/*'])
.pipe( gulpSSH.dest('/data/tsj/blog/services/posts') )
});
gulp.task('publish-uploads', () => {
return gulp.src(['uploads/**/*'])
.pipe( gulpSSH.dest('/data/tsj/blog/services/uploads') )
});
/**
* clean
*/
gulp.task('clean', () => {
return gulp.src('dest', {read: false})
.pipe(clean());
});
/**
* postcss
* 这里的编译是单独给开发样式的时候用的
*/
gulp.task('postcss', () => {
var processors = [
cssimport,
nested,
mqpacker,
autoprefixer,
csswring({
removeAllComments: true
})
];
return gulp.src('./src/css/main.css')
.pipe(plumber())
.pipe(gulpif(dev, sourcemaps.init()))
.pipe(gulp_postcss(processors))
.pipe(plumber.stop())
.pipe(rename({suffix: ".min"}))
.pipe(gulpif(dev, sourcemaps.write('.')))
.pipe(gulp.dest('./src/css'))
});
/**
* webpack pro build
*/
gulp.task('webpack:build', callback => {
var config = Object.create(webpackConfig);
// run webpack
webpack(config, function(err, stats) {
if(err) throw new gutil.PluginError("webpack:build", err);
gutil.log("[webpack:build]", stats.toString({
colors: true
}));
callback();
});
});
/**
* webpack dev server
*/
gulp.task("webpack-dev-server", callback => {
var config = Object.create(webpackConfig);
config.devtool = "source-map";
config.entry.app.unshift("webpack-dev-server/client?http://localhost:8080", "webpack/hot/dev-server");
var compiler = webpack(config);
new webpackDevServer(compiler, {
historyApiFallback: true, // 为React-Router 开启 browserHistory
hot: true,
compress: true, // enable gzip
//publicPath: config.output.publicPath,
stats: {
colors: true,
chunkModules: false // nodejs API https://webpack.github.io/docs/node.js-api.html
}
}).listen(8080, "localhost", function(err) {
if(err) throw new gutil.PluginError("webpack-dev-server", err);
gutil.log("[webpack-dev-server]", "http://localhost:8080/webpack-dev-server/");
});
});
/**
* watch task
*/
gulp.task('watch', () => {
gulp.watch([
'./src/css/**/*.css',
'!./src/css/main.min.css'
], ['postcss']);
});
/**
* quick
*/
gulp.task('dev', ['webpack-dev-server', 'watch']);
gulp.task('build', ['clean', 'webpack:build']);
gulp.task('default', () => {
console.log('缺少指令!please use: [gulp dev] OR [gulp build]');
});