generator-tbc
Version:
generator for TBC
445 lines (381 loc) • 10.6 kB
JavaScript
module.exports = function (grunt) {
var file = grunt.file;
var task = grunt.task;
/**
* 对每个具体任务进行配置
*/
grunt.initConfig({
pkg: grunt.file.readJSON('abc.json'),
// 配置默认分支
currentBranch: 'master',
/**
* 对页面进行清理
*/
clean: {
build: {
src: 'build/*'
// },
//
// mobile: {
// expand: true,
// cwd: 'build/',
// src: ['mobile*.css', '*.mobile*.css', 'mobile*.js', '*.mobile*.js'],
}
},
// /**
// * 多终端适配.
// */
// copy: {
//
// main: {
// files: [
// {
// expand: true,
// cwd: 'build/',
// src: ['mobile*.css', '*.mobile*.css', 'mobile*.js', '*.mobile*.js'],
// dest: 'build/mobile/',
// rename: function(dest, src) {
// return dest + src.replace(/^mobile/, 'index').replace(/\.mobile/, '');
// }
// }
// ]
// }
//
// },
/**
* copy file
*/
copy: {
swf: {
files: [
{
expand: true,
src: ['*.swf'],
dest: 'build/'
},
{
expand: true,
cwd: 'plugin/',
src: ['*.swf'],
dest: 'build/plugin/'
},
{
expand: true,
cwd: 'images/',
src: ['*.png', '*.swf', '*.jpg', '*.gif', '*.svg', '*.eot', '*.ttf', '*.woff'],
dest: 'build/images/'
}
]
}
},
/**
* 进行KISSY 打包
* @link https://github.com/daxingplay/grunt-kmc
*/
kmc: {
options: {
packages: [
{
name: '<%%= pkg.name %>',
path: '../'
}
],
map: [['<%%= pkg.name %>/', 'tbc/<%%= pkg.name %>/<%%= currentBranch %>/']]
},
main: {
files: [
{
expand: true,
src: [ '*.js', '!Gruntfile.js'],
dest: 'build/'
},
{
expand: true,
src: ['plugin/*.js'],
dest: 'build/'
}
]
}
},
/**
* 替换 version 变量。
*/
"regex-replace": {
// js 中的模块文件替换,主要是修复 kmc 的 bug
main: {
src: ['build/*.js', 'build/plugin/*.js'],
actions: [
{
name: 'plugin',
search: /(\.use\s*\(\s*['"]\s*)plugin\//,
replace: '$1tbc/<%%= pkg.name %>/<%%= currentBranch %>/plugin/',
flags: 'g'
},
// 修复 kmc 未替换 mod 路径的问题。。。
{
name: 'mods',
search: /['"]*requires['"]*\s*\:\n*\t*\s*\[\s*\n*\t*(['"]\s*[^'"]+\s*['"]\s*,*\s*)+\s*\n*\t*\]/gm,
replace: function(match) {
var pkgname = grunt.config.get('pkg').name,
branch = grunt.config.get('currentBranch');
return match.replace(/(\.+\/)*mods\//g, 'tbc/' + pkgname + '/' + branch + '/mods/')
// 替换 ./ ../ 类似的模块名
.replace(/\.+\/([^mu'"\/]+)/g, 'tbc/' + pkgname + '/' + branch + '/$1')
// 替换 util
.replace(/(\.+\/)*util\//g, 'tbc/' + pkgname + '/' + branch + '/util/')
// 样式替换
.replace(/(['"]\s*)(?:\.+\/)*([^'"\/]+\.css\s*['"])/g, '$1' + 'tbc/' + pkgname + '/' + branch + '/$2');
}
}
]
},
// 所有文件中版本号相关的替换
branch: {
src: ['build/*.js', 'build/*.css', 'build/plugin/*.js', 'build/plugin/*.css'],
actions: [
// 替换分支版本
{
name: 'version',
search: /\{BRANCH\}/,
replace: '<%%= currentBranch %>',
flags: 'g'
}
]
}
},
/**
* 将HTML编译为KISSY 模块
* @link https://github.com/maxbbn/grunt-kissy-template
*/
ktpl: {
main: {
files: [
{
expand: true,
dest: './',
src: 'mods/*-tpl.html',
ext: '.js'
}
]
}
}<% if(enableCSSCombo) { %>,
/**
* CSS-Combo
*/
css_combo: {
options: {
paths: './'
},
main: {
files: [
{
expand: true,
src: '*.css',
dest: 'build/',
ext: '.css'
}
]
}
}<% } %><% if(enableLess) { %>,
/**
* 将LESS编译为CSS
* @link https://github.com/gruntjs/grunt-contrib-less
*/
less: {
options: {
paths: './'
},
main: {
files: [
{
expand: true,
src: '*.less',
dest: 'build/',
ext: '.css'
}
]
}
}<% } %><% if(enableSass) { %>,
/**
* 编译Compass & SASS
* @link https://github.com/gruntjs/grunt-contrib-compass
*/
compass: {
options: {
outputStyle: 'nested',
noLineComments: true,
importPath: './',
trace: true
},
main: {
options: {
sassDir: './',
cssDir: 'build/'
}
}
}<% } %>,
/**
* 对JS文件进行压缩
* @link https://github.com/gruntjs/grunt-contrib-uglify
*/
uglify: {
options: {
banner: '/*! <%%= pkg.name %> <%%= grunt.template.today("yyyy-mm-dd") %> */\n',
beautify: {
ascii_only: true
}
},
main: {
files: [
{
expand: true,
cwd: 'build/',
src: ['*.js', '!*-min.js'],
dest: 'build/',
ext: '-min.js'
},
{
expand: true,
cwd: 'build/plugin/',
src: ['*.js', '!*-min.js'],
dest: 'build/plugin/',
ext: '-min.js'
}
]
}
},
/**
* 对CSS 文件进行压缩
* @link https://github.com/gruntjs/grunt-contrib-cssmin
*/
cssmin: {
main: {
files: [
{
expand: true,
cwd: 'build/',
src: ['*.css', '!*-min.css'],
dest: 'build/',
ext: '-min.css'
}
]
}
},
watch: {
files: [
/* js */
'*.js', 'mods/*.js', 'plugin/*.js', '!Gruntfile.js'
/* css */
,'*.css', 'mods/*.css', 'plugin/*.css'
/* html */
,'mods/*-tpl.html'
<% if(enableLess) { %>
/*less*/
,'*.less', 'mods/*.less'
<% } %>
<% if(enableSass) { %>
/*sass*/
,'*.scss', 'mods/*.scss'
<% } %>
],
tasks: 'exec:build'
},
/**
* 同步 demo。
*/
http: {
demo: {
url: 'http://tbc.fed.taobao.net/<%= pkg.name %>/git-pull.php'
}
},
/**
* 发布命令。
*/
exec: {
tag: {
command: 'git tag publish/<%%= currentBranch %>'
},
push: {
command: 'git push origin publish/<%%= currentBranch %>:publish/<%%= currentBranch %>'
},
publish: {
command: 'grunt default:publish'
},
build: {
command: 'grunt'
},
demo: {
command: 'grunt http'
},
addutil: {
command: 'git submodule add git@gitlab.alibaba-inc.com:tbc/util.git'
},
initutil: {
command: 'git submodule init'
},
updateutil: {
command: 'git submodule update --init'
},
pullutil: {
command: 'git submodule foreach git pull origin master'
}
}
});
/**
* 载入使用到的通过NPM安装的模块
*/
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-css-combo'); <% if(enableLess) { %>
grunt.loadNpmTasks('grunt-contrib-less');<% } %> <% if(enableSass) { %>
grunt.loadNpmTasks('grunt-contrib-compass');<% } %>
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-kissy-template');
grunt.loadNpmTasks('grunt-kmc');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-regex-replace');
grunt.loadNpmTasks('grunt-exec');
grunt.loadNpmTasks('grunt-http');
/**
* 发布
*/
grunt.registerTask('publish', 'tbc publish...', function() {
task.run(['exec:publish', 'exec:demo']);
});
/**
* 添加 util 模块
*/
grunt.registerTask('addutil', 'add util...', function() {
task.run(['exec:addutil', 'exec:initutil']);
});
/**
* 更新 util 模块
*/
grunt.registerTask('updateutil', 'update util...', function() {
task.run(['exec:pullutil']);
});
// 打包
return grunt.registerTask('default', 'tbc running ...', function(type) {
var done = this.async();
var exec = require('child_process').exec;
// 获取当前分支
exec('git branch', function(err, stdout, stderr, cb) {
var reg = /\*\s+[^\s]*daily\/((?:\d+\.*){3})/,
match = stdout.match(reg);
if (!match) {
return grunt.log.error('当前分支为 master 或者名字不合法(daily/x.y.z),请切换分支'.red);
}
grunt.log.write(('当前分支:' + match[1]).green);
grunt.config.set('currentBranch', match[1]);
done();
});
// 任务
if (!type) {
task.run(['clean:build', 'exec:updateutil', 'ktpl', 'kmc'<% if(enableCSSCombo) { %>, 'css_combo'<% } %> <% if(enableLess) { %> ,'less'<% } %> <% if(enableSass) { %>, 'compass'<% } %>, 'regex-replace', 'uglify', 'cssmin', 'copy'/*, 'copy', 'clean:mobile'*/]);
} else if ('publish' === type) {
task.run(['exec:tag', 'exec:push']);
}
});
};