Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to turn off conversion to relative path #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class AwesomeButtonComponent { }
### How does it work
The `angular2-template-loader` searches for `templateUrl` and `styleUrls` declarations inside of the Angular 2 Component metadata and replaces the paths with the corresponding `require` statement.
If `keepUrl=true` is added to the loader's query string, `templateUrl` and `styleUrls` will not be replaced by `template` and `style` respectively so you can use a loader like `file-loader`.
By default loader will convert non-relative paths (e.g. `templateUrl: 'file.html'`) to relative by prepending `./` if this behavior is not desired set `keepNonRelative=true`.

The generated `require` statements will be handled by the given loader for `.html` and `.js` files.

Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ var templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*([,}]))/gm;
var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g;
var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;

function replaceStringsWithRequires(string) {
function replaceStringsWithRequires(string, config) {
return string.replace(stringRegex, function (match, quote, url) {
if (url.charAt(0) !== ".") {
if (url.charAt(0) !== "." && !config.keepNonRelative) {
url = "./" + url;
}
return "require('" + url + "')";
Expand Down Expand Up @@ -41,14 +41,14 @@ module.exports = function(source, sourcemap) {
// with: template: require('./path/to/template.html')
// or: templateUrl: require('./path/to/template.html')
// if `keepUrl` query parameter is set to true.
return templateProperty + ":" + replaceStringsWithRequires(url);
return templateProperty + ":" + 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")]
// or: styleUrls: [require('./foo.css'), require("./baz.css"), require("./index.component.css")]
// if `keepUrl` query parameter is set to true.
return styleProperty + ":" + replaceStringsWithRequires(urls);
return styleProperty + ":" + replaceStringsWithRequires(urls, config);
});

// Support for tests
Expand Down
17 changes: 17 additions & 0 deletions test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ describe("loader", function() {
);
});

it("Should not handle the absense of proper relative path notation if configured", function() {
loader.call({query: '?keepNonRelative=true'}, fixtures.componentWithoutRelPeriodSlash)
.should
.be
.eql(`
import {Component} from '@angular/core';

@Component({
selector: 'test-component',
template: require('file.html'),
styles: [require('styles.css')]
})
export class TestComponent {}
`
);
});

it("Should convert html and style file strings to require()s regardless of spacing", function(){

loader.call({}, fixtures.componentWithSpacing)
Expand Down