|
| 1 | +import {oneLine} from 'common-tags'; |
| 2 | +import * as fs from 'fs'; |
| 3 | + |
| 4 | +import {ng} from '../../utils/process'; |
| 5 | +import {writeFile} from '../../utils/fs'; |
| 6 | +import {addImportToModule} from '../../utils/ast'; |
| 7 | + |
| 8 | + |
| 9 | +export default function() { |
| 10 | + const oldHashes: {[module: string]: string} = {}; |
| 11 | + const newHashes: {[module: string]: string} = {}; |
| 12 | + // First, collect the hashes. |
| 13 | + return Promise.resolve() |
| 14 | + .then(() => ng('generate', 'module', 'lazy', '--routing')) |
| 15 | + .then(() => addImportToModule('src/app/app.module.ts', oneLine` |
| 16 | + RouterModule.forRoot([{ path: "lazy", loadChildren: "./lazy/lazy.module#LazyModule" }]) |
| 17 | + `, '@angular/router')) |
| 18 | + .then(() => ng('build', '--prod')) |
| 19 | + .then(() => { |
| 20 | + fs.readdirSync('./dist') |
| 21 | + .forEach(name => { |
| 22 | + if (!name.match(/(main|inline|styles|\d+)\.[a-z0-9]+\.bundle\.(js|css)/)) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + const [module, hash] = name.split('.'); |
| 27 | + oldHashes[module] = hash; |
| 28 | + }); |
| 29 | + }) |
| 30 | + .then(() => writeFile('src/app/app.component.css', 'h1 { margin: 5px; }')) |
| 31 | + .then(() => writeFile('src/styles.css', 'body { background: red; }')) |
| 32 | + .then(() => ng('build', '--prod')) |
| 33 | + .then(() => { |
| 34 | + fs.readdirSync('./dist') |
| 35 | + .forEach(name => { |
| 36 | + if (!name.match(/(main|inline|styles|\d+)\.[a-z0-9]+\.bundle\.(js|css)/)) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const [module, hash] = name.split('.'); |
| 41 | + newHashes[module] = hash; |
| 42 | + }); |
| 43 | + }) |
| 44 | + .then(() => { |
| 45 | + console.log(' Validating hashes...'); |
| 46 | + console.log(` Old hashes: ${JSON.stringify(oldHashes)}`); |
| 47 | + console.log(` New hashes: ${JSON.stringify(newHashes)}`); |
| 48 | + |
| 49 | + Object.keys(oldHashes) |
| 50 | + .forEach(module => { |
| 51 | + if (oldHashes[module] == newHashes[module]) { |
| 52 | + throw new Error(`Module "${module}" did not change hash (${oldHashes[module]})...`); |
| 53 | + } |
| 54 | + }); |
| 55 | + }); |
| 56 | +} |
0 commit comments