Skip to content

Commit

Permalink
update:雪碧图功能完善😁
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Jul 13, 2016
1 parent 2135e64 commit 8e6405b
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 113 deletions.
6 changes: 3 additions & 3 deletions lib/build/athena_mate/athena_replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@ function transmitResource(filename, value, resourcePrefix, config, includeJson,
}
}
}
if (!(config.pack || config.compress)) {
value = resourcePrefix + moduleName + '/' + value;
}
var mapJson = mapJSONCache[moduleName];
if (mapJson && needMd5 && !config.pack) {
value = Util.getHashName(value, mapJson);
}
if (!(config.pack || config.compress)) {
value = resourcePrefix + moduleName + '/' + value;
}
}
value = vStart + value + vEnd;
return value;
Expand Down
6 changes: 6 additions & 0 deletions lib/build/athena_mate/athena_replace_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ function trackResource(filename, value, config) {
var splitAfter = valueParse.split;
var newValue = valueParse.pathname;
value = newValue;
if (path.dirname(value).length <= 1) {
var extname = path.extname(value);
if (extname.indexOf('.') >= 0 && extname.length > 1) {
value = path.join('static', extname.slice(1), value);
}
}
var resourcePath = path.join(distPath, value);
var gResourcePath = path.join(gDistPath, value);
var moduleName = '';
Expand Down
22 changes: 11 additions & 11 deletions lib/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ function buildSingleModule (app, mod, conf, args) {
var csslintPromise = taskList.csslint($, conf.appConf, conf.moduleConf, args);
var jshintPromise = taskList.jshint($, conf.appConf, conf.moduleConf, args);
var athenaMatePromise = taskList.athena_mate($, conf.appConf, conf.moduleConf, args);
var transImagesPromise = taskList.trans_images($, conf.appConf, conf.moduleConf, args);
var stylesPromise = taskList.styles($, conf.appConf, conf.moduleConf, args);
var scriptsPromise = taskList.scripts($, conf.appConf, conf.moduleConf, args);
var compressPromise = taskList.compress($, conf.appConf, conf.moduleConf, args);
Expand All @@ -120,9 +121,10 @@ function buildSingleModule (app, mod, conf, args) {
.then(csslintPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(jshintPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(athenaMatePromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(imagesPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(transImagesPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(stylesPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(scriptsPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(imagesPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(compressPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(fontsPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(function () {
Expand Down Expand Up @@ -169,7 +171,7 @@ function buildSingleModuleSimple (app, mod, conf, args) {
var athenaMatePromise = taskList.athena_mate($, conf.appConf, conf.moduleConf, args);
var sassPromise = taskList.sass($, conf.appConf, conf.moduleConf, args);
var lessPromise = taskList.less($, conf.appConf, conf.moduleConf, args);
var serveTransImagesPromise = taskList.serve_trans_images($, conf.appConf, conf.moduleConf, args);
var transImagesPromise = taskList.trans_images($, conf.appConf, conf.moduleConf, args);
var stylesPromise = taskList.styles($, conf.appConf, conf.moduleConf, args);
var serveTransPromise = taskList.serve_trans($, conf.appConf, conf.moduleConf, args);
var tempPromise = taskList.temp($, conf.appConf, conf.moduleConf, args);
Expand All @@ -179,7 +181,7 @@ function buildSingleModuleSimple (app, mod, conf, args) {
.then(sassPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(lessPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(athenaMatePromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(serveTransImagesPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(transImagesPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(stylesPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(serveTransPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(tempPromise.bind(null, mod, conf.modulePath, conf.appPath))
Expand Down Expand Up @@ -213,8 +215,9 @@ function buildSingleModuleServer (app, mod, conf, args) {
var tplGetPromise = taskList.tpl_get($, conf.appConf, conf.moduleConf, args);
var transStaticPromise = taskList.trans_static($, conf.appConf, conf.moduleConf, args);
var athenaMateServerPromise = taskList.athena_mate_server($, conf.appConf, conf.moduleConf, args);
var imagesPromise = taskList.images($, conf.appConf, conf.moduleConf, args);
var transImagesPromise = taskList.trans_images($, conf.appConf, conf.moduleConf, args);
var stylesServerPromise = taskList.styles_server($, conf.appConf, conf.moduleConf, args);
var imagesPromise = taskList.images($, conf.appConf, conf.moduleConf, args);
var compressServerPromise = taskList.compress_server($, conf.appConf, conf.moduleConf, args);
var injectServerPromise = taskList.inject_server($, conf.appConf, conf.moduleConf, args);
var revNoHashServerPromise = taskList.rev_no_hash_server($, conf.appConf, conf.moduleConf, args);
Expand All @@ -231,8 +234,9 @@ function buildSingleModuleServer (app, mod, conf, args) {
.then(tplGetPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(transStaticPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(athenaMateServerPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(imagesPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(transImagesPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(stylesServerPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(imagesPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(compressServerPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(injectServerPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(function () {
Expand Down Expand Up @@ -286,7 +290,7 @@ function buildSingleModuleSimpleServer (app, mod, conf, args) {
var lessPromise = taskList.less($, conf.appConf, conf.moduleConf, args);
var transStaticPromise = taskList.trans_static($, conf.appConf, conf.moduleConf, args);
var athenaMateServerPromise = taskList.athena_mate_server($, conf.appConf, conf.moduleConf, args);
var serveTransImagesPromise = taskList.serve_trans_images($, conf.appConf, conf.moduleConf, args);
var transImagesPromise = taskList.trans_images($, conf.appConf, conf.moduleConf, args);
var stylesServerPromise = taskList.styles_server($, conf.appConf, conf.moduleConf, args);
var injectServerPromise = taskList.inject_server($, conf.appConf, conf.moduleConf, args);
var transOutputServerPromise = taskList.trans_output_server($, conf.appConf, conf.moduleConf, args);
Expand All @@ -297,7 +301,7 @@ function buildSingleModuleSimpleServer (app, mod, conf, args) {
.then(lessPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(transStaticPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(athenaMateServerPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(serveTransImagesPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(transImagesPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(stylesServerPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(injectServerPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(transOutputServerPromise.bind(null, mod, conf.modulePath, conf.appPath))
Expand Down Expand Up @@ -1723,11 +1727,9 @@ function publish (app, mod, args) {
return promsies.reduce(function (prev, curr) {
return prev.then(function (val) {
conf = getConf(app, moduleList[curr]);
var uploadPromise = taskList.upload($, appConf, conf.moduleConf, args);
var publishPromise = publishFunction($, appConf, conf.moduleConf, args);
var modulePath = path.join(conf.appPath, moduleList[curr]);
return buildModuleFunction(app, moduleList[curr], conf, args)
.then(uploadPromise.bind(null, moduleList[curr], modulePath, conf.appPath))
.then(publishPromise.bind(null, moduleList[curr], modulePath, conf.appPath))
.then(function (files) {
publishFiles = publishFiles.concat(files);
Expand Down Expand Up @@ -1761,10 +1763,8 @@ function publish (app, mod, args) {
if (mod === appConf.common) {
gutil.log(gutil.colors.red('检测到您正在发布公共模块,请先确认公共模块代码是否最新再继续发布!'));
}
var uploadPromise = taskList.upload($, appConf, conf.moduleConf, args);
var publishPromise = publishFunction($, appConf, conf.moduleConf, args);
return buildModuleFunction(app, mod, conf, args)
.then(uploadPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(publishPromise.bind(null, mod, conf.modulePath, conf.appPath))
.then(function (files) {
return Promise.resolve({
Expand Down
26 changes: 4 additions & 22 deletions lib/build/tasks/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module.exports = function ($, appConf, moduleConf, args) {
var vfs = require('vinyl-fs');
var Util = require( '../../util' );
var athenaMate = require('../athena_mate');
var through2 = require('through2');

var noImage = (args && args.noImage) ? args.noImage : false;
var imageminSupport = moduleConf.support.imagemin;
Expand All @@ -30,7 +29,7 @@ module.exports = function ($, appConf, moduleConf, args) {
}
$.util.log($.util.colors.green('开始' + mod + '模块任务images!'));

vfs.src(path.join(modulePath, 'dist', '_', '**', 'images', '**'))
vfs.src(path.join(modulePath, 'dist', '_static', 'images', '**'))
.pipe(athenaMate.buildFilter({
app: appConf.app,
module: moduleConf.module,
Expand Down Expand Up @@ -60,28 +59,11 @@ module.exports = function ($, appConf, moduleConf, args) {
Util.generateStatistics(modulePath, 'optimize.img', savedInfo);
}
})))
.pipe(through2.obj(function(file, enc, cb){
if (!file.isDirectory()) {
var fp = file.path;
var fb = file.base;
var fbaseAfter = fp.replace(fb, '');
var fbaseDist = fb.replace(path.join(modulePath, 'dist'), '');
if (fbaseDist.indexOf('images') <= 0 && fbaseAfter.indexOf('images') >= 0) {
fbaseAfter = fbaseAfter.substr(0, fbaseAfter.indexOf('images') + 7);
file.base = path.join(file.base, fbaseAfter);
}
this.push(file);
}
cb();
}))
.pipe(vfs.dest(path.join(modulePath, 'dist', '_static', 'images')))
.pipe(vfs.dest(imagesCacheFolder))
.on('finish', function () {
vfs.src(path.join(modulePath, 'dist', '_static', 'images', '**'))
.pipe(vfs.dest(imagesCacheFolder))
.on('finish', function () {
$.util.log($.util.colors.green('结束' + mod + '模块任务images!'));
resolve();
});
$.util.log($.util.colors.green('结束' + mod + '模块任务images!'));
resolve();
})
.on('error', function (err) {
$.util.log($.util.colors.red(mod + '模块任务images失败!'));
Expand Down
4 changes: 0 additions & 4 deletions lib/build/tasks/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
module.exports = function ($, appConf, moduleConf, args) {
return function (mod, modulePath, appPath) {
return new Promise(function (resolve, reject) {
var path = require('path');
var vfs = require('vinyl-fs');

var athenaMate = require('../athena_mate');
var Util = require('../../util');

$.util.log($.util.colors.green('开始' + mod + '模块任务scripts!'));
vfs.src([modulePath + '/dist/_static/js/*.js', '!' + modulePath + '/dist/_static/js/*.min.js'])
.pipe(vfs.dest(modulePath + '/dist/_static/js'))
Expand Down
63 changes: 61 additions & 2 deletions lib/build/tasks/serve_css.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ module.exports = function ($, appConf, moduleConf, args) {
var fs = require('fs');
var vfs = require('vinyl-fs');
var path = require('path');
var athenaMate = require('../athena_mate');
var Util = require( '../../util' );
var through2 = require('through2');
var autoprefixer = require('autoprefixer');
var pxtorem = require('postcss-pxtorem');
var sprites = require('athena-spritesmith');
var es = require('event-stream');
var del = require('del');

var athenaMate = require('../athena_mate');
var Util = require( '../../util' );

//是否开启px转rem
var px2rem = moduleConf.support.px2rem;
//是否开启雪碧图合并
var csssprite = moduleConf.support.csssprite;
var platform = appConf.platform ? appConf.platform : 'mobile';

var autoprefixerConf = moduleConf.support.autoprefixer;
Expand All @@ -44,6 +50,21 @@ module.exports = function ($, appConf, moduleConf, args) {
}));
}

if(csssprite && csssprite.enable !== false){
var opts = {
outputDimensions: true,
stylesheetPath: path.join(modulePath, 'dist', '_static', 'css'),
imageFolder: 'images',
spritePath: path.join(modulePath, 'dist', '_static', 'images', csssprite.spriteFolder ? csssprite.spriteFolder : '', 'sprite.png'),
retina: csssprite.retina || false,
rootValue: csssprite.rootValue,
padding: csssprite.padding,
groupBy: function (image) {
return image.urlSpe;
}
};
}

var stream = null;

if (/\.scss|\.sass/.test(args.cssFile)) {
Expand Down Expand Up @@ -184,6 +205,7 @@ module.exports = function ($, appConf, moduleConf, args) {

function processCss (moduleName) {
return new Promise(function (res, rej) {
var fileContents = [];
athenaMate.concat({
cwd: appPath,
pageFiles: args.pageFiles,
Expand All @@ -194,6 +216,43 @@ module.exports = function ($, appConf, moduleConf, args) {
vfs.src(path.join(appPath, moduleName, 'dist', '_static', 'css', '**', '*.css'))
.pipe(athenaMate.plumber())
.pipe($.postcss(processors))
.pipe(vfs.dest(path.join(modulePath, 'dist', '_static', 'css')))
.pipe(through2.obj(function (file, enc, cb) {
if (file.isNull() || file.isStream()) {
return cb(null, file);
}
fileContents.push('/*filename=' + path.basename(file.path) + '*/\n' + file.contents.toString());
cb();
}, function (cb) {
var file = new $.util.File({
path: path.join(modulePath, 'dist', '_static', 'css', 'sprite.css'),
base: path.join(modulePath, 'dist', '_static', 'css'),
contents: new Buffer(fileContents.join('\n/*sprite_file_split*/\n'))
});
this.push(file);
cb();
}))
.pipe($.postcss([sprites(opts)]))
.pipe(through2.obj(function (file, enc, cb) {
if (file.isNull() || file.isStream()) {
return cb(null, file);
}
var content = file.contents.toString();
fileContents = content.split('/*sprite_file_split*/');
fileContents.forEach(function (item) {
var reg = /filename=(.*)\*/;
var filename = item.match(reg)[1];
var filedir = path.join(modulePath, 'dist', '_static', 'css');
var file = new $.util.File({
path: path.join(filedir, filename),
base: filedir,
contents: new Buffer(item)
});
this.push(file);
}.bind(this));
cb();
}))
.pipe(vfs.dest(path.join(modulePath, 'dist', '_static', 'css')))
.pipe(athenaMate.replace({
cwd: appPath,
module: moduleName,
Expand Down
60 changes: 58 additions & 2 deletions lib/build/tasks/serve_css_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ module.exports = function ($, appConf, moduleConf, args) {
var fs = require('fs');
var vfs = require('vinyl-fs');
var path = require('path');
var athenaMate = require('../athena_mate');
var Util = require( '../../util' );
var through2 = require('through2');
var autoprefixer = require('autoprefixer');
var pxtorem = require('postcss-pxtorem');
var sprites = require('athena-spritesmith');
var es = require('event-stream');
var del = require('del');

var athenaMate = require('../athena_mate');
var Util = require( '../../util' );
//是否开启px转rem
var px2rem = moduleConf.support.px2rem;
//是否开启雪碧图合并
var csssprite = moduleConf.support.csssprite;
var platform = appConf.platform ? appConf.platform : 'mobile';

var autoprefixerConf = moduleConf.support.autoprefixer;
Expand All @@ -44,6 +49,21 @@ module.exports = function ($, appConf, moduleConf, args) {
}));
}

if(csssprite && csssprite.enable !== false){
var opts = {
outputDimensions: true,
stylesheetPath: path.join(modulePath, 'dist', '_static', 'css'),
imageFolder: 'images',
spritePath: path.join(modulePath, 'dist', '_static', 'images', csssprite.spriteFolder ? csssprite.spriteFolder : '', 'sprite.png'),
retina: csssprite.retina || false,
rootValue: csssprite.rootValue,
padding: csssprite.padding,
groupBy: function (image) {
return image.urlSpe;
}
};
}

var cssName = path.basename(args.fPath, path.extname(args.fPath)) + '.css';
var athenaPath = Util.getAthenaPath();
if (args.type === 'changed') {
Expand Down Expand Up @@ -221,7 +241,43 @@ module.exports = function ($, appConf, moduleConf, args) {
.pipe($.postcss(processors))
.pipe(vfs.dest(path.join(appPath, moduleName, 'dist', '_static')))
.on('finish', function () {
var fileContents = [];
vfs.src(path.join(appPath, moduleName, 'dist', '_static', '**', '*.css'))
.pipe(through2.obj(function (file, enc, cb) {
if (file.isNull() || file.isStream()) {
return cb(null, file);
}
fileContents.push('/*filepath=' + file.path + '*/\n' + file.contents.toString());
cb();
}, function (cb) {
var file = new $.util.File({
path: path.join(modulePath, 'dist', '_static', 'css', 'sprite.css'),
base: path.join(modulePath, 'dist', '_static', 'css'),
contents: new Buffer(fileContents.join('\n/*sprite_file_split*/\n'))
});
this.push(file);
cb();
}))
.pipe($.postcss([sprites(opts)]))
.pipe(through2.obj(function (file, enc, cb) {
if (file.isNull() || file.isStream()) {
return cb(null, file);
}
var content = file.contents.toString();
fileContents = content.split('/*sprite_file_split*/');
fileContents.forEach(function (item) {
var reg = /filepath=(.*)\*/;
var filepath = item.match(reg)[1];
var file = new $.util.File({
path: filepath,
base: path.join(modulePath, 'dist', '_static'),
contents: new Buffer(item)
});
this.push(file);
}.bind(this));
cb();
}))
.pipe(vfs.dest(path.join(modulePath, 'dist', '_static')))
.pipe(vfs.dest(path.join(appPath, moduleName, 'dist', 'output', 's')))
.pipe(athenaMate.replaceServer({
cwd: appPath,
Expand Down
Loading

0 comments on commit 8e6405b

Please sign in to comment.