|
8 | 8 |
|
9 | 9 | import type { Metafile } from 'esbuild';
|
10 | 10 | import { extname } from 'node:path';
|
| 11 | +import { runInThisContext } from 'node:vm'; |
11 | 12 | import { NormalizedApplicationBuildOptions } from '../../builders/application/options';
|
12 | 13 | import { type BuildOutputFile, BuildOutputFileType } from '../../tools/esbuild/bundler-context';
|
13 | 14 | import { createOutputFile } from '../../tools/esbuild/utils';
|
@@ -139,20 +140,27 @@ export function generateAngularServerAppManifest(
|
139 | 140 | } {
|
140 | 141 | const serverAssetsChunks: BuildOutputFile[] = [];
|
141 | 142 | const serverAssets: Record<string, string> = {};
|
| 143 | + |
142 | 144 | for (const file of [...additionalHtmlOutputFiles.values(), ...outputFiles]) {
|
143 | 145 | const extension = extname(file.path);
|
144 | 146 | if (extension === '.html' || (inlineCriticalCss && extension === '.css')) {
|
145 | 147 | const jsChunkFilePath = `assets-chunks/${file.path.replace(/[./]/g, '_')}.mjs`;
|
| 148 | + const escapedContent = escapeUnsafeChars(file.text); |
| 149 | + |
146 | 150 | serverAssetsChunks.push(
|
147 | 151 | createOutputFile(
|
148 | 152 | jsChunkFilePath,
|
149 |
| - `export default \`${escapeUnsafeChars(file.text)}\`;`, |
| 153 | + `export default \`${escapedContent}\`;`, |
150 | 154 | BuildOutputFileType.ServerApplication,
|
151 | 155 | ),
|
152 | 156 | );
|
153 | 157 |
|
| 158 | + // This is needed because JavaScript engines script parser convert `\r\n` to `\n` in template literals, |
| 159 | + // which can result in an incorrect byte length. |
| 160 | + const size = runInThisContext(`new TextEncoder().encode(\`${escapedContent}\`).byteLength`); |
| 161 | + |
154 | 162 | serverAssets[file.path] =
|
155 |
| - `{size: ${file.size}, hash: '${file.hash}', text: () => import('./${jsChunkFilePath}').then(m => m.default)}`; |
| 163 | + `{size: ${size}, hash: '${file.hash}', text: () => import('./${jsChunkFilePath}').then(m => m.default)}`; |
156 | 164 | }
|
157 | 165 | }
|
158 | 166 |
|
|
0 commit comments