From f3de85bfc54e2467e2c161a005f98975d83b4308 Mon Sep 17 00:00:00 2001 From: "sergey.sokolov" Date: Tue, 19 Sep 2017 15:56:53 +0300 Subject: [PATCH] Added option to turn off conversion to relative path --- README.md | 1 + index.js | 8 ++++---- test/loader.spec.js | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 59ff731..d4c79f6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.js b/index.js index 587cf75..b081af7 100644 --- a/index.js +++ b/index.js @@ -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 + "')"; @@ -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 diff --git a/test/loader.spec.js b/test/loader.spec.js index ea730ac..c7d2928 100644 --- a/test/loader.spec.js +++ b/test/loader.spec.js @@ -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)