Skip to content

Commit

Permalink
0.5.0 (#48)
Browse files Browse the repository at this point in the history
* feat(loader): Added support for nested lazy loading in AOT

* refactor: resolve all scenarios of AOT relative module paths

Closes #45

* 0.5.0
  • Loading branch information
brandonroberts authored Jan 7, 2017
1 parent b96316c commit 0049bbf
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 14 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<a name="0.4.0"></a>
# [0.4.0](https://github.com/brandonroberts/angular-router-loader/compare/v0.3.4...v0.4.0) (2016-12-05)
<a name="0.5.0"></a>
# [0.5.0](https://github.com/brandonroberts/angular-router-loader/compare/v0.3.4...v0.5.0) (2017-01-07)


### Bug Fixes

* **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))
* **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))


### Features

* **loader:** Added support for nested lazy loading in AOT ([a2fb4d6](https://github.com/brandonroberts/angular-router-loader/commit/a2fb4d6))



Expand Down
4 changes: 2 additions & 2 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ tsconfig.json (Angular Compiler Options)

```json
"angularCompilerOptions": {
"genDir": "src/compiled",
"genDir": "compiled",
"skipMetadataEmit" : true
}
```
Expand All @@ -86,7 +86,7 @@ loaders: [
test: /\.ts$/,
loaders: [
'awesome-typescript-loader',
'angular-router-loader?aot=true&genDir=src/compiled/src/app'
'angular-router-loader?aot=true&genDir=compiled'
]
}
]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-router-loader",
"version": "0.4.0",
"version": "0.5.0",
"description": "A webpack loader for Angular that enables string-based module loading with the Angular Router",
"main": "src/index.js",
"scripts": {
Expand Down
35 changes: 30 additions & 5 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ describe('Loader', function() {
var result = [
'loadChildren: () => new Promise(function (resolve) {',
' (require as any).ensure([], function (require: any) {',
' resolve(require(\'../../path/to/file.module.ngfactory\')[\'FileModuleNgFactory\']);',
' resolve(require(\'./path/to/file.module.ngfactory\')[\'FileModuleNgFactory\']);',
' });',
'})'
];
Expand All @@ -244,7 +244,7 @@ describe('Loader', function() {
it('should return a loadChildren sync require statement', function() {
var result = [
'loadChildren: function() {',
' return require(\'../../path/to/file.module.ngfactory\')[\'FileModuleNgFactory\'];',
' return require(\'./path/to/file.module.ngfactory\')[\'FileModuleNgFactory\'];',
'}'
];

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

it ('should return a loadChildren System.import statement', function() {
var result = [
'loadChildren: () => System.import(\'../../path/to/file.module.ngfactory\')',
'loadChildren: () => System.import(\'./path/to/file.module.ngfactory\')',
' .then(function(module) {',
' return module[\'FileModuleNgFactory\'];',
' })'
Expand All @@ -278,7 +278,7 @@ describe('Loader', function() {
var result = [
'loadChildren: () => new Promise(function (resolve) {',
' (require as any).ensure([], function (require: any) {',
' resolve(require(\'../../path/to/file.module' + moduleSuffix + '\')[\'FileModuleNgFactory\']);',
' resolve(require(\'./path/to/file.module' + moduleSuffix + '\')[\'FileModuleNgFactory\']);',
' });',
'})'
];
Expand All @@ -297,7 +297,7 @@ describe('Loader', function() {
var result = [
'loadChildren: () => new Promise(function (resolve) {',
' (require as any).ensure([], function (require: any) {',
' resolve(require(\'../../path/to/file.module.ngfactory\')[\'FileModule' + factorySuffix + '\']);',
' resolve(require(\'./path/to/file.module.ngfactory\')[\'FileModule' + factorySuffix + '\']);',
' });',
'})'
];
Expand Down Expand Up @@ -328,4 +328,29 @@ describe('Loader', function() {
});
});

describe('AoT + genDir', function() {
var resourcePath = 'src/app/my-module/my-module.routes.ts';
var modulePath = '../groups/inventory/index#InventoryModule';

beforeEach(function () {
query = '?aot=true&genDir=compiled'
});

it('should return a loadChildren async require statement', function() {
var result = [
'loadChildren: () => new Promise(function (resolve) {',
' (require as any).ensure([], function (require: any) {',
' resolve(require(\'../../../compiled/src/app/groups/inventory/index.ngfactory\')[\'InventoryModuleNgFactory\']);',
' });',
'})'
];

var loadedString = loader.call({
resourcePath: resourcePath,
query: query
}, `loadChildren: '${modulePath}'`);

checkResult(loadedString, result);
});
});
});
34 changes: 30 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = function(source, sourcemap) {
var genDir = query.genDir || '';
var inline = query.inline || true;
var debug = (typeof query.debug !== 'boolean' ? this.debug : query.debug);
var baseDir = query.baseDir || process.cwd();

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

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

// build the relative path from the genDir
filePath = path.join(relativeDir, filePath);
// the absolute path of our destenation NgModule module.
var absoluteNgModulePath = path.resolve(currentDir, filePath);

/*
* If "genDir" is empty the compiler emits to the source tree, next to the original component source code.
* absoluteNgModulePath points to there so we're good.
*
* If "genDir" exist need to map the path based on "genDir"
*/
if (genDir && genDir !== '.') {

/*
"genDir" is tricky.
The path used for "genDir" is resolved relative to the "tsconfig.json" file used to execute ngc.
This out of the context of webpack so we can't figure this out automatically.
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".
If "tsconfig.json" is in the root of the project it's identical.
*/

var relativeNgModulePath = path.relative(baseDir, absoluteNgModulePath);
absoluteNgModulePath = path.join(path.resolve(baseDir, genDir), relativeNgModulePath);
}


// filePath is an absolute path, we need the relative filePath:
filePath = path.relative(currentDir, absoluteNgModulePath);
}

filePath = utils.normalizeFilePath(filePath, isRelativePath);
Expand Down

0 comments on commit 0049bbf

Please sign in to comment.