Skip to content

Commit 28befa8

Browse files
committedNov 11, 2016
feat(loader): Added support for non-relative paths
1 parent e98322b commit 28befa8

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed
 

‎spec/index.spec.js

+34
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,23 @@ describe('Loader', function() {
178178
env.reset();
179179
});
180180

181+
it('should support non-relative paths', function() {
182+
var result = [
183+
'loadChildren: () => new Promise(function (resolve) {',
184+
' (require as any).ensure([], function (require: any) {',
185+
' resolve(require(\'path/to/file.module\')[\'FileModule\']);',
186+
' });',
187+
'})'
188+
];
189+
190+
var loadedString = loader.call({
191+
resourcePath: resourcePath,
192+
query: ''
193+
}, `loadChildren: '${modulePath.replace('./', '')}'`);
194+
195+
checkResult(loadedString, result);
196+
});
197+
181198
describe('AoT', function() {
182199
beforeEach(function() {
183200
query = '?aot=true&genDir=.'
@@ -268,5 +285,22 @@ describe('Loader', function() {
268285

269286
checkResult(loadedString, result);
270287
});
288+
289+
it('should support non-relative paths', function() {
290+
var result = [
291+
'loadChildren: () => new Promise(function (resolve) {',
292+
' (require as any).ensure([], function (require: any) {',
293+
' resolve(require(\'path/to/file.module.ngfactory\')[\'FileModuleNgFactory\']);',
294+
' });',
295+
'})'
296+
];
297+
298+
var loadedString = loader.call({
299+
resourcePath: resourcePath,
300+
query: query
301+
}, `loadChildren: '${modulePath.replace('./', '')}'`);
302+
303+
checkResult(loadedString, result);
304+
});
271305
});
272306
});

‎spec/utils.spec.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,15 @@ describe('Utils', function() {
6969
});
7070

7171
it('should replace backslashes with forward slashes', function() {
72-
normalizeFilePath('./path').should.eql('.\\\\path');
72+
normalizeFilePath('./path', true).should.eql('.\\\\path');
7373
});
7474

75-
it('should make a relative path if the path is not relative', function() {
76-
normalizeFilePath('path').should.eql('.\\\\path');
75+
it('should make a relative path if the path is not relative and the original path was relative', function() {
76+
normalizeFilePath('path', true).should.eql('.\\\\path');
77+
});
78+
79+
it('should not make a relative path if the original path was not relative', function() {
80+
normalizeFilePath('path', false).should.eql('path');
7781
});
7882

7983
afterEach(function() {
@@ -87,11 +91,15 @@ describe('Utils', function() {
8791
});
8892

8993
it('should not replace backslashes', function() {
90-
normalizeFilePath('./path').should.eql('./path');
94+
normalizeFilePath('./path', true).should.eql('./path');
95+
});
96+
97+
it('should make a relative path if the path is not relative and the original path was relative', function() {
98+
normalizeFilePath('path', true).should.eql('./path');
9199
});
92100

93-
it('should make a relative path if the path is not relative', function() {
94-
normalizeFilePath('path').should.eql('./path');
101+
it('should not make a relative path if the original path was not relative', function() {
102+
normalizeFilePath('path', false).should.eql('path');
95103
});
96104

97105
afterEach(function() {

‎src/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = function(source, sourcemap) {
3131
var hasQuery = queryIndex !== -1;
3232
var loadStringQuery = hasQuery ? loaderUtils.parseQuery(loadString.substr(queryIndex)) : {};
3333
var sync = !!loadStringQuery.sync;
34+
var isRelativePath = loadString.startsWith('.');
3435

3536
// get the module path string
3637
var pathString = hasQuery ? loadString.substr(0, queryIndex) : loadString;
@@ -45,15 +46,15 @@ module.exports = function(source, sourcemap) {
4546
moduleName += (aot ? factorySuffix : '');
4647

4748
// update the file path for non-ngfactory files
48-
if (aot && filename.substr(-9) !== moduleSuffix.substr(-9)) {
49+
if (aot && filename.substr(-9) !== moduleSuffix.substr(-9) && isRelativePath) {
4950
// find the relative dir to file from the genDir
5051
var relativeDir = path.relative(path.dirname(resourcePath), path.resolve(genDir));
5152

5253
// build the relative path from the genDir
5354
filePath = path.join(relativeDir, filePath);
5455
}
5556

56-
filePath = utils.normalizeFilePath(filePath);
57+
filePath = utils.normalizeFilePath(filePath, isRelativePath);
5758

5859
var replacement = match;
5960

‎src/utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ module.exports.getFilename = function(resourcePath) {
4848
return path.basename(resourcePath, path.extname(filename));
4949
};
5050

51-
module.exports.normalizeFilePath = function(filePath) {
51+
module.exports.normalizeFilePath = function(filePath, relativePathMatch) {
5252
var newPath = filePath;
5353

54-
if (!newPath.startsWith('./') && !newPath.startsWith('../')) {
54+
if (relativePathMatch && !newPath.startsWith('./') && !newPath.startsWith('../')) {
5555
newPath = './' + newPath;
5656
}
5757

0 commit comments

Comments
 (0)