|
| 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 { OutputMode, Platform } from '../../schema'; |
| 11 | +import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; |
| 12 | + |
| 13 | +/** |
| 14 | + * `@angular/platform-server/init` (bundled into the server polyfills) and |
| 15 | + * `@angular/platform-server` (bundled into the main server bundle) both import this file. |
| 16 | + */ |
| 17 | +const DOMINO_BUNDLE = 'third_party/domino/bundled-domino.mjs'; |
| 18 | + |
| 19 | +/** The property on globalThis where the Domino instance is stored. */ |
| 20 | +const DOMINO_GLOBAL_PROPERTY = /(?:ɵ|\\u0275)domino/; |
| 21 | + |
| 22 | +describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { |
| 23 | + describe('Behavior: "Domino is bundled once for the server"', () => { |
| 24 | + beforeEach(async () => { |
| 25 | + await harness.modifyFile('src/tsconfig.app.json', (content) => { |
| 26 | + const tsConfig = JSON.parse(content); |
| 27 | + tsConfig.files ??= []; |
| 28 | + tsConfig.files.push('main.server.ts', 'server.ts'); |
| 29 | + |
| 30 | + return JSON.stringify(tsConfig); |
| 31 | + }); |
| 32 | + |
| 33 | + await harness.writeFile('src/server.ts', `console.log('Hello!');`); |
| 34 | + |
| 35 | + // The default template uses i18n which requires `@angular/localize` to prerender. |
| 36 | + await harness.writeFile('src/app/app.component.html', `<p>Hello!</p>`); |
| 37 | + }); |
| 38 | + |
| 39 | + it('shares the server polyfills copy of Domino with the main server bundle', async () => { |
| 40 | + harness.useTarget('build', { |
| 41 | + ...BASE_OPTIONS, |
| 42 | + server: 'src/main.server.ts', |
| 43 | + ssr: true, |
| 44 | + }); |
| 45 | + |
| 46 | + const { result } = await harness.executeOnce(); |
| 47 | + expect(result?.success).toBeTrue(); |
| 48 | + |
| 49 | + harness.expectFile('dist/server/polyfills.server.mjs').content.toContain(DOMINO_BUNDLE); |
| 50 | + harness |
| 51 | + .expectFile('dist/server/polyfills.server.mjs') |
| 52 | + .content.toMatch(DOMINO_GLOBAL_PROPERTY); |
| 53 | + |
| 54 | + harness.expectFile('dist/server/main.server.mjs').content.not.toContain(DOMINO_BUNDLE); |
| 55 | + harness.expectFile('dist/server/main.server.mjs').content.toMatch(DOMINO_GLOBAL_PROPERTY); |
| 56 | + }); |
| 57 | + |
| 58 | + it('shares the server polyfills copy of Domino when "outputMode" is "server"', async () => { |
| 59 | + harness.useTarget('build', { |
| 60 | + ...BASE_OPTIONS, |
| 61 | + server: 'src/main.server.ts', |
| 62 | + ssr: { entry: 'src/server.ts' }, |
| 63 | + outputMode: OutputMode.Server, |
| 64 | + }); |
| 65 | + |
| 66 | + const { result } = await harness.executeOnce(); |
| 67 | + expect(result?.success).toBeTrue(); |
| 68 | + |
| 69 | + harness.expectFile('dist/server/polyfills.server.mjs').content.toContain(DOMINO_BUNDLE); |
| 70 | + harness.expectFile('dist/server/main.server.mjs').content.not.toContain(DOMINO_BUNDLE); |
| 71 | + harness.expectFile('dist/server/main.server.mjs').content.toMatch(DOMINO_GLOBAL_PROPERTY); |
| 72 | + harness.expectFile('dist/server/server.mjs').content.not.toContain(DOMINO_BUNDLE); |
| 73 | + }); |
| 74 | + |
| 75 | + it('shares the server polyfills copy of Domino when "ssr.platform" is "neutral"', async () => { |
| 76 | + harness.useTarget('build', { |
| 77 | + ...BASE_OPTIONS, |
| 78 | + server: 'src/main.server.ts', |
| 79 | + ssr: { entry: 'src/server.ts', platform: Platform.Neutral }, |
| 80 | + outputMode: OutputMode.Server, |
| 81 | + }); |
| 82 | + |
| 83 | + const { result } = await harness.executeOnce(); |
| 84 | + expect(result?.success).toBeTrue(); |
| 85 | + |
| 86 | + harness.expectFile('dist/server/polyfills.server.mjs').content.toContain(DOMINO_BUNDLE); |
| 87 | + harness |
| 88 | + .expectFile('dist/server/polyfills.server.mjs') |
| 89 | + .content.toMatch(DOMINO_GLOBAL_PROPERTY); |
| 90 | + harness.expectFile('dist/server/main.server.mjs').content.not.toContain(DOMINO_BUNDLE); |
| 91 | + harness.expectFile('dist/server/main.server.mjs').content.toMatch(DOMINO_GLOBAL_PROPERTY); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments