diff --git a/index.js b/index.js index 7091cde..0b883ab 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,12 @@ var stylesRegex = /styleUrls:(\s*\[[^\]]*?\])/g; var stringRegex = /(['"])((?:[^\\]\\\1|.)*?)\1/g; function replaceStringsWithRequires(string) { - return string.replace(stringRegex, "require('$2')"); + return string.replace(stringRegex, function (match, quote, url) { + if (url.charAt(0) !== ".") { + url = "./" + url; + } + return "require('" + url + "')"; + }); } module.exports = function(source) { diff --git a/test/fixtures/index.js b/test/fixtures/index.js index 852d6cf..654d8e6 100644 --- a/test/fixtures/index.js +++ b/test/fixtures/index.js @@ -1,8 +1,10 @@ var sampleAngular2ComponentSimpleFixture = require('./sample_angular2_component_file_string_simple.js'); 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'); exports.simpleAngular2TestComponentFileStringSimple = sampleAngular2ComponentSimpleFixture; exports.componentWithQuoteInUrls = componentWithQuoteInUrls; exports.componentWithMultipleStyles = componentWithMultipleStyles; +exports.componentWithoutRelPeriodSlash = componentWithoutRelPeriodSlash; diff --git a/test/loader.spec.js b/test/loader.spec.js index 66b0004..4b83ce7 100644 --- a/test/loader.spec.js +++ b/test/loader.spec.js @@ -78,5 +78,22 @@ describe("loader", function() { .eql(`template: require('./index/app.html')`); }); + it("Should handle the absense of proper relative path notation", function() { + loader.call({}, 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 {} +` + ); + }); + });