diff --git a/index.js b/index.js index f7df93b..421b2ce 100644 --- a/index.js +++ b/index.js @@ -1,30 +1,44 @@ +var utils = require('loader-utils'); +var assign = require('object-assign'); // using: regex, capture groups, and capture group variables. var templateUrlRegex = /templateUrl *:(.*)$/gm; var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g; var stringRegex = /(['"])((?:[^\\]\\\1|.)*?)\1/g; -function replaceStringsWithRequires(string) { +function replaceStringsWithRequires(string, config) { + // Defaults type assertion inclusion to false for now + var typeAssertion = config.hasOwnProperty('typeAssertion') ? config.typeAssertion : false; + return string.replace(stringRegex, function (match, quote, url) { if (url.charAt(0) !== ".") { url = "./" + url; } - return "require('" + url + "')"; + return (typeAssertion ? "" : "") + "require('" + url + "')"; }); } + module.exports = function(source, sourcemap) { // Not cacheable during unit tests; this.cacheable && this.cacheable(); + // getLoaderConfig expects options to be always set + if(!this.options){ + this.options = {} + } + + // Reads config + var config = utils.getLoaderConfig(this, 'angular2Template'); + var newSource = source.replace(templateUrlRegex, function (match, url) { // replace: templateUrl: './path/to/template.html' // with: template: require('./path/to/template.html') - return "template:" + replaceStringsWithRequires(url); + return "template:" + replaceStringsWithRequires(url, config); }) .replace(stylesRegex, function (match, urls) { // replace: stylesUrl: ['./foo.css', "./baz.css", "./index.component.css"] // with: styles: [require('./foo.css'), require("./baz.css"), require("./index.component.css")] - return "styles:" + replaceStringsWithRequires(urls); + return "styles:" + replaceStringsWithRequires(urls, config); }); // Support for tests diff --git a/package.json b/package.json index d187033..0de8ee6 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "should": "^9.0.0" }, "dependencies": { - "loader-utils": "^0.2.15" + "loader-utils": "^0.2.15", + "object-assign": "^4.1.0" } } diff --git a/test/loader.spec.js b/test/loader.spec.js index 4f21849..c339af6 100644 --- a/test/loader.spec.js +++ b/test/loader.spec.js @@ -114,4 +114,119 @@ describe("loader", function() { }); + it("Should convert html and style file strings adding type assertion when typeAssertion option is true", function() { + var ctx = { + options: { + angular2Template : { + typeAssertion : true + } + } + }; + loader.call(ctx, fixtures.componentWithSpacing) + .should + .be + .eql(` + import {Component} from '@angular/core'; + + @Component({ + selector : 'test-component', + template: require('./some/path/to/file.html'), + styles: [require('./app/css/styles.css')] + }) + export class TestComponent {} +`) + + }); + + it("Should convert html and style file strings adding type assertion when typeAssertion query param is true", function() { + var ctx = { + query: '?typeAssertion=true' + }; + loader.call(ctx, fixtures.componentWithSpacing) + .should + .be + .eql(` + import {Component} from '@angular/core'; + + @Component({ + selector : 'test-component', + template: require('./some/path/to/file.html'), + styles: [require('./app/css/styles.css')] + }) + export class TestComponent {} +`) + + }); + + it("Should convert html and style file strings adding type assertion and query should take precedence", function() { + var ctx = { + query: '?typeAssertion=true', + options: { + angular2Template : { + typeAssertion : false + } + } + }; + loader.call(ctx, fixtures.componentWithSpacing) + .should + .be + .eql(` + import {Component} from '@angular/core'; + + @Component({ + selector : 'test-component', + template: require('./some/path/to/file.html'), + styles: [require('./app/css/styles.css')] + }) + export class TestComponent {} +`) + + }); + + + it("Should convert html and style file strings NOT adding type assertion when typeAssertion option is false", function() { + var ctx = { + options: { + angular2Template : { + typeAssertion : false + } + } + }; + loader.call(ctx, fixtures.componentWithSpacing) + .should + .be + .eql(` + import {Component} from '@angular/core'; + + @Component({ + selector : 'test-component', + template: require('./some/path/to/file.html'), + styles: [require('./app/css/styles.css')] + }) + export class TestComponent {} +`) + + }); + + it("Should convert html and style file strings NOT adding type assertion when typeAssertion query param is false", function() { + var ctx = { + query: '?typeAssertion=false' + }; + loader.call(ctx, fixtures.componentWithSpacing) + .should + .be + .eql(` + import {Component} from '@angular/core'; + + @Component({ + selector : 'test-component', + template: require('./some/path/to/file.html'), + styles: [require('./app/css/styles.css')] + }) + export class TestComponent {} +`) + + }); + + });