|
| 1 | +import {readdirSync} from 'fs'; |
| 2 | +import {oneLine} from 'common-tags'; |
| 3 | + |
| 4 | +import {ng, npm} from '../../utils/process'; |
| 5 | +import {addImportToModule} from '../../utils/ast'; |
| 6 | +import {appendToFile} from '../../utils/fs'; |
| 7 | + |
| 8 | + |
| 9 | +export default function() { |
| 10 | + let oldNumberOfFiles = 0; |
| 11 | + return Promise.resolve() |
| 12 | + .then(() => ng('build')) |
| 13 | + .then(() => oldNumberOfFiles = readdirSync('dist').length) |
| 14 | + .then(() => ng('generate', 'module', 'lazyA', '--routing')) |
| 15 | + .then(() => ng('generate', 'module', 'lazyB', '--routing')) |
| 16 | + .then(() => addImportToModule('src/app/app.module.ts', oneLine` |
| 17 | + RouterModule.forRoot([{ path: "lazyA", loadChildren: "./lazy-a/lazy-a.module#LazyAModule" }]), |
| 18 | + RouterModule.forRoot([{ path: "lazyB", loadChildren: "./lazy-b/lazy-b.module#LazyBModule" }]) |
| 19 | + `, '@angular/router')) |
| 20 | + .then(() => ng('build')) |
| 21 | + .then(() => readdirSync('dist').length) |
| 22 | + .then(currentNumberOfDistFiles => { |
| 23 | + if (oldNumberOfFiles >= currentNumberOfDistFiles) { |
| 24 | + throw new Error('A bundle for the lazy module was not created.'); |
| 25 | + } |
| 26 | + oldNumberOfFiles = currentNumberOfDistFiles; |
| 27 | + }) |
| 28 | + .then(() => npm('install', 'moment')) |
| 29 | + .then(() => appendToFile('src/app/lazy-a/lazy-a.module.ts', ` |
| 30 | + import * as moment from 'moment'; |
| 31 | + console.log(moment); |
| 32 | + `)) |
| 33 | + .then(() => ng('build')) |
| 34 | + .then(() => readdirSync('dist').length) |
| 35 | + .then(currentNumberOfDistFiles => { |
| 36 | + if (oldNumberOfFiles != currentNumberOfDistFiles) { |
| 37 | + throw new Error('The build contains a different number of files.'); |
| 38 | + } |
| 39 | + }) |
| 40 | + .then(() => appendToFile('src/app/lazy-b/lazy-b.module.ts', ` |
| 41 | + import * as moment from 'moment'; |
| 42 | + console.log(moment); |
| 43 | + `)) |
| 44 | + .then(() => ng('build')) |
| 45 | + .then(() => readdirSync('dist').length) |
| 46 | + .then(currentNumberOfDistFiles => { |
| 47 | + if (oldNumberOfFiles >= currentNumberOfDistFiles) { |
| 48 | + throw new Error('A bundle for the common async module was not created.'); |
| 49 | + } |
| 50 | + oldNumberOfFiles = currentNumberOfDistFiles; |
| 51 | + }) |
| 52 | + // Check for AoT and lazy routes. |
| 53 | + .then(() => ng('build', '--aot')) |
| 54 | + .then(() => readdirSync('dist').length) |
| 55 | + .then(currentNumberOfDistFiles => { |
| 56 | + if (oldNumberOfFiles != currentNumberOfDistFiles) { |
| 57 | + throw new Error('AoT build contains a different number of files.'); |
| 58 | + } |
| 59 | + }); |
| 60 | +} |
0 commit comments