From dd64d81a33cdff1060a0fc86daff2b9af55fc11b Mon Sep 17 00:00:00 2001 From: Maxim Tolochko Date: Tue, 6 Dec 2016 00:13:13 +0300 Subject: [PATCH] Fix templateUrl regexp for single line component decorator --- index.js | 2 +- .../component_with_single_line_decorator.js | 8 ++++++++ test/fixtures/index.js | 2 ++ test/loader.spec.js | 19 +++++++++++++++++-- 4 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/component_with_single_line_decorator.js diff --git a/index.js b/index.js index d6ce46a..b708d29 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ var loaderUtils = require("loader-utils"); // using: regex, capture groups, and capture group variables. -var templateUrlRegex = /templateUrl *:(.*)$/gm; +var templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]([,}\n]))/gm; var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g; var stringRegex = /(['"])((?:[^\\]\\\1|.)*?)\1/g; diff --git a/test/fixtures/component_with_single_line_decorator.js b/test/fixtures/component_with_single_line_decorator.js new file mode 100644 index 0000000..90ba7c3 --- /dev/null +++ b/test/fixtures/component_with_single_line_decorator.js @@ -0,0 +1,8 @@ +var componentWithSingleLineDecorator = ` + import {Component} from '@angular/core'; + + @Component({selector: 'test-component', templateUrl: './file.html', styleUrls: ['./styles.css']}) + export class TestComponent {}; +`; + +module.exports = componentWithSingleLineDecorator; diff --git a/test/fixtures/index.js b/test/fixtures/index.js index fba40d3..b3f17a2 100644 --- a/test/fixtures/index.js +++ b/test/fixtures/index.js @@ -3,9 +3,11 @@ var componentWithQuoteInUrls = require('./component_with_quote_in_urls.js'); var componentWithMultipleStyles = require('./component_with_multiple_styles.js'); var componentWithoutRelPeriodSlash = require('./component_without_relative_period_slash.js'); var componentWithSpacing = require('./component_with_spacing.js'); +var componentWithSingleLineDecorator = require('./component_with_single_line_decorator.js'); exports.simpleAngular2TestComponentFileStringSimple = sampleAngular2ComponentSimpleFixture; exports.componentWithQuoteInUrls = componentWithQuoteInUrls; exports.componentWithMultipleStyles = componentWithMultipleStyles; exports.componentWithoutRelPeriodSlash = componentWithoutRelPeriodSlash; exports.componentWithSpacing = componentWithSpacing; +exports.componentWithSingleLineDecorator = componentWithSingleLineDecorator; diff --git a/test/loader.spec.js b/test/loader.spec.js index 729de9d..7e9cc09 100644 --- a/test/loader.spec.js +++ b/test/loader.spec.js @@ -72,10 +72,10 @@ describe("loader", function() { }); it("Should convert partial string match requires", function() { - loader.call({}, `templateUrl: './index/app.html'`) + loader.call({}, `{templateUrl: './index/app.html'}`) .should .be - .eql(`template: require('./index/app.html')`); + .eql(`{template: require('./index/app.html')}`); }); it("Should handle the absense of proper relative path notation", function() { @@ -160,4 +160,19 @@ describe("loader", function() { }); + it("Should convert html and style file strings to require()s in a single line component decorator", function() { + + loader.call({}, fixtures.componentWithSingleLineDecorator) + .should + .be + .eql(` + import {Component} from '@angular/core'; + + @Component({selector: 'test-component', template: require('./file.html'), styles: [require('./styles.css')]}) + export class TestComponent {}; +` + ); + + }); + });