forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlazy-module.ts
More file actions
88 lines (84 loc) · 3.58 KB
/
lazy-module.ts
File metadata and controls
88 lines (84 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import {readdirSync} from 'fs';
import {ng, silentNpm} from '../../utils/process';
import {appendToFile, writeFile, prependToFile, replaceInFile} from '../../utils/fs';
export default function() {
let oldNumberOfFiles = 0;
return Promise.resolve()
.then(() => ng('build'))
.then(() => oldNumberOfFiles = readdirSync('dist').length)
.then(() => ng('generate', 'module', 'lazy', '--routing'))
.then(() => ng('generate', 'module', 'too/lazy', '--routing'))
.then(() => prependToFile('src/app/app.module.ts', `
import { RouterModule } from '@angular/router';
`))
.then(() => replaceInFile('src/app/app.module.ts', 'imports: [', `imports: [
RouterModule.forRoot([{ path: "lazy", loadChildren: "src/app/lazy/lazy.module#LazyModule" }]),
RouterModule.forRoot([{ path: "lazy1", loadChildren: "./lazy/lazy.module#LazyModule" }]),
RouterModule.forRoot([{ path: "lazy2", loadChildren: "./too/lazy/lazy.module#LazyModule" }]),
`))
.then(() => ng('build', '--named-chunks'))
.then(() => readdirSync('dist/test-project'))
.then((distFiles) => {
const currentNumberOfDistFiles = distFiles.length;
if (oldNumberOfFiles >= currentNumberOfDistFiles) {
throw new Error('A bundle for the lazy module was not created.');
}
oldNumberOfFiles = currentNumberOfDistFiles;
if (!distFiles.includes('lazy-lazy-module.js')) {
throw new Error('The lazy module chunk did not have a name.');
}
if (!distFiles.includes('too-lazy-lazy-module.js')) {
throw new Error('The lazy module chunk did not use a unique name.');
}
})
// verify System.import still works
.then(() => writeFile('src/app/lazy-file.ts', ''))
.then(() => appendToFile('src/app/app.component.ts', `
// verify other System.import still work
declare var System: any;
const lazyFile = 'file';
System.import(/*webpackChunkName: '[request]'*/'./lazy-' + lazyFile);
`))
.then(() => ng('build', '--named-chunks'))
.then(() => readdirSync('dist/test-project'))
.then((distFiles) => {
const currentNumberOfDistFiles = distFiles.length;
if (oldNumberOfFiles >= currentNumberOfDistFiles) {
throw new Error('A bundle for the lazy file was not created.');
}
if (!distFiles.includes('lazy-file.js')) {
throw new Error('The lazy file chunk did not have a name.');
}
oldNumberOfFiles = currentNumberOfDistFiles;
})
// verify 'import *' syntax doesn't break lazy modules
.then(() => silentNpm('install', 'moment'))
.then(() => appendToFile('src/app/app.component.ts', `
import * as moment from 'moment';
console.log(moment);
`))
.then(() => ng('build'))
.then(() => readdirSync('dist/test-project').length)
.then(currentNumberOfDistFiles => {
if (oldNumberOfFiles != currentNumberOfDistFiles) {
throw new Error('Bundles were not created after adding \'import *\'.');
}
})
.then(() => ng('build', '--no-named-chunks'))
.then(() => readdirSync('dist/test-project'))
.then((distFiles) => {
if (distFiles.includes('lazy-lazy-module.js')
|| distFiles.includes('too-lazy-lazy-module.js')
) {
throw new Error('Lazy chunks shouldn\'t have a name but did.');
}
})
// Check for AoT and lazy routes.
.then(() => ng('build', '--aot'))
.then(() => readdirSync('dist/test-project').length)
.then(currentNumberOfDistFiles => {
if (oldNumberOfFiles != currentNumberOfDistFiles) {
throw new Error('AoT build contains a different number of files.');
}
});
}