Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 <string> 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 ? "<string>" : "") + "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
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"should": "^9.0.0"
},
"dependencies": {
"loader-utils": "^0.2.15"
"loader-utils": "^0.2.15",
"object-assign": "^4.1.0"
}
}
115 changes: 115 additions & 0 deletions test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: <string>require('./some/path/to/file.html'),
styles: [<string>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: <string>require('./some/path/to/file.html'),
styles: [<string>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: <string>require('./some/path/to/file.html'),
styles: [<string>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 {}
`)

});


});