From 5aff8f82a0a52f2c924d8418818128f9e1b9bf39 Mon Sep 17 00:00:00 2001 From: leeight Date: Mon, 6 Jul 2015 15:05:34 +0800 Subject: [PATCH 1/5] bump to 1.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 81440ba..0be07bc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "edp-build", "description": "Package for edp build", - "version": "1.0.15-beta.25", + "version": "1.1.0", "contributors": [ { "name": "errorrik", From 0167ab89dc6c2d6155434d83f92fc39f7bc801e8 Mon Sep 17 00:00:00 2001 From: chestnut Date: Fri, 10 Jul 2015 15:14:25 +0800 Subject: [PATCH 2/5] fix #95 --- lib/processor/less-compiler.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/processor/less-compiler.js b/lib/processor/less-compiler.js index cd13f9d..bbd6d0d 100644 --- a/lib/processor/less-compiler.js +++ b/lib/processor/less-compiler.js @@ -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 { From b410ed666dc5dfc9d067a893c6b326d57ad2e400 Mon Sep 17 00:00:00 2001 From: leeight Date: Fri, 10 Jul 2015 15:27:57 +0800 Subject: [PATCH 3/5] bump to 1.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0be07bc..37d13c0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "edp-build", "description": "Package for edp build", - "version": "1.1.0", + "version": "1.1.1", "contributors": [ { "name": "errorrik", From 037837cff8777ce47f908149b4f6fbd9a0a8f9a2 Mon Sep 17 00:00:00 2001 From: wuhy Date: Thu, 15 Oct 2015 15:24:37 +0800 Subject: [PATCH 4/5] add inline processor and upgrade dependence stylus/less --- lib/processor/inline-processor.js | 116 ++++++++++++++++++ package.json | 5 +- .../output/templates/index.custom.tpl | 42 +++++++ .../output/templates/index.default.tpl | 42 +++++++ .../src/common/img/bookmark.png | Bin 0 -> 581 bytes test/data/inline-processor/src/common/log.js | 15 +++ .../inline-processor/src/index/index.less | 30 +++++ .../data/inline-processor/templates/index.tpl | 27 ++++ test/inline-processor.spec.js | 108 ++++++++++++++++ 9 files changed, 383 insertions(+), 2 deletions(-) create mode 100644 lib/processor/inline-processor.js create mode 100644 test/data/inline-processor/output/templates/index.custom.tpl create mode 100644 test/data/inline-processor/output/templates/index.default.tpl create mode 100644 test/data/inline-processor/src/common/img/bookmark.png create mode 100644 test/data/inline-processor/src/common/log.js create mode 100644 test/data/inline-processor/src/index/index.less create mode 100644 test/data/inline-processor/templates/index.tpl create mode 100644 test/inline-processor.spec.js diff --git a/lib/processor/inline-processor.js b/lib/processor/inline-processor.js new file mode 100644 index 0000000..1f6eb89 --- /dev/null +++ b/lib/processor/inline-processor.js @@ -0,0 +1,116 @@ +/** + * @file 内联静态资源 处理器 + * @author wuhuiyao(sparklewhy@gmail.com) + */ + +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; diff --git a/package.json b/package.json index 37d13c0..02d97ef 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/data/inline-processor/output/templates/index.custom.tpl b/test/data/inline-processor/output/templates/index.custom.tpl new file mode 100644 index 0000000..07a58f1 --- /dev/null +++ b/test/data/inline-processor/output/templates/index.custom.tpl @@ -0,0 +1,42 @@ +{strip} + + + + + test + + + +
+
+ {foreach $tplData.itemList as $item} + + + + {/foreach} +
+ + +
+ + +{/strip} diff --git a/test/data/inline-processor/output/templates/index.default.tpl b/test/data/inline-processor/output/templates/index.default.tpl new file mode 100644 index 0000000..d798592 --- /dev/null +++ b/test/data/inline-processor/output/templates/index.default.tpl @@ -0,0 +1,42 @@ +{strip} + + + + + test + + + +
+
+ {foreach $tplData.itemList as $item} + + + + {/foreach} +
+ + +
+ + +{/strip} diff --git a/test/data/inline-processor/src/common/img/bookmark.png b/test/data/inline-processor/src/common/img/bookmark.png new file mode 100644 index 0000000000000000000000000000000000000000..ec779a33b681686037f1240a04f513096c31ecf8 GIT binary patch literal 581 zcmV-L0=oT)P)6H_@C#{#Qf>Q6P1%| zX~bG2_kLa>G8(}BRvN1nkr^)^j2PJwrC`H>7p2TU8}Q}%oTJEb6gkXW%(c?7da(al zDG8#KtS;N z83Ae8oL~?(fsU{MfQ0~nVHN`LWgnr8HFZ3d_cUF#w1O`+zPl#?3Cr)uZ3Tg{tn zk6El%?OC5~E4k`~FEuQ!;3<7=D%<^bB|>_vI21?iwx_7LWVkA1n6uJ7UKrW?~E@D?6Jn0^3btD^t_Hja$P|4KA!+UoHy&!Y9o Tvh;Q000000NkvXXu0mjf+yM_W literal 0 HcmV?d00001 diff --git a/test/data/inline-processor/src/common/log.js b/test/data/inline-processor/src/common/log.js new file mode 100644 index 0000000..a97c97c --- /dev/null +++ b/test/data/inline-processor/src/common/log.js @@ -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); +}; diff --git a/test/data/inline-processor/src/index/index.less b/test/data/inline-processor/src/index/index.less new file mode 100644 index 0000000..be4e42c --- /dev/null +++ b/test/data/inline-processor/src/index/index.less @@ -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; + } +} diff --git a/test/data/inline-processor/templates/index.tpl b/test/data/inline-processor/templates/index.tpl new file mode 100644 index 0000000..fcd6cc3 --- /dev/null +++ b/test/data/inline-processor/templates/index.tpl @@ -0,0 +1,27 @@ +{strip} + + + + + test + + + +
+
+ {foreach $tplData.itemList as $item} + + + + {/foreach} +
+ + +
+'); + })(); + + +{/strip} diff --git a/test/inline-processor.spec.js b/test/inline-processor.spec.js new file mode 100644 index 0000000..4ac7264 --- /dev/null +++ b/test/inline-processor.spec.js @@ -0,0 +1,108 @@ +/*************************************************************************** + * + * Copyright (c) 2015 Baidu.com, Inc. All Rights Reserved + * $Id$ + * + **************************************************************************/ + + + +/** + * inline-procesor.spec.js ~ 2015/10/08 09:25:52 + * @author wuhuiyao(sparklewhy@gmail.com) + * @version $Revision$ + * @description + * + **/ +var fs = require('fs'); +var path = require('path'); +var ProcessContext = require('../lib/process-context'); +var InlineProcessor = require('../lib/processor/inline-processor.js'); +var LessCompiler = require('../lib/processor/less-compiler.js'); +var base = require('./base'); + +var Project = path.resolve(__dirname, 'data', 'inline-processor'); + +describe('inline-processor', function() { + it('default', function(done) { + var processContext = new ProcessContext({ + baseDir: Project, + exclude: ['output'], + outputDir: 'output', + fileEncodings: {} + }); + + base.traverseDir(Project, processContext); + + var lessCompiler = new LessCompiler(); + var inlineProcessor = new InlineProcessor({ + files: ['templates/index.tpl'], + inlineOption: { + inlinePathGetter: function (path) { + var regexp = /\{\$host\}\//; + if (regexp.test(path)) { + var newPath = path.replace(regexp, ''); + return {path: newPath, dir: '.'}; + } + return path; + } + } + }); + + base.launchProcessors([lessCompiler, inlineProcessor], processContext, function () { + var tplData = processContext.getFileByPath('templates/index.tpl'); + expect(tplData).not.toBeNull(); + + var expectedData = fs.readFileSync(path.join(__dirname, 'data/inline-processor', 'output/templates/index.default.tpl')).toString(); + expect(tplData.data).toEqual(expectedData); + + done(); + }); + }); + + it('custom tasks', function(done) { + var processContext = new ProcessContext({ + baseDir: Project, + exclude: ['output'], + outputDir: 'output', + fileEncodings: {} + }); + + base.traverseDir(Project, processContext); + + var lessCompiler = new LessCompiler(); + + var escapeTask = function (file) { + return file.data.replace(/\{|\}/g, function (match) { + return {'{': '{ldelim}', '}': '{rdelim}'}[match]; + }); + }; + var inlineProcessor = new InlineProcessor({ + files: ['templates/index.tpl'], + customTask: { + js: escapeTask, + css: escapeTask + }, + inlineOption: { + inlinePathGetter: function (path) { + var regexp = /\{\$host\}\//; + if (regexp.test(path)) { + var newPath = path.replace(regexp, ''); + return {path: newPath, dir: '.'}; + } + return path; + } + } + }); + + base.launchProcessors([lessCompiler, inlineProcessor], processContext, function () { + var tplData = processContext.getFileByPath('templates/index.tpl'); + expect(tplData).not.toBeNull(); + + var expectedData = fs.readFileSync(path.join(__dirname, 'data/inline-processor', 'output/templates/index.custom.tpl')).toString(); + expect(tplData.data).toEqual(expectedData); + + done(); + }); + }); +}); From 5bc7a1f7f83ece4bc08833ac21c3aa1e5a1a187a Mon Sep 17 00:00:00 2001 From: wuhy Date: Thu, 15 Oct 2015 15:25:24 +0800 Subject: [PATCH 5/5] fix test case --- test/babel-processor.spec.js | 2 +- test/issue-94.spec.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/babel-processor.spec.js b/test/babel-processor.spec.js index 2ed2405..2201f1b 100644 --- a/test/babel-processor.spec.js +++ b/test/babel-processor.spec.js @@ -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(); }); }); diff --git a/test/issue-94.spec.js b/test/issue-94.spec.js index 9fb7ab6..d3c4985 100644 --- a/test/issue-94.spec.js +++ b/test/issue-94.spec.js @@ -82,7 +82,7 @@ describe('issue-94', function () { base.launchProcessors([p0, p1, p1], processContext, function () { var fi = processContext.getFileByPath('src/issue-94.js'); - expect(fi.data).toEqual("define([\n 'exports',\n 'bat-ria/tpl!startup/template',\n 'bat-ria/tpl!startup/template',\n 'er/View',\n 'babel-runtime/helpers/interop-require-default',\n 'no-such-plugin!./tpl/123.html'\n], function (exports, _batRiaTplTplListTplHtml, _batRiaTplTpl123Html, _erView, _babelRuntimeHelpersInteropRequireDefault, _noSuchPluginTpl123Html) {\n var _View = (0, _babelRuntimeHelpersInteropRequireDefault['default'])(_erView);\n});"); + expect(fi.data).toEqual("define([\n 'exports',\n 'bat-ria/tpl!startup/template',\n 'bat-ria/tpl!startup/template',\n 'er/View',\n 'babel-runtime/helpers/interop-require-default',\n 'no-such-plugin!./tpl/123.html'\n], function (exports, _batRiaTplTplListTplHtml, _batRiaTplTpl123Html, _erView, _babelRuntimeHelpersInteropRequireDefault, _noSuchPluginTpl123Html) {\n var _View = _babelRuntimeHelpersInteropRequireDefault['default'](_erView);\n});"); done(); }); });