From 92c2bb4116e05ff012e79a9c7e1203c7cae80cda Mon Sep 17 00:00:00 2001 From: crossjs Date: Mon, 11 Aug 2014 23:49:18 +0800 Subject: [PATCH 1/5] upgrade handlebars; change default css parser --- tasks/lib/style.js | 17 ++++++----------- tasks/lib/template.js | 14 ++------------ tasks/transport.js | 30 ++++++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/tasks/lib/style.js b/tasks/lib/style.js index dd6555f..0220678 100644 --- a/tasks/lib/style.js +++ b/tasks/lib/style.js @@ -19,7 +19,10 @@ exports.init = function(grunt) { var data = fileObj.srcData || grunt.file.read(fileObj.src); var id = unixy(options.idleading + fileObj.name.replace(/\.js$/, '')); - data = css2js(data, id, options, fileObj); + // importstyle alias + var alias = options.alias.importstyle || options.importstyle.id; + + data = css2js(data, id, alias, options, fileObj); data = ast.getAst(data).print_to_string(options.uglify); var dest = fileObj.dest + '.js'; grunt.file.write(dest, data); @@ -124,7 +127,7 @@ function parseRules(rules, prefix) { }); } -function css2js(code, id, options, fileObj) { +function css2js(code, id, alias, options, fileObj) { // ex. arale/widget/1.0.0/ => arale-widget-1_0_0 var styleId = unixy((options || {}).idleading || '') .replace(/\/$/, '') @@ -158,20 +161,12 @@ function css2js(code, id, options, fileObj) { removeEmpty: true }); - // transform css to js - // spmjs/spm#581 - var tpl = [ - 'define("%s", [], function() {', - "seajs.importStyle('%s')", - '});' - ].join('\n'); - // spmjs/spm#651 code = code.split(/\r\n|\r|\n/).map(function(line) { return line.replace(/\\/g, '\\\\'); }).join('\n'); - code = format(tpl, id, code.replace(/\'/g, '\\\'')); + code = format(options.css.template, id, alias, alias, code.replace(/\'/g, '\\\''), id); return code; } diff --git a/tasks/lib/template.js b/tasks/lib/template.js index d688126..90d8d25 100644 --- a/tasks/lib/template.js +++ b/tasks/lib/template.js @@ -47,24 +47,14 @@ exports.init = function(grunt) { var id = unixy(options.idleading + fileObj.name.replace(/\.js$/, '')); // handlebars alias - var alias = options.handlebars.id; - - var template = [ - 'define("%s", ["%s"], function(require, exports, module) {', - 'var Handlebars = require("%s");', - 'var template = Handlebars.template;', - 'module.exports = template(', - '%s', - ');', - '})' - ].join('\n'); + var alias = options.alias.handlebars || options.handlebars.id; var data = fileObj.srcData || grunt.file.read(fileObj.src); patchHandlebars(handlebars); var code = handlebars.precompile(data, options.handlebars); - var ret = format(template, id, alias, alias, code); + var ret = format(options.handlebars.template, id, alias, alias, code); var astCache = ast.getAst(ret); data = astCache.print_to_string(options.uglify); diff --git a/tasks/transport.js b/tasks/transport.js index a0abf1f..2075f94 100644 --- a/tasks/transport.js +++ b/tasks/transport.js @@ -22,7 +22,10 @@ module.exports = function(grunt) { paths: ['sea-modules'], idleading: '', - alias: {}, + alias: { + // importstyle: 'pandora/importstyle/1.0.0/importstyle', + // handlebars: 'gallery/handlebars/1.3.0/handlebars-runtime' + }, // create a debug file or not debug: true, @@ -33,18 +36,37 @@ module.exports = function(grunt) { // define parsers parsers: { '.js': [script.jsParser], - '.css': [style.cssParser], + '.css': [style.css2jsParser], '.html': [text.html2jsParser], '.json': [json.jsonParser], '.tpl': [template.tplParser], '.handlebars': [template.handlebarsParser] }, + // for styles + css: { + template: [ + 'define(\'%s\', [\'%s\'], function(require, exports, module) {', + 'var importStyle = require(\'%s\');', + 'module.exports = function() {', + 'importStyle(\'%s\', \'%s\');', + '};', + '});' + ].join('\n') + }, + // for handlebars handlebars: { - id: 'gallery/handlebars/1.0.2/runtime', knownHelpers: [], - knownHelpersOnly: false + knownHelpersOnly: false, + template: [ + 'define(\'%s\', [\'%s\'], function(require, exports, module) {', + 'var Handlebars = require(\'%s\');', + 'module.exports = Handlebars.template(', + '%s', + ');', + '})' + ].join('\n') }, // output beautifier From 43f63fec5c1b190b4d018d5bbe4228f332a9df7f Mon Sep 17 00:00:00 2001 From: crossjs Date: Tue, 12 Aug 2014 14:06:45 +0800 Subject: [PATCH 2/5] update config --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 28f3ab4..05b7d27 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "grunt-cmd-transport", "description": "Transport javascript into cmd.", - "version": "0.4.1", + "version": "0.4.2", "homepage": "https://github.com/spmjs/grunt-cmd-transport", "author": { "name": "Hsiaoming Yang", @@ -29,7 +29,7 @@ "dependencies": { "clean-css": "~1.0.1", "cmd-util": "~0.3.5", - "handlebars": "1.0.11", + "handlebars": "1.3.0", "uglify-js": "~2.2.5", "css": "~1.4.0" }, @@ -43,4 +43,4 @@ "keywords": [ "gruntplugin" ] -} \ No newline at end of file +} From 8dfe68d576b5ce059c6ae2a4732ac48c5b9bfccd Mon Sep 17 00:00:00 2001 From: crossjs Date: Mon, 1 Sep 2014 22:05:15 +0800 Subject: [PATCH 3/5] Update README.md --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a8335bc..9803828 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ Prepend idleading to generate the id of the module. Type: `Object` Default value: `{}` -Alias of modules. +Alias of modules, such as handlebars and importStyle id. #### options.debug @@ -69,6 +69,8 @@ Create a debugfile or not. #### options.handlebars +**deprecated**, use `options.alias` instead. + Type: `Object` Options for handlebars compiler. @@ -131,6 +133,14 @@ In lieu of a formal styleguide, take care to maintain the existing coding style. ## Release History +**Sep 1st, 2014** `0.4.2` + +- get `handlebars` and `importStyle` id in `options.alias` + +- set default css parser to `css2jsParser` + +- add `importStyle` template + **Dec 4th, 2013** `0.4.0` fix Windows path #58 From c39b81d1c814102e604679ee1a03f65293ed9219 Mon Sep 17 00:00:00 2001 From: crossjs Date: Mon, 1 Sep 2014 23:17:47 +0800 Subject: [PATCH 4/5] Update a.css.js.expect --- test/cases/css2js/a.css.js.expect | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/cases/css2js/a.css.js.expect b/test/cases/css2js/a.css.js.expect index 972e288..5782f9f 100644 --- a/test/cases/css2js/a.css.js.expect +++ b/test/cases/css2js/a.css.js.expect @@ -1,3 +1,6 @@ -define("a.css", [], function() { - seajs.importStyle("body{color:#fff}"); -}); \ No newline at end of file +define("a.css", ["importstyle"], function() { + var importStyle = require("importstyle"); + module.exports = function() { + importStyle("body{color:#fff}", "a.css"); + }; +}); From d795e14b68710782ed2a7df39fdcd9cef77fed6d Mon Sep 17 00:00:00 2001 From: crossjs Date: Tue, 2 Sep 2014 00:24:18 +0800 Subject: [PATCH 5/5] update unit tests --- Gruntfile.js | 18 ++++++++++- test/cases/css2js/a-debug.css.js.expect | 7 +++-- test/cases/css2js/a.css.js.expect | 6 ++-- .../month-debug.handlebars.js.expect | 31 ++++++++++--------- .../handlebars/month.handlebars.js.expect | 31 ++++++++++--------- .../simple-debug.handlebars.js.expect | 18 +++++------ .../handlebars/simple.handlebars.js.expect | 18 +++++------ test/cases/style/a-debug.css.js.expect | 7 +++-- test/cases/style/a.css.js.expect | 7 +++-- 9 files changed, 83 insertions(+), 60 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index cabd0e2..ef2bcd2 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -10,6 +10,8 @@ module.exports = function(grunt) { var style = require('./').style.init(grunt); + var cssParser = style.cssParser; + // now as default var css2jsParser = style.css2jsParser; var jsParser = require('./').script.init(grunt).jsParser; @@ -125,6 +127,9 @@ module.exports = function(grunt) { options: { alias: { 'button': 'alice/button/1.0.0/button.css' + }, + parsers: { + '.css': [cssParser] } }, files: [{ @@ -147,6 +152,11 @@ module.exports = function(grunt) { // parsing handlebars into js handlebars: { + options: { + alias: { + 'handlebars': 'gallery/handlebars/1.3.0/handlebars-runtime' + } + }, files: [{ expand: true, cwd: 'test/cases/handlebars', @@ -167,6 +177,9 @@ module.exports = function(grunt) { css2js: { options: { + alias: { + 'importstyle': 'pandora/importstyle/1.0.0/importstyle' + }, parsers: { '.css': [css2jsParser] } @@ -181,11 +194,14 @@ module.exports = function(grunt) { style: { options: { + alias: { + 'importstyle': 'pandora/importstyle/1.0.0/importstyle' + }, parsers: { '.css': [css2jsParser], '.js': [jsParser] }, - styleBox: ["a.css"], + styleBox: ['a.css'], idleading: 'arale/widget/1.0.0/' }, files: [{ diff --git a/test/cases/css2js/a-debug.css.js.expect b/test/cases/css2js/a-debug.css.js.expect index 2d7f3e1..3db94c4 100644 --- a/test/cases/css2js/a-debug.css.js.expect +++ b/test/cases/css2js/a-debug.css.js.expect @@ -1,3 +1,6 @@ -define("a-debug.css", [], function() { - seajs.importStyle("body{color:#fff}"); +define("a-debug.css", [ "pandora/importstyle/1.0.0/importstyle-debug" ], function(require, exports, module) { + var importStyle = require("pandora/importstyle/1.0.0/importstyle-debug"); + module.exports = function() { + importStyle("body{color:#fff}", "a.css"); + }; }); \ No newline at end of file diff --git a/test/cases/css2js/a.css.js.expect b/test/cases/css2js/a.css.js.expect index 5782f9f..f40c030 100644 --- a/test/cases/css2js/a.css.js.expect +++ b/test/cases/css2js/a.css.js.expect @@ -1,6 +1,6 @@ -define("a.css", ["importstyle"], function() { - var importStyle = require("importstyle"); +define("a.css", [ "pandora/importstyle/1.0.0/importstyle" ], function(require, exports, module) { + var importStyle = require("pandora/importstyle/1.0.0/importstyle"); module.exports = function() { importStyle("body{color:#fff}", "a.css"); }; -}); +}); \ No newline at end of file diff --git a/test/cases/handlebars/month-debug.handlebars.js.expect b/test/cases/handlebars/month-debug.handlebars.js.expect index ade6979..49ed95f 100644 --- a/test/cases/handlebars/month-debug.handlebars.js.expect +++ b/test/cases/handlebars/month-debug.handlebars.js.expect @@ -1,8 +1,7 @@ -define("month-debug.handlebars", [ "gallery/handlebars/1.0.2/runtime-debug" ], function(require, exports, module) { - var Handlebars = require("gallery/handlebars/1.0.2/runtime-debug"); - var template = Handlebars.template; - module.exports = template(function(Handlebars, depth0, helpers, partials, data) { - this.compilerInfo = [ 3, ">= 1.0.0-rc.4" ]; +define("month-debug.handlebars", [ "gallery/handlebars/1.3.0/handlebars-runtime-debug" ], function(require, exports, module) { + var Handlebars = require("gallery/handlebars/1.3.0/handlebars-runtime-debug"); + module.exports = Handlebars.template(function(Handlebars, depth0, helpers, partials, data) { + this.compilerInfo = [ 4, ">= 1.0.0" ]; helpers = helpers || {}; for (var key in Handlebars.helpers) { helpers[key] = helpers[key] || Handlebars.helpers[key]; @@ -25,9 +24,9 @@ define("month-debug.handlebars", [ "gallery/handlebars/1.0.2/runtime-debug" ], f return buffer; } function program2(depth0, data) { - var buffer = "", stack1, options; + var buffer = "", stack1, helper, options; buffer += '\n
  • '; + buffer += escapeExpression(stack1) + '">' + escapeExpression((helper = helpers._ || depth0 && depth0._, options = { hash: {}, data: data - }; - buffer += escapeExpression((stack1 = helpers["_"], stack1 ? stack1.call(depth0, depth0.label, options) : helperMissing.call(depth0, "_", depth0.label, options))) + "
  • \n "; + }, helper ? helper.call(depth0, depth0 && depth0.label, options) : helperMissing.call(depth0, "_", depth0 && depth0.label, options))) + "\n "; return buffer; } function program3(depth0, data) { return "disabled-element"; } buffer += '
    \n'; - stack1 = helpers.each.call(depth0, depth0.items, { + stack1 = helpers.each.call(depth0, depth0 && depth0.items, { hash: {}, inverse: self.noop, fn: self.program(1, program1, data), diff --git a/test/cases/handlebars/month.handlebars.js.expect b/test/cases/handlebars/month.handlebars.js.expect index 95fec75..39e6b2e 100644 --- a/test/cases/handlebars/month.handlebars.js.expect +++ b/test/cases/handlebars/month.handlebars.js.expect @@ -1,8 +1,7 @@ -define("month.handlebars", [ "gallery/handlebars/1.0.2/runtime" ], function(require, exports, module) { - var Handlebars = require("gallery/handlebars/1.0.2/runtime"); - var template = Handlebars.template; - module.exports = template(function(Handlebars, depth0, helpers, partials, data) { - this.compilerInfo = [ 3, ">= 1.0.0-rc.4" ]; +define("month.handlebars", [ "gallery/handlebars/1.3.0/handlebars-runtime" ], function(require, exports, module) { + var Handlebars = require("gallery/handlebars/1.3.0/handlebars-runtime"); + module.exports = Handlebars.template(function(Handlebars, depth0, helpers, partials, data) { + this.compilerInfo = [ 4, ">= 1.0.0" ]; helpers = helpers || {}; for (var key in Handlebars.helpers) { helpers[key] = helpers[key] || Handlebars.helpers[key]; @@ -25,9 +24,9 @@ define("month.handlebars", [ "gallery/handlebars/1.0.2/runtime" ], function(requ return buffer; } function program2(depth0, data) { - var buffer = "", stack1, options; + var buffer = "", stack1, helper, options; buffer += '\n
  • '; + buffer += escapeExpression(stack1) + '">' + escapeExpression((helper = helpers._ || depth0 && depth0._, options = { hash: {}, data: data - }; - buffer += escapeExpression((stack1 = helpers["_"], stack1 ? stack1.call(depth0, depth0.label, options) : helperMissing.call(depth0, "_", depth0.label, options))) + "
  • \n "; + }, helper ? helper.call(depth0, depth0 && depth0.label, options) : helperMissing.call(depth0, "_", depth0 && depth0.label, options))) + "\n "; return buffer; } function program3(depth0, data) { return "disabled-element"; } buffer += '
    \n'; - stack1 = helpers.each.call(depth0, depth0.items, { + stack1 = helpers.each.call(depth0, depth0 && depth0.items, { hash: {}, inverse: self.noop, fn: self.program(1, program1, data), diff --git a/test/cases/handlebars/simple-debug.handlebars.js.expect b/test/cases/handlebars/simple-debug.handlebars.js.expect index c79a193..146c680 100644 --- a/test/cases/handlebars/simple-debug.handlebars.js.expect +++ b/test/cases/handlebars/simple-debug.handlebars.js.expect @@ -1,8 +1,7 @@ -define("simple-debug.handlebars", [ "gallery/handlebars/1.0.2/runtime-debug" ], function(require, exports, module) { - var Handlebars = require("gallery/handlebars/1.0.2/runtime-debug"); - var template = Handlebars.template; - module.exports = template(function(Handlebars, depth0, helpers, partials, data) { - this.compilerInfo = [ 3, ">= 1.0.0-rc.4" ]; +define("simple-debug.handlebars", [ "gallery/handlebars/1.3.0/handlebars-runtime-debug" ], function(require, exports, module) { + var Handlebars = require("gallery/handlebars/1.3.0/handlebars-runtime-debug"); + module.exports = Handlebars.template(function(Handlebars, depth0, helpers, partials, data) { + this.compilerInfo = [ 4, ">= 1.0.0" ]; helpers = helpers || {}; for (var key in Handlebars.helpers) { helpers[key] = helpers[key] || Handlebars.helpers[key]; @@ -10,16 +9,15 @@ define("simple-debug.handlebars", [ "gallery/handlebars/1.0.2/runtime-debug" ], data = data || {}; var buffer = "", stack1, helperMissing = helpers.helperMissing, escapeExpression = this.escapeExpression, self = this; function program1(depth0, data) { - var buffer = "", stack1, options; - buffer += "\n

    "; + var buffer = "", helper, options; + buffer += "\n

    " + escapeExpression((helper = helpers._ || depth0 && depth0._, options = { hash: {}, data: data - }; - buffer += escapeExpression((stack1 = helpers["_"], stack1 ? stack1.call(depth0, depth0, options) : helperMissing.call(depth0, "_", depth0, options))) + "

    \n"; + }, helper ? helper.call(depth0, depth0, options) : helperMissing.call(depth0, "_", depth0, options))) + "

    \n"; return buffer; } - stack1 = helpers.each.call(depth0, depth0.items, { + stack1 = helpers.each.call(depth0, depth0 && depth0.items, { hash: {}, inverse: self.noop, fn: self.program(1, program1, data), diff --git a/test/cases/handlebars/simple.handlebars.js.expect b/test/cases/handlebars/simple.handlebars.js.expect index 82e8b8f..f367a79 100644 --- a/test/cases/handlebars/simple.handlebars.js.expect +++ b/test/cases/handlebars/simple.handlebars.js.expect @@ -1,8 +1,7 @@ -define("simple.handlebars", [ "gallery/handlebars/1.0.2/runtime" ], function(require, exports, module) { - var Handlebars = require("gallery/handlebars/1.0.2/runtime"); - var template = Handlebars.template; - module.exports = template(function(Handlebars, depth0, helpers, partials, data) { - this.compilerInfo = [ 3, ">= 1.0.0-rc.4" ]; +define("simple.handlebars", [ "gallery/handlebars/1.3.0/handlebars-runtime" ], function(require, exports, module) { + var Handlebars = require("gallery/handlebars/1.3.0/handlebars-runtime"); + module.exports = Handlebars.template(function(Handlebars, depth0, helpers, partials, data) { + this.compilerInfo = [ 4, ">= 1.0.0" ]; helpers = helpers || {}; for (var key in Handlebars.helpers) { helpers[key] = helpers[key] || Handlebars.helpers[key]; @@ -10,16 +9,15 @@ define("simple.handlebars", [ "gallery/handlebars/1.0.2/runtime" ], function(req data = data || {}; var buffer = "", stack1, helperMissing = helpers.helperMissing, escapeExpression = this.escapeExpression, self = this; function program1(depth0, data) { - var buffer = "", stack1, options; - buffer += "\n

    "; + var buffer = "", helper, options; + buffer += "\n

    " + escapeExpression((helper = helpers._ || depth0 && depth0._, options = { hash: {}, data: data - }; - buffer += escapeExpression((stack1 = helpers["_"], stack1 ? stack1.call(depth0, depth0, options) : helperMissing.call(depth0, "_", depth0, options))) + "

    \n"; + }, helper ? helper.call(depth0, depth0, options) : helperMissing.call(depth0, "_", depth0, options))) + "

    \n"; return buffer; } - stack1 = helpers.each.call(depth0, depth0.items, { + stack1 = helpers.each.call(depth0, depth0 && depth0.items, { hash: {}, inverse: self.noop, fn: self.program(1, program1, data), diff --git a/test/cases/style/a-debug.css.js.expect b/test/cases/style/a-debug.css.js.expect index 3a81d37..c32cb85 100644 --- a/test/cases/style/a-debug.css.js.expect +++ b/test/cases/style/a-debug.css.js.expect @@ -1,3 +1,6 @@ -define("arale/widget/1.0.0/a-debug.css", [], function() { - seajs.importStyle('@charset "utf-8";.arale-widget-1_0_0 body{_color:red}.arale-widget-1_0_0 .content li,.arale-widget-1_0_0 .content a{*display:inline-block}@media screen{.arale-widget-1_0_0 body{font-size:13px}.arale-widget-1_0_0 .content li{background-color:#fff}}@keyframes foo{0%{top:0;left:0}100%{top:100px;left:100%}}:root .arale-widget-1_0_0 .content{font-size:12px}'); +define("arale/widget/1.0.0/a-debug.css", [ "pandora/importstyle/1.0.0/importstyle-debug" ], function(require, exports, module) { + var importStyle = require("pandora/importstyle/1.0.0/importstyle-debug"); + module.exports = function() { + importStyle('@charset "utf-8";.arale-widget-1_0_0 body{_color:red}.arale-widget-1_0_0 .content li,.arale-widget-1_0_0 .content a{*display:inline-block}@media screen{.arale-widget-1_0_0 body{font-size:13px}.arale-widget-1_0_0 .content li{background-color:#fff}}@keyframes foo{0%{top:0;left:0}100%{top:100px;left:100%}}:root .arale-widget-1_0_0 .content{font-size:12px}', "arale/widget/1.0.0/a.css"); + }; }); \ No newline at end of file diff --git a/test/cases/style/a.css.js.expect b/test/cases/style/a.css.js.expect index e7f92ba..05ee19e 100644 --- a/test/cases/style/a.css.js.expect +++ b/test/cases/style/a.css.js.expect @@ -1,3 +1,6 @@ -define("arale/widget/1.0.0/a.css", [], function() { - seajs.importStyle('@charset "utf-8";.arale-widget-1_0_0 body{_color:red}.arale-widget-1_0_0 .content li,.arale-widget-1_0_0 .content a{*display:inline-block}@media screen{.arale-widget-1_0_0 body{font-size:13px}.arale-widget-1_0_0 .content li{background-color:#fff}}@keyframes foo{0%{top:0;left:0}100%{top:100px;left:100%}}:root .arale-widget-1_0_0 .content{font-size:12px}'); +define("arale/widget/1.0.0/a.css", [ "pandora/importstyle/1.0.0/importstyle" ], function(require, exports, module) { + var importStyle = require("pandora/importstyle/1.0.0/importstyle"); + module.exports = function() { + importStyle('@charset "utf-8";.arale-widget-1_0_0 body{_color:red}.arale-widget-1_0_0 .content li,.arale-widget-1_0_0 .content a{*display:inline-block}@media screen{.arale-widget-1_0_0 body{font-size:13px}.arale-widget-1_0_0 .content li{background-color:#fff}}@keyframes foo{0%{top:0;left:0}100%{top:100px;left:100%}}:root .arale-widget-1_0_0 .content{font-size:12px}', "arale/widget/1.0.0/a.css"); + }; }); \ No newline at end of file