|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { buildApplication } from '../../index'; |
| 10 | +import { Format, OutputMode } from '../../schema'; |
| 11 | +import { |
| 12 | + APPLICATION_BUILDER_INFO, |
| 13 | + BASE_OPTIONS, |
| 14 | + describeBuilder, |
| 15 | + expectLog, |
| 16 | + expectNoLog, |
| 17 | +} from '../setup'; |
| 18 | + |
| 19 | +const routeFiles: Record<string, string> = { |
| 20 | + 'src/app/app.module.ts': ` |
| 21 | + import { NgModule } from '@angular/core'; |
| 22 | + import { BrowserModule } from '@angular/platform-browser'; |
| 23 | + import { RouterModule } from '@angular/router'; |
| 24 | + import { AppComponent } from './app.component'; |
| 25 | + import { routes } from './app.routes'; |
| 26 | +
|
| 27 | + @NgModule({ |
| 28 | + declarations: [AppComponent], |
| 29 | + imports: [BrowserModule, RouterModule.forRoot(routes)], |
| 30 | + bootstrap: [AppComponent], |
| 31 | + }) |
| 32 | + export class AppModule {} |
| 33 | + `, |
| 34 | + 'src/app/app.routes.ts': ` |
| 35 | + import { Component } from '@angular/core'; |
| 36 | + import { Routes } from '@angular/router'; |
| 37 | +
|
| 38 | + @Component({ selector: 'app-home', template: '<p>home works!</p>' }) |
| 39 | + export class HomeComponent {} |
| 40 | +
|
| 41 | + @Component({ selector: 'app-foo', template: '<p>foo works!</p>' }) |
| 42 | + export class FooComponent {} |
| 43 | +
|
| 44 | + @Component({ selector: 'app-bar', template: '<p>foo-bar works!</p>' }) |
| 45 | + export class BarComponent {} |
| 46 | +
|
| 47 | + export const routes: Routes = [ |
| 48 | + { path: '', component: HomeComponent }, |
| 49 | + { path: 'foo', component: FooComponent }, |
| 50 | + { path: 'foo/bar', component: BarComponent }, |
| 51 | + { path: 'old-foo', redirectTo: 'foo' }, |
| 52 | + ]; |
| 53 | + `, |
| 54 | + 'src/app/app.component.html': `<router-outlet></router-outlet>`, |
| 55 | + 'src/server.ts': `console.log('Hello!');`, |
| 56 | +}; |
| 57 | + |
| 58 | +describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { |
| 59 | + beforeEach(async () => { |
| 60 | + await harness.modifyFile('src/tsconfig.app.json', (content) => { |
| 61 | + const tsConfig = JSON.parse(content); |
| 62 | + tsConfig.files ??= []; |
| 63 | + tsConfig.files.push('main.server.ts', 'server.ts'); |
| 64 | + |
| 65 | + return JSON.stringify(tsConfig); |
| 66 | + }); |
| 67 | + |
| 68 | + await harness.writeFiles(routeFiles); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('Option: "prerender.format"', () => { |
| 72 | + it(`should write routes to '<route>.html' when set to 'file'`, async () => { |
| 73 | + harness.useTarget('build', { |
| 74 | + ...BASE_OPTIONS, |
| 75 | + server: 'src/main.server.ts', |
| 76 | + polyfills: ['zone.js'], |
| 77 | + outputMode: OutputMode.Static, |
| 78 | + prerender: { format: Format.File }, |
| 79 | + }); |
| 80 | + |
| 81 | + const { result, logs } = await harness.executeOnce(); |
| 82 | + expect(result?.success).toBeTrue(); |
| 83 | + expectNoLog(logs, 'The "prerender" option has no effect'); |
| 84 | + |
| 85 | + harness.expectFile('dist/browser/index.html').content.toContain('home works!'); |
| 86 | + harness.expectFile('dist/browser/foo.html').content.toContain('foo works!'); |
| 87 | + harness.expectFile('dist/browser/foo/bar.html').content.toContain('foo-bar works!'); |
| 88 | + harness |
| 89 | + .expectFile('dist/browser/old-foo.html') |
| 90 | + .content.toContain('<meta http-equiv="refresh" content="0; url=/foo">'); |
| 91 | + harness.expectFile('dist/browser/foo/index.html').toNotExist(); |
| 92 | + |
| 93 | + const content = harness.readFile('dist/prerendered-routes.json'); |
| 94 | + expect(Object.keys(JSON.parse(content).routes)).toEqual( |
| 95 | + jasmine.arrayContaining(['/', '/foo', '/foo/bar']), |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it(`should warn when 'prerender' is an object without 'format' and 'outputMode' is specified`, async () => { |
| 100 | + harness.useTarget('build', { |
| 101 | + ...BASE_OPTIONS, |
| 102 | + server: 'src/main.server.ts', |
| 103 | + polyfills: ['zone.js'], |
| 104 | + outputMode: OutputMode.Static, |
| 105 | + prerender: { routesFile: 'routes.txt' }, |
| 106 | + }); |
| 107 | + |
| 108 | + const { result, logs } = await harness.executeOnce(); |
| 109 | + expect(result?.success).toBeTrue(); |
| 110 | + |
| 111 | + expectLog(logs, 'The "prerender" option has no effect when "outputMode" is specified'); |
| 112 | + }); |
| 113 | + |
| 114 | + it(`should write routes to '<route>/index.html' by default`, async () => { |
| 115 | + harness.useTarget('build', { |
| 116 | + ...BASE_OPTIONS, |
| 117 | + server: 'src/main.server.ts', |
| 118 | + polyfills: ['zone.js'], |
| 119 | + outputMode: OutputMode.Static, |
| 120 | + }); |
| 121 | + |
| 122 | + const { result } = await harness.executeOnce(); |
| 123 | + expect(result?.success).toBeTrue(); |
| 124 | + |
| 125 | + harness.expectFile('dist/browser/index.html').content.toContain('home works!'); |
| 126 | + harness.expectFile('dist/browser/foo/index.html').content.toContain('foo works!'); |
| 127 | + harness.expectFile('dist/browser/foo/bar/index.html').content.toContain('foo-bar works!'); |
| 128 | + harness.expectFile('dist/browser/foo.html').toNotExist(); |
| 129 | + }); |
| 130 | + |
| 131 | + it(`should warn and write '<route>/index.html' when set to 'file' and the build produces a server`, async () => { |
| 132 | + harness.useTarget('build', { |
| 133 | + ...BASE_OPTIONS, |
| 134 | + server: 'src/main.server.ts', |
| 135 | + polyfills: ['zone.js'], |
| 136 | + outputMode: OutputMode.Server, |
| 137 | + ssr: { entry: 'src/server.ts' }, |
| 138 | + prerender: { format: Format.File }, |
| 139 | + }); |
| 140 | + |
| 141 | + const { result, logs } = await harness.executeOnce(); |
| 142 | + expect(result?.success).toBeTrue(); |
| 143 | + |
| 144 | + expectLog( |
| 145 | + logs, |
| 146 | + 'The "prerender.format" option is not considered when the build produces a server.', |
| 147 | + ); |
| 148 | + |
| 149 | + harness.expectFile('dist/browser/foo/index.html').content.toContain('foo works!'); |
| 150 | + harness.expectFile('dist/browser/foo.html').toNotExist(); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments