Skip to content

Commit 3be17e7

Browse files
clydinhansl
authored andcommitted
feat(@angular/cli): support additional application lazy modules
1 parent 2a2dc1e commit 3be17e7

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

packages/@angular/cli/lib/config/schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,14 @@
263263
"description": "Name and corresponding file for environment config.",
264264
"type": "object",
265265
"additionalProperties": true
266+
},
267+
"lazyModules": {
268+
"description": "List of additional NgModule files that will be lazy loaded. (lazy router modules with be discovered automatically)",
269+
"type": "array",
270+
"items": {
271+
"type": "string",
272+
"minLength": 1
273+
}
266274
}
267275
},
268276
"additionalProperties": false

packages/@angular/cli/models/webpack-configs/typescript.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any, useMain = tru
8181
}
8282

8383
if (AngularCompilerPlugin.isSupported()) {
84+
const additionalLazyModules: { [module: string]: string } = {};
85+
if (appConfig.lazyModules) {
86+
for (const lazyModule of appConfig.lazyModules) {
87+
additionalLazyModules[lazyModule] = path.resolve(
88+
projectRoot,
89+
appConfig.root,
90+
lazyModule,
91+
);
92+
}
93+
}
94+
8495
const pluginOptions: AngularCompilerPluginOptions = Object.assign({}, {
8596
mainPath: useMain ? path.join(projectRoot, appConfig.root, appConfig.main) : undefined,
8697
i18nInFile: buildOptions.i18nFile,
@@ -92,6 +103,7 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any, useMain = tru
92103
missingTranslation: buildOptions.missingTranslation,
93104
hostReplacementPaths,
94105
sourceMap: buildOptions.sourcemaps,
106+
additionalLazyModules,
95107
}, options);
96108
return new AngularCompilerPlugin(pluginOptions);
97109
} else {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as fs from 'fs';
2+
import { writeFile } from '../../utils/fs';
3+
import { ng } from '../../utils/process';
4+
import { updateJsonFile } from '../../utils/project';
5+
6+
7+
export default async function() {
8+
// Add a lazy module
9+
await ng('generate', 'module', 'lazy');
10+
await updateJsonFile('.angular-cli.json', configJson => {
11+
const app = configJson['apps'][0];
12+
app['lazyModules'] = [
13+
'app/lazy/lazy.module'
14+
];
15+
});
16+
17+
// Update the app component to use the lazy module
18+
await writeFile('src/app/app.component.ts', `
19+
import { Component, SystemJsNgModuleLoader } from '@angular/core';
20+
21+
@Component({
22+
selector: 'app-root',
23+
templateUrl: './app.component.html',
24+
styleUrls: ['./app.component.css'],
25+
})
26+
export class AppComponent {
27+
title = 'app';
28+
constructor(loader: SystemJsNgModuleLoader) {
29+
// Module will be split at build time and loaded when requested below
30+
loader.load('app/lazy/lazy.module#LazyModule')
31+
.then((factory) => { /* Use factory here */ });
32+
}
33+
}
34+
`);
35+
36+
// Build and look for the split lazy module
37+
await ng('build');
38+
for (const file of fs.readdirSync('./dist')) {
39+
if (file === 'lazy.module.chunk.js') {
40+
// Lazy module chunk was found and succesfully split
41+
return;
42+
}
43+
}
44+
45+
throw new Error('Lazy module chunk not created');
46+
}

0 commit comments

Comments
 (0)