Skip to content

Commit 0049bbf

Browse files
0.5.0 (#48)
* feat(loader): Added support for nested lazy loading in AOT * refactor: resolve all scenarios of AOT relative module paths Closes #45 * 0.5.0
1 parent b96316c commit 0049bbf

File tree

5 files changed

+76
-14
lines changed

5 files changed

+76
-14
lines changed

CHANGELOG.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
<a name="0.4.0"></a>
2-
# [0.4.0](https://github.com/brandonroberts/angular-router-loader/compare/v0.3.4...v0.4.0) (2016-12-05)
1+
<a name="0.5.0"></a>
2+
# [0.5.0](https://github.com/brandonroberts/angular-router-loader/compare/v0.3.4...v0.5.0) (2017-01-07)
3+
4+
5+
### Bug Fixes
6+
7+
* **docs:** Fix colons in readme.md ([#36](https://github.com/brandonroberts/angular-router-loader/issues/36)) ([58db6de](https://github.com/brandonroberts/angular-router-loader/commit/58db6de))
8+
* **loader:** Prefer the query 'debug' parameter to the global value ([#37](https://github.com/brandonroberts/angular-router-loader/issues/37)) ([b96316c](https://github.com/brandonroberts/angular-router-loader/commit/b96316c))
9+
10+
11+
### Features
12+
13+
* **loader:** Added support for nested lazy loading in AOT ([a2fb4d6](https://github.com/brandonroberts/angular-router-loader/commit/a2fb4d6))
314

415

516

docs/options.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ tsconfig.json (Angular Compiler Options)
7474

7575
```json
7676
"angularCompilerOptions": {
77-
"genDir": "src/compiled",
77+
"genDir": "compiled",
7878
"skipMetadataEmit" : true
7979
}
8080
```
@@ -86,7 +86,7 @@ loaders: [
8686
test: /\.ts$/,
8787
loaders: [
8888
'awesome-typescript-loader',
89-
'angular-router-loader?aot=true&genDir=src/compiled/src/app'
89+
'angular-router-loader?aot=true&genDir=compiled'
9090
]
9191
}
9292
]

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-router-loader",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "A webpack loader for Angular that enables string-based module loading with the Angular Router",
55
"main": "src/index.js",
66
"scripts": {

spec/index.spec.js

+30-5
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ describe('Loader', function() {
228228
var result = [
229229
'loadChildren: () => new Promise(function (resolve) {',
230230
' (require as any).ensure([], function (require: any) {',
231-
' resolve(require(\'../../path/to/file.module.ngfactory\')[\'FileModuleNgFactory\']);',
231+
' resolve(require(\'./path/to/file.module.ngfactory\')[\'FileModuleNgFactory\']);',
232232
' });',
233233
'})'
234234
];
@@ -244,7 +244,7 @@ describe('Loader', function() {
244244
it('should return a loadChildren sync require statement', function() {
245245
var result = [
246246
'loadChildren: function() {',
247-
' return require(\'../../path/to/file.module.ngfactory\')[\'FileModuleNgFactory\'];',
247+
' return require(\'./path/to/file.module.ngfactory\')[\'FileModuleNgFactory\'];',
248248
'}'
249249
];
250250

@@ -258,7 +258,7 @@ describe('Loader', function() {
258258

259259
it ('should return a loadChildren System.import statement', function() {
260260
var result = [
261-
'loadChildren: () => System.import(\'../../path/to/file.module.ngfactory\')',
261+
'loadChildren: () => System.import(\'./path/to/file.module.ngfactory\')',
262262
' .then(function(module) {',
263263
' return module[\'FileModuleNgFactory\'];',
264264
' })'
@@ -278,7 +278,7 @@ describe('Loader', function() {
278278
var result = [
279279
'loadChildren: () => new Promise(function (resolve) {',
280280
' (require as any).ensure([], function (require: any) {',
281-
' resolve(require(\'../../path/to/file.module' + moduleSuffix + '\')[\'FileModuleNgFactory\']);',
281+
' resolve(require(\'./path/to/file.module' + moduleSuffix + '\')[\'FileModuleNgFactory\']);',
282282
' });',
283283
'})'
284284
];
@@ -297,7 +297,7 @@ describe('Loader', function() {
297297
var result = [
298298
'loadChildren: () => new Promise(function (resolve) {',
299299
' (require as any).ensure([], function (require: any) {',
300-
' resolve(require(\'../../path/to/file.module.ngfactory\')[\'FileModule' + factorySuffix + '\']);',
300+
' resolve(require(\'./path/to/file.module.ngfactory\')[\'FileModule' + factorySuffix + '\']);',
301301
' });',
302302
'})'
303303
];
@@ -328,4 +328,29 @@ describe('Loader', function() {
328328
});
329329
});
330330

331+
describe('AoT + genDir', function() {
332+
var resourcePath = 'src/app/my-module/my-module.routes.ts';
333+
var modulePath = '../groups/inventory/index#InventoryModule';
334+
335+
beforeEach(function () {
336+
query = '?aot=true&genDir=compiled'
337+
});
338+
339+
it('should return a loadChildren async require statement', function() {
340+
var result = [
341+
'loadChildren: () => new Promise(function (resolve) {',
342+
' (require as any).ensure([], function (require: any) {',
343+
' resolve(require(\'../../../compiled/src/app/groups/inventory/index.ngfactory\')[\'InventoryModuleNgFactory\']);',
344+
' });',
345+
'})'
346+
];
347+
348+
var loadedString = loader.call({
349+
resourcePath: resourcePath,
350+
query: query
351+
}, `loadChildren: '${modulePath}'`);
352+
353+
checkResult(loadedString, result);
354+
});
355+
});
331356
});

src/index.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = function(source, sourcemap) {
2020
var genDir = query.genDir || '';
2121
var inline = query.inline || true;
2222
var debug = (typeof query.debug !== 'boolean' ? this.debug : query.debug);
23+
var baseDir = query.baseDir || process.cwd();
2324

2425
// get the filename path
2526
var resourcePath = this.resourcePath;
@@ -48,11 +49,36 @@ module.exports = function(source, sourcemap) {
4849

4950
// update the file path for non-ngfactory files
5051
if (aot && filename.substr(-9) !== moduleSuffix.substr(-9) && isRelativePath) {
51-
// find the relative dir to file from the genDir
52-
var relativeDir = path.relative(path.dirname(resourcePath), path.resolve(genDir));
52+
// the full path of the directory of the current resource
53+
var currentDir = path.dirname(resourcePath);
5354

54-
// build the relative path from the genDir
55-
filePath = path.join(relativeDir, filePath);
55+
// the absolute path of our destenation NgModule module.
56+
var absoluteNgModulePath = path.resolve(currentDir, filePath);
57+
58+
/*
59+
* If "genDir" is empty the compiler emits to the source tree, next to the original component source code.
60+
* absoluteNgModulePath points to there so we're good.
61+
*
62+
* If "genDir" exist need to map the path based on "genDir"
63+
*/
64+
if (genDir && genDir !== '.') {
65+
66+
/*
67+
"genDir" is tricky.
68+
The path used for "genDir" is resolved relative to the "tsconfig.json" file used to execute ngc.
69+
This out of the context of webpack so we can't figure this out automatically.
70+
The user needs to set a "genDir" relative to the root of the project which should resolve to the same absolute path ngc resolves for "genDir".
71+
72+
If "tsconfig.json" is in the root of the project it's identical.
73+
*/
74+
75+
var relativeNgModulePath = path.relative(baseDir, absoluteNgModulePath);
76+
absoluteNgModulePath = path.join(path.resolve(baseDir, genDir), relativeNgModulePath);
77+
}
78+
79+
80+
// filePath is an absolute path, we need the relative filePath:
81+
filePath = path.relative(currentDir, absoluteNgModulePath);
5682
}
5783

5884
filePath = utils.normalizeFilePath(filePath, isRelativePath);

0 commit comments

Comments
 (0)