|
| 1 | +import { join } from 'node:path'; |
| 2 | +import { existsSync } from 'node:fs'; |
| 3 | +import assert from 'node:assert'; |
| 4 | +import { expectFileNotToExist, expectFileToMatch, writeFile } from '../../../utils/fs'; |
| 5 | +import { ng, noSilentNg, silentNg } from '../../../utils/process'; |
| 6 | +import { installWorkspacePackages, uninstallPackage } from '../../../utils/packages'; |
| 7 | +import { useSha } from '../../../utils/project'; |
| 8 | +import { getGlobalVariable } from '../../../utils/env'; |
| 9 | +import { langTranslations, setupI18nConfig } from '../../i18n/setup'; |
| 10 | + |
| 11 | +export default async function () { |
| 12 | + assert( |
| 13 | + getGlobalVariable('argv')['esbuild'], |
| 14 | + 'This test should not be called in the Webpack suite.', |
| 15 | + ); |
| 16 | + |
| 17 | + // Setup project |
| 18 | + await setupI18nConfig(); |
| 19 | + |
| 20 | + // Forcibly remove in case another test doesn't clean itself up. |
| 21 | + await uninstallPackage('@angular/ssr'); |
| 22 | + await ng('add', '@angular/ssr', '--server-routing', '--skip-confirmation', '--skip-install'); |
| 23 | + await useSha(); |
| 24 | + await installWorkspacePackages(); |
| 25 | + |
| 26 | + // Add routes |
| 27 | + await writeFile( |
| 28 | + 'src/app/app.routes.ts', |
| 29 | + ` |
| 30 | + import { Routes } from '@angular/router'; |
| 31 | + import { HomeComponent } from './home/home.component'; |
| 32 | + import { SsgComponent } from './ssg/ssg.component'; |
| 33 | +
|
| 34 | + export const routes: Routes = [ |
| 35 | + { |
| 36 | + path: '', |
| 37 | + component: HomeComponent, |
| 38 | + }, |
| 39 | + { |
| 40 | + path: 'ssg', |
| 41 | + component: SsgComponent, |
| 42 | + }, |
| 43 | + { |
| 44 | + path: '**', |
| 45 | + component: HomeComponent, |
| 46 | + }, |
| 47 | + ]; |
| 48 | + `, |
| 49 | + ); |
| 50 | + |
| 51 | + // Add server routing |
| 52 | + await writeFile( |
| 53 | + 'src/app/app.routes.server.ts', |
| 54 | + ` |
| 55 | + import { RenderMode, ServerRoute } from '@angular/ssr'; |
| 56 | +
|
| 57 | + export const serverRoutes: ServerRoute[] = [ |
| 58 | + { |
| 59 | + path: '**', |
| 60 | + renderMode: RenderMode.Prerender, |
| 61 | + }, |
| 62 | + ]; |
| 63 | + `, |
| 64 | + ); |
| 65 | + |
| 66 | + await writeFile( |
| 67 | + 'src/app/app.config.ts', |
| 68 | + ` |
| 69 | + import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core'; |
| 70 | + import { provideRouter } from '@angular/router'; |
| 71 | +
|
| 72 | + import { routes } from './app.routes'; |
| 73 | + import { provideClientHydration } from '@angular/platform-browser'; |
| 74 | + import { APP_BASE_HREF } from '@angular/common'; |
| 75 | +
|
| 76 | + export const appConfig: ApplicationConfig = { |
| 77 | + providers: [ |
| 78 | + provideZoneChangeDetection({ eventCoalescing: true }), |
| 79 | + provideRouter(routes), |
| 80 | + provideClientHydration(), |
| 81 | + { |
| 82 | + provide: APP_BASE_HREF, |
| 83 | + useValue: '/', |
| 84 | + }, |
| 85 | + ], |
| 86 | + }; |
| 87 | + `, |
| 88 | + ); |
| 89 | + |
| 90 | + // Generate components for the above routes |
| 91 | + await silentNg('generate', 'component', 'home'); |
| 92 | + await silentNg('generate', 'component', 'ssg'); |
| 93 | + |
| 94 | + await noSilentNg('build', '--output-mode=static'); |
| 95 | + |
| 96 | + for (const { lang, outputPath } of langTranslations) { |
| 97 | + await expectFileToMatch(join(outputPath, 'index.html'), `<p id="locale">${lang}</p>`); |
| 98 | + await expectFileToMatch(join(outputPath, 'ssg/index.html'), `<p id="locale">${lang}</p>`); |
| 99 | + } |
| 100 | + |
| 101 | + // Check that server directory does not exist |
| 102 | + assert( |
| 103 | + !existsSync('dist/test-project/server'), |
| 104 | + 'Server directory should not exist when output-mode is static', |
| 105 | + ); |
| 106 | +} |
0 commit comments