...
|
...
|
@@ -2,10 +2,10 @@ |
|
|
* GULP-FILE
|
|
|
* author: xuqi(qi.xu@yoho.cn)
|
|
|
* date: 2015/9/30
|
|
|
* 根据 PC 版修改为 H5 版, 毕凯
|
|
|
*/
|
|
|
|
|
|
var gulp = require('gulp'),
|
|
|
compass = require('gulp-compass'),
|
|
|
cp = require('child_process');
|
|
|
|
|
|
var fs = require('fs'),
|
...
|
...
|
@@ -16,7 +16,11 @@ var fs = require('fs'), |
|
|
transport = require('gulp-spm'),
|
|
|
concat = require('gulp-concat');
|
|
|
|
|
|
var config = JSON.parse(fs.readFileSync('./package.json').toString());
|
|
|
var postcss = require('gulp-postcss');
|
|
|
var sourcemaps = require('gulp-sourcemaps');
|
|
|
var cssnano = require('gulp-cssnano');
|
|
|
|
|
|
var config = require('./package');
|
|
|
|
|
|
var rootDist = 'dist/myohobuy/',
|
|
|
ftpConfig = {
|
...
|
...
|
@@ -32,31 +36,112 @@ var distDir = { |
|
|
font: rootDist + 'assets/font'
|
|
|
};
|
|
|
|
|
|
gulp.task('default', ['compass', 'compass-watch', 'spm-doc']);
|
|
|
|
|
|
// compass
|
|
|
gulp.task('compass', function() {
|
|
|
gulp.src('sass/**/*.scss')
|
|
|
.pipe(
|
|
|
compass({
|
|
|
config_file: 'config.rb',
|
|
|
css: 'css',
|
|
|
sass: 'sass'
|
|
|
})
|
|
|
).on('error', function(error) {
|
|
|
console.log(error);
|
|
|
this.emit('end');
|
|
|
gulp.task('default', ['postcss-dev', 'postcss-watch', 'spm-doc']);
|
|
|
|
|
|
|
|
|
//根据环境变量生成postcss插件配置
|
|
|
function postcssEnvPlugin(env) {
|
|
|
var sprites = {
|
|
|
spritesmith: {
|
|
|
padding: 4
|
|
|
},
|
|
|
groupBy: function(file) {
|
|
|
|
|
|
// 根据目录分组,防止合并后的图片太大
|
|
|
var group = file.url.split('/')[1];
|
|
|
|
|
|
file.retina = true; // H5 强制所有图片都是用二倍图
|
|
|
return group ? Promise.resolve(group) : Promise.reject();
|
|
|
},
|
|
|
filterBy: function(file) {
|
|
|
|
|
|
//使用resolve转化后静态资源生成../img或者../assets/img/的路径
|
|
|
if (/\/img/.test(file.url) || /data:image/.test(file.url)) {
|
|
|
return Promise.reject();
|
|
|
}
|
|
|
return Promise.resolve();
|
|
|
}
|
|
|
},
|
|
|
assets;
|
|
|
|
|
|
if (env === 'DEV') {
|
|
|
assets = {
|
|
|
loadPaths: ['font/', 'img/'],
|
|
|
relativeTo: 'css/'
|
|
|
};
|
|
|
|
|
|
Object.assign(sprites, {
|
|
|
basePath: './img',
|
|
|
stylesheetPath: './css',
|
|
|
spritePath: './img'
|
|
|
});
|
|
|
} else if (env === 'PRO') {
|
|
|
assets = {
|
|
|
loadPaths: [distDir.img, distDir.font],
|
|
|
relativeTo: distDir.css,
|
|
|
cachebuster: function(filePath, urlPathname) {
|
|
|
|
|
|
//只给字体加no-cache
|
|
|
if (/font\//.test(urlPathname)) {
|
|
|
return fs.statSync(filePath).mtime.getTime().toString(16);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
Object.assign(sprites, {
|
|
|
basePath: distDir.img,
|
|
|
stylesheetPath: distDir.css,
|
|
|
spritePath: distDir.img
|
|
|
});
|
|
|
}
|
|
|
|
|
|
return [
|
|
|
require('precss'),
|
|
|
require('postcss-assets')(assets),
|
|
|
require('postcss-sprites').default(sprites),
|
|
|
require('postcss-calc'),
|
|
|
require('postcss-pxtorem')({
|
|
|
rootValue: 40,
|
|
|
unitPrecision: 5, // 保留5位小数字
|
|
|
minPixelValue: 2, // 小于 2px 时,不转换, 单位为 PX 大写的时候不转换
|
|
|
selectorBlackList: [], // 选择器黑名单,可以使用正则
|
|
|
propWhiteList: [] // 属性名称为空,表示替换所有属性的值
|
|
|
}),
|
|
|
require('autoprefixer')({
|
|
|
browsers: ['> 1%']
|
|
|
}),
|
|
|
|
|
|
//可选
|
|
|
require('postcss-use')({
|
|
|
modules: ['postcss-clearfix', 'postcss-crip', 'postcss-short', 'postcss-center', 'postcss-position']
|
|
|
})
|
|
|
];
|
|
|
}
|
|
|
|
|
|
//Postcss开发环境
|
|
|
gulp.task('postcss-dev', function() {
|
|
|
return gulp.src('sass/index.css')
|
|
|
.pipe(sourcemaps.init())
|
|
|
.pipe(postcss(postcssEnvPlugin('DEV')))
|
|
|
.pipe(sourcemaps.write('.'))
|
|
|
.pipe(gulp.dest('css/'))
|
|
|
});
|
|
|
|
|
|
gulp.task('postcss-watch', function() {
|
|
|
gulp.watch('sass/**/*.css', ['postcss-dev']);
|
|
|
});
|
|
|
|
|
|
// compass watch
|
|
|
gulp.task('compass-watch', function() {
|
|
|
gulp.watch('sass/**/*.scss', ['compass']);
|
|
|
//Postcss正式环境生成
|
|
|
gulp.task('postcss-pro', ['assets'], function() {
|
|
|
return gulp.src('sass/index.css')
|
|
|
.pipe(postcss(postcssEnvPlugin('PRO')))
|
|
|
.pipe(cssnano())
|
|
|
.pipe(gulp.dest(distDir.css))
|
|
|
});
|
|
|
|
|
|
// start spm server
|
|
|
gulp.task('spm-doc', function() {
|
|
|
var sd = cp.exec('spm doc');
|
|
|
var sd = cp.exec('spm doc watch --port 8000'); // PC 用8001,H5 用8000, 跑两个服务器,不冲突
|
|
|
|
|
|
// sd.stdout.on('data', function(data) {
|
|
|
// console.log(data);
|
...
|
...
|
@@ -72,10 +157,10 @@ gulp.task('spm-doc', function() { |
|
|
});
|
|
|
|
|
|
//生成发布目录,可用于上传测试机
|
|
|
gulp.task('ge', ['assets', 'compass-production', 'build']);
|
|
|
gulp.task('ge', ['assets', 'postcss-pro', 'build']);
|
|
|
|
|
|
//发布
|
|
|
gulp.task('dist', ['assets', 'compass-production', 'build'], function() {
|
|
|
gulp.task('dist', ['assets', 'postcss-pro', 'build'], function() {
|
|
|
var ftpstream = ftp(ftpConfig);
|
|
|
|
|
|
return gulp.src('dist/**/')
|
...
|
...
|
@@ -84,30 +169,16 @@ gulp.task('dist', ['assets', 'compass-production', 'build'], function() { |
|
|
});
|
|
|
|
|
|
//font+img->dist/assets
|
|
|
gulp.task('assets', function() {
|
|
|
gulp.src('img/**')
|
|
|
gulp.task('assets', ['img', 'font']);
|
|
|
|
|
|
gulp.task('img', function() {
|
|
|
return gulp.src('img/**/*')
|
|
|
.pipe(gulp.dest(distDir.img));
|
|
|
gulp.src('font/*')
|
|
|
.pipe(gulp.dest(distDir.font));
|
|
|
});
|
|
|
|
|
|
//compass
|
|
|
gulp.task('compass-production', ['assets'], function() {
|
|
|
gulp.src('sass/index.scss')
|
|
|
.pipe(
|
|
|
compass({
|
|
|
css: distDir.css,
|
|
|
sass: 'sass',
|
|
|
image: distDir.img,
|
|
|
font: distDir.font,
|
|
|
http_path: '/',
|
|
|
style: 'compressed'
|
|
|
})
|
|
|
)
|
|
|
.on('error', function(error) {
|
|
|
console.log(error);
|
|
|
this.emit('end');
|
|
|
});
|
|
|
gulp.task('font', function() {
|
|
|
return gulp.src('font/*')
|
|
|
.pipe(gulp.dest(distDir.font))
|
|
|
});
|
|
|
|
|
|
//spm build
|
...
|
...
|
@@ -166,7 +237,7 @@ gulp.task('concat-min-libs', ['pre-libs'], function() { |
|
|
|
|
|
fs.writeFileSync(path, jsStr.substr(jsStr.indexOf('});') + 4));
|
|
|
|
|
|
return gulp.src(['js/sea.js', distDir.js + '/libs.js'])
|
|
|
return gulp.src(['plugin/sea.js', distDir.js + '/libs.js'])
|
|
|
.pipe(concat('lib.js'))
|
|
|
.pipe(uglify())
|
|
|
.pipe(gulp.dest(distDir.js));
|
...
|
...
|
@@ -190,4 +261,4 @@ gulp.task('business', ['libs'], function() { |
|
|
.pipe(uglify())
|
|
|
.pipe(concat('index.js'))
|
|
|
.pipe(gulp.dest(distDir.js));
|
|
|
}); |
|
|
\ No newline at end of file |
|
|
}); |
...
|
...
|
|