Optimize workflow

This commit is contained in:
dengtongyu committed 2018-05-16 17:32:40 +08:00
1 parent cec28fc0f3
commit 1f18052940
14 files changed
+11765 -451

No files matched your search

+227
View File
@@ -0,0 +1,227 @@
/**
*
* @authors Tony.Deng (dengtongyu123@163.com)
* @date 2018-05-15 14:49:06
* @version $Id$
*/
const gulp = require('gulp');
const webpack = require('webpack-stream');
const stylus = require('gulp-stylus');
const swig = require('gulp-swig');
const htmlmin = require('gulp-htmlmin');
const uglify = require('gulp-uglify');
const tmodjs = require('gulp-tmod');
const browserSync = require('browser-sync');
const reload = browserSync.reload;
const proxyMiddleware = require('http-proxy-middleware');
const babel = require('gulp-babel');
const runSequence = require('run-sequence');
const plumber = require('gulp-plumber');
const notify = require('gulp-notify');
const copy = require('gulp-contrib-copy');
const util = require('./util');
const watch = require('./watch');
function onErrorHandler(err) {
notify.onError({
title: "Webpage",
subtitle: err.plugin + " error!",
message: "Error: <%= error.message %>",
sound: "Beep"
})(err);
this.emit('end');
}
init = (pathName) => {
let pathConfig = util.initPath(pathName);
let config = util.getConfig();
let swigOpts = util.siwgConfig();
//stylus任务
gulp.task('stylus_build', function() {
return gulp.src(pathConfig.styleBuildPath, config)
.pipe(plumber({
errorHandler: onErrorHandler
}))
.pipe(stylus({
compress: true
}))
.pipe(gulp.dest(config.build))
.on("end", reload);
});
//swig任务
gulp.task('swig_build', function() {
swigOpts = util.siwgConfig();
return gulp.src(pathConfig.htmlBuildPath, config)
.pipe(plumber({
errorHandler: onErrorHandler
}))
.pipe(swig(swigOpts))
.pipe(gulp.dest(config.build))
.on("end", reload);
});
//htmlmin任务
gulp.task('htmlmin_build', function() {
return gulp.src(pathConfig.htmlminBuildPath, config)
.pipe(plumber({
errorHandler: onErrorHandler
}))
.pipe(htmlmin({
collapseWhitespace: true
}))
.pipe(gulp.dest(config.build))
.on("end", reload);
});
//babel任务
gulp.task('babel_build', function() {
return gulp.src(pathConfig.scriptBuildPath)
.pipe(plumber({
errorHandler: onErrorHandler
}))
.pipe(babel({
presets: ['env']
}))
.pipe(webpack({
output: {
filename: 'index.js',
}
}))
.pipe(gulp.dest(pathConfig.babelOutPath))
.on("end", reload);
});
//uglify任务
gulp.task('uglify_build', function() {
return gulp.src(pathConfig.uglifyBuildPath, {
base: config.build
})
.pipe(plumber({
errorHandler: onErrorHandler
}))
.pipe(uglify())
.pipe(gulp.dest(config.build))
.on("end", reload);
});
//tmod任务
gulp.task('tmod_build', function() {
//console.log(pathConfig.tmodBuildPath);
return gulp.src(pathConfig.tmodBuildPath)
.pipe(plumber({
errorHandler: onErrorHandler
}))
.pipe(tmodjs({
templateBase: pathConfig.tmodBasePath,
output: pathConfig.tmodOutPath,
combo: false,
type: 'cmd'
}))
.on("end", reload);
});
//watch html任务
gulp.task('html_build', function() {
runSequence('swig_build', 'htmlmin_build');
});
//watch js任务
gulp.task('js_build', function() {
//console.log('执行了js_build')
runSequence('babel_build', 'uglify_build');
});
//watch tmodjs任务
gulp.task('tmodjs_build', function() {
runSequence('tmod_build', 'babel_build', 'uglify_build');
});
//server任务
gulp.task('server', function() {
//代理配置
let proxy = proxyMiddleware(
['/node_modules', '/res/images/userimage'], {
//target:'http://172.26.68.35:8085'//
target: 'http://node.dengtongyu.com'
//target:'http://47.94.23.46:80'
}
);
// let proxy2 = proxyMiddleware(
// ['/data/getPictureList'], {
// target: 'http://localhost:8080'
// }
// );
browserSync({
host: 'localhost',
open: 'external',
startPath: '/pages/' + pathConfig.currPath,
browser: 'google chrome',
ghostMode: false, // 每个页面的镜像独立,不相互影响
server: {
baseDir: 'build',
middleware: [proxy]
}
});
});
gulp.task('build', function(callback) {
runSequence('stylus_build', 'swig_build', 'htmlmin_build', 'tmod_build', 'babel_build', 'uglify_build');
});
gulp.task('dev', function(callback) {
runSequence('stylus_build', 'swig_build', 'htmlmin_build', 'tmod_build', 'babel_build', 'server');
});
}
copyTpl = (pathName) => {
let pathConfig = util.initPath(pathName);
let config = util.getConfig();
//复制模板
gulp.task('copy-pc', function() {
return gulp.src(config.base + '/template/tpl-pc/**/*.*')
.pipe(copy())
.pipe(gulp.dest(config.base + '/pages/' + pathConfig.currPath));
});
gulp.task('copy-phone', function() {
return gulp.src(config.base + '/template/tpl-phone/**/*.*')
.pipe(copy())
.pipe(gulp.dest(config.base + '/pages/' + pathConfig.currPath));
});
}
exports.selectTpl = (pathName,type) => {
copyTpl(pathName);
if (type == 1) {
runSequence('copy-pc', function() {
init(pathName);
runSequence('dev');
});
}
if (type == 2) {
runSequence('copy-phone', function() {
init(pathName);
runSequence('dev');
});
}
}
exports.dev = (pathName) => {
init(pathName);
runSequence('dev');
watch.init(util.getPathConfig(),gulp);
}
exports.pack = (pathName) => {
init(pathName);
runSequence('build');
}
+102
View File
@@ -0,0 +1,102 @@
/**
*
* @authors Tony.Deng (dengtongyu123@163.com)
* @date 2018-05-15 10:55:16
* @version $Id$
*/
const path = require('path');
let _pathConfig = null;
let _config = {
base: 'src',
build: 'build'
};
exports.initPath = (pathName) => {
_pathConfig = {};
_pathConfig.currPath = pathName;
//stylus build目录
_pathConfig.styleBuildPath = _config.base + '/pages/' + pathName + '/**/!(_)*.styl';
//swig build目录
_pathConfig.htmlBuildPath = _config.base + '/pages/' + pathName + '/**/*.htm';
//htmlmin build目录
_pathConfig.htmlminBuildPath = _config.build + '/pages/' + pathName + '/**/*.html';
//wabpck build 目录
_pathConfig.scriptBuildPath = _config.base + '/pages/' + pathName + '/index.js';
_pathConfig.babelOutPath = _config.build + '/pages/' + pathName;
//uglify build目录
_pathConfig.uglifyBuildPath = _config.build + '/pages/' + pathName + '/**/*.js';
//tmod build目录
_pathConfig.tmodBuildPath = _config.base + '/pages/' + pathName + '/**/*.tmod';
_pathConfig.tmodBasePath = _config.base + '/pages/';
_pathConfig.tmodOutPath = _config.base + '/script/tmodjs/';
return _pathConfig;
}
exports.getPathConfig = () => {
return _pathConfig;
}
exports.getConfig = () => {
return _config;
}
//随机路径名
exports.randomActName = () => {
let d = new Date();
let m = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1).toString();
let dd = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
let str = d.getFullYear() + m + dd + String.fromCharCode(Math.floor(Math.random() * 26) + "a".charCodeAt(0)) + Math.floor(Math.random() * 10);
return str;
}
exports.siwgConfig = () => {
let url = '../' + _config.base + '/pages/' + _pathConfig.currPath + '/data.json';
delete require.cache[require.resolve(url)]
let swigData = require(url);
swigData.now = function () {
return new Date().getTime()
};
return {
defaults: {
varControls: ['{[', ']}'],
cache: false,
locals:swigData
}
};
}
exports.pathType = (filePath) => {
let type = path.parse(filePath).ext;
if (type == '.styl') {
return 0;
} else if (type == '.htm') {
return 1;
} else if (type == '.html') {
return 2;
} else if (type == '.js') {
if (filePath.match(/build/ig)) {
return 4;
} else {
return 3;
}
} else if (type == '.tmod') {
return 5;
}
}
+53
View File
@@ -0,0 +1,53 @@
/**
*
* @authors Tony.Deng (dengtongyu123@163.com)
* @date 2018-05-15 15:45:22
* @version $Id$
*/
const chalk = require('chalk');
//const util = require('./util');
exports.init = (pathConfig,gulp) => {
//let pathConfig = util.getPathConfig();
let watchList = [{
watchPath: pathConfig.styleBuildPath,
taskName: ['stylus_build'],
taskPath: 'styleChangePath'
}, {
watchPath: pathConfig.htmlBuildPath,
taskName: ['html_build'],
taskPath: 'swigChangePath'
}, {
watchPath: pathConfig.htmlminBuildPath,
taskName: ['htmlmin_build'],
taskPath: 'htmlminChangePath'
}, {
watchPath: pathConfig.scriptBuildPath,
taskName: ['js_build'],
taskPath: 'scriptChangePath'
}, {
watchPath: pathConfig.uglifyBuildPath,
taskName: ['uglify_build'],
taskPath: 'uglifyChangePath'
}, {
watchPath: pathConfig.tmodBuildPath,
taskName: ['tmodjs_build'],
taskPath: 'tmodChangePath'
}];
for (var i = 0; i < watchList.length; i++) {
if (watchList[i].taskName[0] == 'uglify_build' || watchList[i].taskName[0] == 'htmlmin_build') continue;
gulp.watch(watchList[i].watchPath, watchList[i].taskName).on('change', function(event) {
//let fileType = util.pathType(event.path);
//pathConfig[watchList[fileType].taskPath] = path.relative(__dirname, event.path);
//added为新增,deleted为删除,changed为改变
//变化类型 和 变化的文件的路径
console.log(chalk.green(event.type + ' : ' + event.path));
});
}
}