|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
| 9 | +import { getSystemPath } from '@angular-devkit/core'; |
| 10 | +import { createHash } from 'node:crypto'; |
| 11 | +import { readdirSync, readFileSync } from 'node:fs'; |
| 12 | +import { join } from 'node:path'; |
9 | 13 | import { buildApplication } from '../../index'; |
10 | | -import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder, expectNoLog } from '../setup'; |
| 14 | +import { |
| 15 | + APPLICATION_BUILDER_INFO, |
| 16 | + BASE_OPTIONS, |
| 17 | + describeBuilder, |
| 18 | + expectNoLog, |
| 19 | + host, |
| 20 | + lazyModuleFiles, |
| 21 | + lazyModuleFnImport, |
| 22 | +} from '../setup'; |
| 23 | + |
| 24 | +/** Resolve a path inside the harness workspace synchronously. */ |
| 25 | +function workspacePath(...segments: string[]): string { |
| 26 | + return join(getSystemPath(host.root()), ...segments); |
| 27 | +} |
11 | 28 |
|
12 | 29 | describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { |
13 | 30 | describe('Option: "subresourceIntegrity"', () => { |
@@ -65,5 +82,84 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { |
65 | 82 | .content.toMatch(/integrity="\w+-[A-Za-z0-9/+=]+"/); |
66 | 83 | expectNoLog(logs, /subresource-integrity/); |
67 | 84 | }); |
| 85 | + |
| 86 | + it(`emits an importmap with integrity for lazy chunks when 'true'`, async () => { |
| 87 | + await harness.writeFiles(lazyModuleFiles); |
| 88 | + await harness.writeFiles(lazyModuleFnImport); |
| 89 | + |
| 90 | + harness.useTarget('build', { |
| 91 | + ...BASE_OPTIONS, |
| 92 | + subresourceIntegrity: true, |
| 93 | + }); |
| 94 | + |
| 95 | + const { result } = await harness.executeOnce(); |
| 96 | + expect(result?.success).toBe(true); |
| 97 | + |
| 98 | + const indexHtml = harness.readFile('dist/browser/index.html'); |
| 99 | + const match = indexHtml.match(/<script type="importmap">([^<]+)<\/script>/); |
| 100 | + expect(match).withContext('importmap script tag missing').not.toBeNull(); |
| 101 | + |
| 102 | + const importmap = JSON.parse(match![1]) as { integrity: Record<string, string> }; |
| 103 | + expect(importmap.integrity).toBeDefined(); |
| 104 | + |
| 105 | + const initialJsFiles = new Set<string>(); |
| 106 | + for (const [, src] of indexHtml.matchAll(/<script[^>]*src="([^"]+)"[^>]*>/g)) { |
| 107 | + initialJsFiles.add(src); |
| 108 | + } |
| 109 | + for (const [, href] of indexHtml.matchAll( |
| 110 | + /<link[^>]*rel="modulepreload"[^>]*href="([^"]+)"[^>]*>/g, |
| 111 | + )) { |
| 112 | + initialJsFiles.add(href); |
| 113 | + } |
| 114 | + |
| 115 | + const distDir = workspacePath('dist/browser'); |
| 116 | + const lazyJsFiles = readdirSync(distDir).filter( |
| 117 | + (f) => f.endsWith('.js') && !initialJsFiles.has(f), |
| 118 | + ); |
| 119 | + expect(lazyJsFiles.length) |
| 120 | + .withContext('expected at least one non-initial (lazy) JS file') |
| 121 | + .toBeGreaterThan(0); |
| 122 | + |
| 123 | + const importmapFiles = Object.keys(importmap.integrity); |
| 124 | + expect(importmapFiles.sort()) |
| 125 | + .withContext('importmap integrity keys should match emitted lazy JS files') |
| 126 | + .toEqual(lazyJsFiles.sort()); |
| 127 | + |
| 128 | + for (const [file, integrity] of Object.entries(importmap.integrity)) { |
| 129 | + const expectedSri = |
| 130 | + 'sha384-' + |
| 131 | + createHash('sha384') |
| 132 | + .update(readFileSync(join(distDir, file))) |
| 133 | + .digest('base64'); |
| 134 | + |
| 135 | + expect(integrity) |
| 136 | + .withContext('importmap integrity hash should match for emitted lazy JS file ' + file) |
| 137 | + .toBe(expectedSri); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + it(`places the importmap before any module script tag`, async () => { |
| 142 | + await harness.writeFiles(lazyModuleFiles); |
| 143 | + await harness.writeFiles(lazyModuleFnImport); |
| 144 | + |
| 145 | + harness.useTarget('build', { |
| 146 | + ...BASE_OPTIONS, |
| 147 | + subresourceIntegrity: true, |
| 148 | + }); |
| 149 | + |
| 150 | + const { result } = await harness.executeOnce(); |
| 151 | + expect(result?.success).toBe(true); |
| 152 | + |
| 153 | + const indexHtml = harness.readFile('dist/browser/index.html'); |
| 154 | + const importmapIdx = indexHtml.indexOf('<script type="importmap">'); |
| 155 | + const moduleScriptMatch = indexHtml.match(/<script[^>]*type="module"[^>]*>/); |
| 156 | + const moduleScriptIdx = moduleScriptMatch ? (moduleScriptMatch.index ?? -1) : -1; |
| 157 | + |
| 158 | + expect(importmapIdx).toBeGreaterThanOrEqual(0); |
| 159 | + expect(moduleScriptIdx).toBeGreaterThanOrEqual(0); |
| 160 | + expect(importmapIdx) |
| 161 | + .withContext('importmap must precede the first module script tag') |
| 162 | + .toBeLessThan(moduleScriptIdx); |
| 163 | + }); |
68 | 164 | }); |
69 | 165 | }); |
0 commit comments