|
| 1 | +import { createHash } from 'crypto'; |
| 2 | +import { |
| 3 | + appendToFile, |
| 4 | + expectFileToMatch, |
| 5 | + prependToFile, |
| 6 | + readFile, |
| 7 | + replaceInFile, |
| 8 | + writeFile, |
| 9 | +} from '../../utils/fs'; |
| 10 | +import { ng } from '../../utils/process'; |
| 11 | + |
| 12 | +export default async function () { |
| 13 | + // Enable Differential loading |
| 14 | + await replaceInFile('.browserslistrc', 'not IE 11', 'IE 11'); |
| 15 | + |
| 16 | + const appRoutingModulePath = 'src/app/app-routing.module.ts'; |
| 17 | + |
| 18 | + // Add app routing. |
| 19 | + // This is done automatically on a new app with --routing. |
| 20 | + await writeFile( |
| 21 | + appRoutingModulePath, |
| 22 | + ` |
| 23 | + import { NgModule } from '@angular/core'; |
| 24 | + import { Routes, RouterModule } from '@angular/router'; |
| 25 | +
|
| 26 | + const routes: Routes = []; |
| 27 | +
|
| 28 | + @NgModule({ |
| 29 | + imports: [RouterModule.forRoot(routes)], |
| 30 | + exports: [RouterModule] |
| 31 | + }) |
| 32 | + export class AppRoutingModule { } |
| 33 | + `, |
| 34 | + ); |
| 35 | + await prependToFile( |
| 36 | + 'src/app/app.module.ts', |
| 37 | + `import { AppRoutingModule } from './app-routing.module';`, |
| 38 | + ); |
| 39 | + await replaceInFile('src/app/app.module.ts', `imports: [`, `imports: [ AppRoutingModule,`); |
| 40 | + await appendToFile('src/app/app.component.html', '<router-outlet></router-outlet>'); |
| 41 | + |
| 42 | + await ng('generate', 'module', 'lazy', '--module=app.module', '--route', 'lazy'); |
| 43 | + |
| 44 | + await ng( |
| 45 | + 'build', |
| 46 | + '--prod', |
| 47 | + '--subresource-integrity', |
| 48 | + '--output-hashing=none', |
| 49 | + '--output-path=dist/first', |
| 50 | + ); |
| 51 | + |
| 52 | + // Second build used to ensure cached files use correct integrity values |
| 53 | + await ng( |
| 54 | + 'build', |
| 55 | + '--prod', |
| 56 | + '--subresource-integrity', |
| 57 | + '--output-hashing=none', |
| 58 | + '--output-path=dist/second', |
| 59 | + ); |
| 60 | + |
| 61 | + const codeHashES5 = createHash('sha384') |
| 62 | + .update(await readFile('dist/first/5-es5.js')) |
| 63 | + .digest('base64'); |
| 64 | + const codeHashES2015 = createHash('sha384') |
| 65 | + .update(await readFile('dist/first/5-es2015.js')) |
| 66 | + .digest('base64'); |
| 67 | + |
| 68 | + await expectFileToMatch('dist/first/runtime-es5.js', 'sha384-' + codeHashES5); |
| 69 | + await expectFileToMatch('dist/first/runtime-es2015.js', 'sha384-' + codeHashES2015); |
| 70 | + |
| 71 | + await expectFileToMatch('dist/second/runtime-es5.js', 'sha384-' + codeHashES5); |
| 72 | + await expectFileToMatch('dist/second/runtime-es2015.js', 'sha384-' + codeHashES2015); |
| 73 | +} |
0 commit comments