From cf3c0b000bed1b56222a6b801a96bb9d5dd7edff Mon Sep 17 00:00:00 2001 From: emaphp Date: Wed, 9 Mar 2016 16:49:06 -0300 Subject: [PATCH] added: support for the 'parseDynamicRoutes' argument --- README.md | 35 ++++++++++++++++++- index.js | 10 ++++-- lib/attributeParser.js | 15 ++++---- package.json | 2 +- test/loaderTest.js | 12 +++++++ ...mic-attribute-with-parseDynamicRoutes.html | 2 ++ ...amic-attribute-with-parseDynamicRoutes.txt | 11 ++++++ .../output/dynamic-attribute-with-root.txt | 2 +- 8 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 test/templates/dynamic-attribute-with-parseDynamicRoutes.html create mode 100644 test/templates/output/dynamic-attribute-with-parseDynamicRoutes.txt diff --git a/README.md b/README.md index 0c97631..2712ea6 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ An Underscore.js and Lodash template loader for Webpack ### Changelog
- * 0.7.1: FIX: Check if attribute contains a template expression before replacing it. + * 0.7.2: Support for the `parseDynamicRoutes` argument (deactivated by default). ### Installation @@ -229,6 +229,39 @@ module.exports = { }; ``` +Dynamic attributes won't be afected by this behaviour by default. + +```html + + +``` + +In order to append the root directory you'll need to specify the `parseDynamicRoutes` argument. + +```javascript +module.exports = { + //... + + module: { + loaders: [ + { + test: /\.html$/, + loader: "underscore-template-loader", + query: { + root: "myapp", + parseDynamicRoutes: true + } + } + ] + } +}; +``` + +```html + + +``` + ### Macros Macros allow additional features like including templates or inserting custom text in compiled templates. diff --git a/index.js b/index.js index 0bda291..d624d60 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,8 @@ module.exports = function(content) { parseMacros = true, engine = false, withImports = false, - attributes = ['img:src']; + attributes = ['img:src'], + parseDynamicRoutes = false; // Parse arguments var query = this.query instanceof Object ? this.query : loaderUtils.parseQuery(this.query); @@ -66,6 +67,11 @@ module.exports = function(content) { var filenameRelative = path.relative(query.prependFilenameComment, this.resource); content = "\n\n" + content; } + + // Check if dynamic routes must be parsed + if (query.parseDynamicRoutes !== undefined) { + parseDynamicRoutes = !!query.parseDynamicRoutes; + } } // Include additional macros @@ -84,7 +90,7 @@ module.exports = function(content) { // Parse attributes var attributesContext = attributeParser(content, function (tag, attr) { return attributes.indexOf(tag + ':' + attr) != -1; - }, 'ATTRIBUTE', root); + }, 'ATTRIBUTE', root, parseDynamicRoutes); content = attributesContext.replaceMatches(content); // Compile template diff --git a/lib/attributeParser.js b/lib/attributeParser.js index c805597..63dcce3 100644 --- a/lib/attributeParser.js +++ b/lib/attributeParser.js @@ -37,7 +37,7 @@ var isTemplate = function (content) { }; // AttributeContext class -var AttributeContext = function (isRelevantTagAttr, usid, root) { +var AttributeContext = function (isRelevantTagAttr, usid, root, parseDynamicRoutes) { this.currentDirective = null; this.matches = []; this.isRelevantTagAttr = isRelevantTagAttr; @@ -47,6 +47,7 @@ var AttributeContext = function (isRelevantTagAttr, usid, root) { }; this.data = {}; this.root = root; + this.parseDynamicRoutes = parseDynamicRoutes; }; AttributeContext.prototype.replaceMatches = function(content) { @@ -56,8 +57,10 @@ AttributeContext.prototype.replaceMatches = function(content) { this.matches.forEach(function (match) { if (isTemplate(match.value)) { - // Replate template if a "root" option has been defined - if (pathIsAbsolute(match.value) && self.root != undefined) { + // Replace attribute value + // This is used if it contains a template expression and both the "root" and "parseDynamicRoutes" + // were defined + if (pathIsAbsolute(match.value) && self.root !== undefined) { var x = content.pop(); content.push(x.substr(match.start + match.length)); content.push(match.expression); @@ -126,7 +129,7 @@ var processMatch = function (match, strUntilValue, name, value, index) { // Try and set "root" directory when a dynamic attribute is found if (isTemplate(value)) { - if (pathIsAbsolute(value) && self.root != undefined) { + if (pathIsAbsolute(value) && self.root !== undefined && self.parseDynamicRoutes) { // Generate new value for replacement expression = loaderUtils.urlToRequest(value, self.root); } @@ -164,7 +167,7 @@ var specs = { var parser = new Parser(specs); -module.exports = function parse(html, isRelevantTagAttr, usid, root) { - var context = new AttributeContext(isRelevantTagAttr, usid, root); +module.exports = function parse(html, isRelevantTagAttr, usid, root, parseDynamicRoutes) { + var context = new AttributeContext(isRelevantTagAttr, usid, root, parseDynamicRoutes); return parser.parse('outside', html, context); }; diff --git a/package.json b/package.json index af328be..640a508 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "underscore-template-loader", - "version": "0.7.1", + "version": "0.7.2", "description": "An Underscore and Lodash template loader for Webpack", "main": "index.js", "homepage": "https://github.com/emaphp/underscore-template-loader", diff --git a/test/loaderTest.js b/test/loaderTest.js index 7890556..7766780 100644 --- a/test/loaderTest.js +++ b/test/loaderTest.js @@ -155,6 +155,18 @@ describe('loader', function () { done(); }); }); + + it('should parse dynamic attributes with parseDynamicRoutes', function (done) { + testTemplate(loader, 'dynamic-attribute-with-parseDynamicRoutes.html', { + query: { + root: 'myapp', + parseDynamicRoutes: true + } + }, function (output) { + assert.equal(output, loadOutput('dynamic-attribute-with-parseDynamicRoutes.txt')); + done(); + }); + }); // FIXME: Changing the underscore tags changes it globally it('should allow custom underscore tags', function (done) { diff --git a/test/templates/dynamic-attribute-with-parseDynamicRoutes.html b/test/templates/dynamic-attribute-with-parseDynamicRoutes.html new file mode 100644 index 0000000..2d10236 --- /dev/null +++ b/test/templates/dynamic-attribute-with-parseDynamicRoutes.html @@ -0,0 +1,2 @@ + + diff --git a/test/templates/output/dynamic-attribute-with-parseDynamicRoutes.txt b/test/templates/output/dynamic-attribute-with-parseDynamicRoutes.txt new file mode 100644 index 0000000..2ba81e6 --- /dev/null +++ b/test/templates/output/dynamic-attribute-with-parseDynamicRoutes.txt @@ -0,0 +1,11 @@ +module.exports = function(obj){ +var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');}; +with(obj||{}){ +__p+='\n\n'; +} +return __p; +}; diff --git a/test/templates/output/dynamic-attribute-with-root.txt b/test/templates/output/dynamic-attribute-with-root.txt index 36e4cc3..8c23770 100644 --- a/test/templates/output/dynamic-attribute-with-root.txt +++ b/test/templates/output/dynamic-attribute-with-root.txt @@ -3,7 +3,7 @@ var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments with(obj||{}){ __p+='\n\n\n'; }