Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加内联处理器支持和依赖升级 #100

Open
wants to merge 6 commits into
base: 1.0/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions lib/processor/inline-processor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* @file 内联静态资源 处理器
* @author wuhuiyao([email protected])
*/

var util = require('util');
var inliner = require('inline-resource');
var AbstractProcessor = require('./abstract');

/**
* 静态资源内联处理器
*
* @constructor
* @param {Object} options 初始化参数
*/
function InlineProcessor(options) {
AbstractProcessor.call(this, options);

// inject custom process type tasks
var customTask = this.customTask || {};
Object.keys(customTask).forEach(function (type) {
inliner.addInlineTaskFor(type, customTask[type]);
});
}

util.inherits(InlineProcessor, AbstractProcessor);

InlineProcessor.DEFAULT_OPTIONS = {

/**
* 处理器名称
*
* @const
* @type {string}
*/
name: 'InlineProcessor',

/**
* 要处理的文件
*
* @type {Array}
*/
files: ['*.css', '*.html', '*.tpl', '!dep/**/*'],

/**
* 内联选项,选项参考依赖的 `inline-resource` npm module
*
* @type {Object}
*/
inlineOption: {},

/**
* 自定义的内联任务定义
*
* @type {Object}
*/
customTask: {}
};

/**
* 构建处理前的行为,选择要处理的文件
*
* @param {ProcessContext} processContext 构建环境对象
* @override
*/
InlineProcessor.prototype.beforeAll = function (processContext) {
AbstractProcessor.prototype.beforeAll.apply(this, arguments);

var fileMap = this.fileMap = {};
processContext.getFiles().forEach(function (item) {
fileMap[item.outputPath] = item.data;
});
};

/**
* 构建处理
*
* @param {FileInfo} file 文件信息对象
* @param {ProcessContext} processContext 构建环境对象
* @param {Function} callback 处理完成回调函数
*/
InlineProcessor.prototype.process = function (file, processContext, callback) {
var me = this;

var opts = {
files: [file],
fileMap: this.fileMap
};
var customOpts = me.inlineOption || {};
Object.keys(customOpts).forEach(function (key) {
opts[key] = customOpts[key];
});

try {
var result = inliner.inline(opts);
file.data = result[0].data;
}
catch(ex) {
me.log.error(ex.stack);
}

callback();
};

/**
* 构建处理后的行为
*
* @param {ProcessContext} processContext 构建环境对象
* @override
*/
InlineProcessor.prototype.afterAll = function (processContext) {
AbstractProcessor.prototype.afterAll.apply(this, arguments);
this.fileMap = null;
};

module.exports = exports = InlineProcessor;
3 changes: 2 additions & 1 deletion lib/processor/less-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ LessCompiler.prototype.process = function (file, processContext, callback) {
var options = u.extend({
relativeUrls: true,
compress: true,
paths: paths
paths: paths,
filename: file.fullPath
}, this.compileOptions);

try {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "edp-build",
"description": "Package for edp build",
"version": "1.0.15-beta.25",
"version": "1.1.1",
"contributors": [
{
"name": "errorrik",
Expand Down Expand Up @@ -33,10 +33,11 @@
"estraverse": "~3.1.0",
"html2js": "~0.2.0",
"iconv-lite": "~0.2.11",
"less": "~1.7.0",
"inline-resource": "^0.1.0",
"less": "^2.5.3",
"mkdirp": "~0.5.0",
"q": "^1.2.0",
"stylus": "~0.44.0",
"stylus": "^0.52.4",
"uglify-js": "~2.4.12",
"underscore": "~1.8.3"
},
Expand Down
2 changes: 1 addition & 1 deletion test/babel-processor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('babel-processor', function () {

base.launchProcessors([p0, p1], processContext, function () {
var fi = processContext.getFileByPath('src/hello.es6');
expect(fi.data).toEqual('define(["exports","babel-runtime/helpers/inherits","babel-runtime/helpers/class-call-check"],function(exports,e,t){(function(l){function n(){if(t["default"](this,n),null!=l)l.apply(this,arguments)}return e["default"](n,l),n.prototype.toString=function(){console.log("HelloWorld")},n})(Array)});');
expect(fi.data).toEqual('define(["exports","babel-runtime/helpers/inherits","babel-runtime/helpers/class-call-check"],function(exports,e,t){(function(n){function l(){t["default"](this,l),n.apply(this,arguments)}return e["default"](l,n),l.prototype.toString=function(){console.log("HelloWorld")},l})(Array)});');
done();
});
});
Expand Down
42 changes: 42 additions & 0 deletions test/data/inline-processor/output/templates/index.custom.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{strip}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<style>#wrap img{ldelim}display:block;border:0{rdelim}#wrap em{ldelim}color:#C60A00;font-style:normal{rdelim}#wrap a{ldelim}text-decoration:none{rdelim}#wrap a:hover{ldelim}text-decoration:underline{rdelim}#wrap .bg{ldelim}display:inline-block;width:16px;height:16px;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACDElEQVR4nLVSPWhTURg9992bF5M0kiGUUh2KpkoFUWs6qINDIw46dvMvWlALVUEnuwlKhyA6iNpF8XdRoUMXcSgOsSKxghmtOmloTO3PS9/Lu/fd+zmIJWlMwcG7ne+c73DPuRf4n+fQxPzpwxOVk2tpxFok935eITIawP1WGqsVkRkvH4wY2RGlYMOBF6XMPxvw5fmco8hxFDnMW8y10rF6sP9ZKQmilKwu7G23g+tz0rpMAJK2Gf0h+QURT7xjjH96PdAxt2Kw5+HMvrCRLwV0zCLzuxRm4BkBadmcYBA2gY6wAICFAIBhFgLwZR+hjDV1PJX3NF21tIIvNb67KjvrsZ4lp2rns1vNm2yPWaxW7VmfbSu56oQvNSyt4Cl9bSrb/XYlQnqsOBRH7banqOyLcOrD2R1Ofbxdd4sRW3szUcE6l0zo3PuhnbcaSiyc2X5nTvEjHKadG/m5qW2tvgoynRUfg3+Wm17h43DvU1ebivblt9UGRtbKrtKl4vm+e/Xzho/UncsLpnXSVcGDzaOTuyO2eM4A1FQw4Er/VUTwi003a0B2NE1aA0SD8RArQAddpIOumGAFRnSKjMGWm9O9LQ0CJdMGBA4kHIlHHo/GPBFrq0r2hAMJgKCV39cyAjO631E0iXXrj325lK7v4eimG9Mj8BYeQ5h+AGOrowAANo6Mx/9KNGra6vEvz6L1ybL0deIAAAAASUVORK5CYII=) 0 0 no-repeat{rdelim}</style>
</head>
<body>
<div id="wrap">
<div class="content">
{foreach $tplData.itemList as $item}
<a class="item-img" href="{$item.url}" target="_blank">
<img src="{$item.img}">
</a>
{/foreach}
</div>
<span class="bg"></span>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACDElEQVR4nLVSPWhTURg9992bF5M0kiGUUh2KpkoFUWs6qINDIw46dvMvWlALVUEnuwlKhyA6iNpF8XdRoUMXcSgOsSKxghmtOmloTO3PS9/Lu/fd+zmIJWlMwcG7ne+c73DPuRf4n+fQxPzpwxOVk2tpxFok935eITIawP1WGqsVkRkvH4wY2RGlYMOBF6XMPxvw5fmco8hxFDnMW8y10rF6sP9ZKQmilKwu7G23g+tz0rpMAJK2Gf0h+QURT7xjjH96PdAxt2Kw5+HMvrCRLwV0zCLzuxRm4BkBadmcYBA2gY6wAICFAIBhFgLwZR+hjDV1PJX3NF21tIIvNb67KjvrsZ4lp2rns1vNm2yPWaxW7VmfbSu56oQvNSyt4Cl9bSrb/XYlQnqsOBRH7banqOyLcOrD2R1Ofbxdd4sRW3szUcE6l0zo3PuhnbcaSiyc2X5nTvEjHKadG/m5qW2tvgoynRUfg3+Wm17h43DvU1ebivblt9UGRtbKrtKl4vm+e/Xzho/UncsLpnXSVcGDzaOTuyO2eM4A1FQw4Er/VUTwi003a0B2NE1aA0SD8RArQAddpIOumGAFRnSKjMGWm9O9LQ0CJdMGBA4kHIlHHo/GPBFrq0r2hAMJgKCV39cyAjO631E0iXXrj325lK7v4eimG9Mj8BYeQ5h+AGOrowAANo6Mx/9KNGra6vEvz6L1ybL0deIAAAAASUVORK5CYII=">
</div>
<script>
(function () {ldelim}
/**
* @file log util
* @author test
*/

var gLog = {ldelim}ldelim{rdelim}{ldelim}rdelim{rdelim};

/**
* output debug info
*
* @param {ldelim}ldelim{rdelim}string{ldelim}rdelim{rdelim} info the information to output
*/
gLog.debug = function (info) {ldelim}ldelim{rdelim}
console.log(info);
{ldelim}rdelim{rdelim};

{rdelim})();
</script>
</body>
{/strip}
42 changes: 42 additions & 0 deletions test/data/inline-processor/output/templates/index.default.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{strip}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<style>#wrap img{display:block;border:0}#wrap em{color:#C60A00;font-style:normal}#wrap a{text-decoration:none}#wrap a:hover{text-decoration:underline}#wrap .bg{display:inline-block;width:16px;height:16px;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACDElEQVR4nLVSPWhTURg9992bF5M0kiGUUh2KpkoFUWs6qINDIw46dvMvWlALVUEnuwlKhyA6iNpF8XdRoUMXcSgOsSKxghmtOmloTO3PS9/Lu/fd+zmIJWlMwcG7ne+c73DPuRf4n+fQxPzpwxOVk2tpxFok935eITIawP1WGqsVkRkvH4wY2RGlYMOBF6XMPxvw5fmco8hxFDnMW8y10rF6sP9ZKQmilKwu7G23g+tz0rpMAJK2Gf0h+QURT7xjjH96PdAxt2Kw5+HMvrCRLwV0zCLzuxRm4BkBadmcYBA2gY6wAICFAIBhFgLwZR+hjDV1PJX3NF21tIIvNb67KjvrsZ4lp2rns1vNm2yPWaxW7VmfbSu56oQvNSyt4Cl9bSrb/XYlQnqsOBRH7banqOyLcOrD2R1Ofbxdd4sRW3szUcE6l0zo3PuhnbcaSiyc2X5nTvEjHKadG/m5qW2tvgoynRUfg3+Wm17h43DvU1ebivblt9UGRtbKrtKl4vm+e/Xzho/UncsLpnXSVcGDzaOTuyO2eM4A1FQw4Er/VUTwi003a0B2NE1aA0SD8RArQAddpIOumGAFRnSKjMGWm9O9LQ0CJdMGBA4kHIlHHo/GPBFrq0r2hAMJgKCV39cyAjO631E0iXXrj325lK7v4eimG9Mj8BYeQ5h+AGOrowAANo6Mx/9KNGra6vEvz6L1ybL0deIAAAAASUVORK5CYII=) 0 0 no-repeat}</style>
</head>
<body>
<div id="wrap">
<div class="content">
{foreach $tplData.itemList as $item}
<a class="item-img" href="{$item.url}" target="_blank">
<img src="{$item.img}">
</a>
{/foreach}
</div>
<span class="bg"></span>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACDElEQVR4nLVSPWhTURg9992bF5M0kiGUUh2KpkoFUWs6qINDIw46dvMvWlALVUEnuwlKhyA6iNpF8XdRoUMXcSgOsSKxghmtOmloTO3PS9/Lu/fd+zmIJWlMwcG7ne+c73DPuRf4n+fQxPzpwxOVk2tpxFok935eITIawP1WGqsVkRkvH4wY2RGlYMOBF6XMPxvw5fmco8hxFDnMW8y10rF6sP9ZKQmilKwu7G23g+tz0rpMAJK2Gf0h+QURT7xjjH96PdAxt2Kw5+HMvrCRLwV0zCLzuxRm4BkBadmcYBA2gY6wAICFAIBhFgLwZR+hjDV1PJX3NF21tIIvNb67KjvrsZ4lp2rns1vNm2yPWaxW7VmfbSu56oQvNSyt4Cl9bSrb/XYlQnqsOBRH7banqOyLcOrD2R1Ofbxdd4sRW3szUcE6l0zo3PuhnbcaSiyc2X5nTvEjHKadG/m5qW2tvgoynRUfg3+Wm17h43DvU1ebivblt9UGRtbKrtKl4vm+e/Xzho/UncsLpnXSVcGDzaOTuyO2eM4A1FQw4Er/VUTwi003a0B2NE1aA0SD8RArQAddpIOumGAFRnSKjMGWm9O9LQ0CJdMGBA4kHIlHHo/GPBFrq0r2hAMJgKCV39cyAjO631E0iXXrj325lK7v4eimG9Mj8BYeQ5h+AGOrowAANo6Mx/9KNGra6vEvz6L1ybL0deIAAAAASUVORK5CYII=">
</div>
<script>
(function () {
/**
* @file log util
* @author test
*/

var gLog = {};

/**
* output debug info
*
* @param {string} info the information to output
*/
gLog.debug = function (info) {
console.log(info);
};

})();
</script>
</body>
{/strip}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions test/data/inline-processor/src/common/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @file log util
* @author test
*/

var gLog = {};

/**
* output debug info
*
* @param {string} info the information to output
*/
gLog.debug = function (info) {
console.log(info);
};
30 changes: 30 additions & 0 deletions test/data/inline-processor/src/index/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @file test style file
* @author test
*/
#wrap {
img {
display: block;
border: 0;
}

em {
color: #C60A00;
font-style: normal;
}

a {
text-decoration: none;

&:hover {
text-decoration: underline;
}
}

.bg {
display: inline-block;
width: 16px;
height: 16px;
background: url(../common/img/bookmark.png?_inline) 0 0 no-repeat;
}
}
27 changes: 27 additions & 0 deletions test/data/inline-processor/templates/index.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{strip}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<link href="{$host}/src/index/index.less?_inline" rel="stylesheet">
</head>
<body>
<div id="wrap">
<div class="content">
{foreach $tplData.itemList as $item}
<a class="item-img" href="{$item.url}" target="_blank">
<img src="{$item.img}">
</a>
{/foreach}
</div>
<span class="bg"></span>
<img src="{$host}/src/common/img/bookmark.png?_inline">
</div>
<script>
(function () {
document.write('<script src="{$host}/src/common/log.js?_inline"></script>');
})();
</script>
</body>
{/strip}
Loading