From ee1ae56b22ac144f44de513cf5c1d277159ae523 Mon Sep 17 00:00:00 2001 From: julianosam Date: Sun, 30 Oct 2016 20:07:59 -0400 Subject: [PATCH 1/4] Adding type assertion option to fix issues with typescript typechecks. --- index.js | 30 ++++++++++-- test/loader.spec.js | 115 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index f7df93b..51e90ad 100644 --- a/index.js +++ b/index.js @@ -1,30 +1,52 @@ +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 + "')"; }); } +/** + * Loads loader config. Reads config from both query string + * and new webpack2 loader options. Query takes precedence. + */ +function getConfig(ctx) { + + var query = ctx.query ? utils.parseQuery(ctx.query) : {}; + var options = ctx.options ? ctx.options['angular2Template'] : {}; + + delete query.config; + + return assign({}, options, query); +} + module.exports = function(source, sourcemap) { // Not cacheable during unit tests; this.cacheable && this.cacheable(); + // Reads config + var config = getConfig(this); + 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/test/loader.spec.js b/test/loader.spec.js index 4f21849..463429b 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 assetion when typeAssetion 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 assetion when typeAssetion 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 assetion 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 assetion when typeAssetion 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 assetion when typeAssetion 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 {} +`) + + }); + + }); From 98712b66555a19b95d7c7f2aee42d32a4e380903 Mon Sep 17 00:00:00 2001 From: julianosam Date: Sun, 30 Oct 2016 20:23:20 -0400 Subject: [PATCH 2/4] Declaring object-assign dependency. --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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" } } From d1d88cea2763b7dc8c4c769471dc66488faafbac Mon Sep 17 00:00:00 2001 From: julianosam Date: Sun, 30 Oct 2016 20:31:21 -0400 Subject: [PATCH 3/4] Fixing typos --- test/loader.spec.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/loader.spec.js b/test/loader.spec.js index 463429b..c339af6 100644 --- a/test/loader.spec.js +++ b/test/loader.spec.js @@ -114,7 +114,7 @@ describe("loader", function() { }); - it("Should convert html and style file strings adding type assetion when typeAssetion option is true", function() { + it("Should convert html and style file strings adding type assertion when typeAssertion option is true", function() { var ctx = { options: { angular2Template : { @@ -138,7 +138,7 @@ describe("loader", function() { }); - it("Should convert html and style file strings adding type assetion when typeAssetion query param is true", function() { + it("Should convert html and style file strings adding type assertion when typeAssertion query param is true", function() { var ctx = { query: '?typeAssertion=true' }; @@ -158,7 +158,7 @@ describe("loader", function() { }); - it("Should convert html and style file strings adding type assetion and query should take precedence", function() { + it("Should convert html and style file strings adding type assertion and query should take precedence", function() { var ctx = { query: '?typeAssertion=true', options: { @@ -184,7 +184,7 @@ describe("loader", function() { }); - it("Should convert html and style file strings NOT adding type assetion when typeAssetion option is false", function() { + it("Should convert html and style file strings NOT adding type assertion when typeAssertion option is false", function() { var ctx = { options: { angular2Template : { @@ -208,7 +208,7 @@ describe("loader", function() { }); - it("Should convert html and style file strings NOT adding type assetion when typeAssetion query param is false", function() { + it("Should convert html and style file strings NOT adding type assertion when typeAssertion query param is false", function() { var ctx = { query: '?typeAssertion=false' }; From 866918e6b041cc75e32ad2333b42386ffd7c5464 Mon Sep 17 00:00:00 2001 From: julianosam Date: Mon, 31 Oct 2016 00:03:53 -0400 Subject: [PATCH 4/4] Using getLoaderConfig() from loader-utils --- index.js | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 51e90ad..421b2ce 100644 --- a/index.js +++ b/index.js @@ -17,26 +17,18 @@ function replaceStringsWithRequires(string, config) { }); } -/** - * Loads loader config. Reads config from both query string - * and new webpack2 loader options. Query takes precedence. - */ -function getConfig(ctx) { - - var query = ctx.query ? utils.parseQuery(ctx.query) : {}; - var options = ctx.options ? ctx.options['angular2Template'] : {}; - - delete query.config; - - return assign({}, options, query); -} 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 = getConfig(this); + var config = utils.getLoaderConfig(this, 'angular2Template'); var newSource = source.replace(templateUrlRegex, function (match, url) { // replace: templateUrl: './path/to/template.html'