@angular/build 18.0.0-next.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (253) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +5 -0
  3. package/builders.json +14 -0
  4. package/package.json +87 -0
  5. package/src/builders/application/build-action.d.ts +33 -0
  6. package/src/builders/application/build-action.js +183 -0
  7. package/src/builders/application/execute-build.d.ts +11 -0
  8. package/src/builders/application/execute-build.js +125 -0
  9. package/src/builders/application/execute-post-bundle.d.ts +25 -0
  10. package/src/builders/application/execute-post-bundle.js +93 -0
  11. package/src/builders/application/i18n.d.ts +29 -0
  12. package/src/builders/application/i18n.js +128 -0
  13. package/src/builders/application/index.d.ts +57 -0
  14. package/src/builders/application/index.js +121 -0
  15. package/src/builders/application/options.d.ts +149 -0
  16. package/src/builders/application/options.js +369 -0
  17. package/src/builders/application/schema.d.ts +512 -0
  18. package/src/builders/application/schema.js +58 -0
  19. package/src/builders/application/schema.json +635 -0
  20. package/src/builders/application/setup-bundling.d.ts +19 -0
  21. package/src/builders/application/setup-bundling.js +71 -0
  22. package/src/builders/dev-server/builder.d.ts +32 -0
  23. package/src/builders/dev-server/builder.js +70 -0
  24. package/src/builders/dev-server/index.d.ts +14 -0
  25. package/src/builders/dev-server/index.js +15 -0
  26. package/src/builders/dev-server/internal.d.ts +19 -0
  27. package/src/builders/dev-server/internal.js +29 -0
  28. package/src/builders/dev-server/options.d.ts +47 -0
  29. package/src/builders/dev-server/options.js +65 -0
  30. package/src/builders/dev-server/output.d.ts +16 -0
  31. package/src/builders/dev-server/output.js +9 -0
  32. package/src/builders/dev-server/schema.d.ts +118 -0
  33. package/src/builders/dev-server/schema.js +4 -0
  34. package/src/builders/dev-server/schema.json +131 -0
  35. package/src/builders/dev-server/vite-server.d.ts +31 -0
  36. package/src/builders/dev-server/vite-server.js +504 -0
  37. package/src/index.d.ts +11 -0
  38. package/src/index.js +16 -0
  39. package/src/private.d.ts +35 -0
  40. package/src/private.js +70 -0
  41. package/src/tools/babel/plugins/adjust-static-class-members.d.ts +26 -0
  42. package/src/tools/babel/plugins/adjust-static-class-members.js +351 -0
  43. package/src/tools/babel/plugins/adjust-typescript-enums.d.ts +22 -0
  44. package/src/tools/babel/plugins/adjust-typescript-enums.js +113 -0
  45. package/src/tools/babel/plugins/elide-angular-metadata.d.ts +22 -0
  46. package/src/tools/babel/plugins/elide-angular-metadata.js +110 -0
  47. package/src/tools/babel/plugins/index.d.ts +11 -0
  48. package/src/tools/babel/plugins/index.js +21 -0
  49. package/src/tools/babel/plugins/pure-toplevel-functions.d.ts +15 -0
  50. package/src/tools/babel/plugins/pure-toplevel-functions.js +90 -0
  51. package/src/tools/babel/typings.d.ts +21 -0
  52. package/src/tools/esbuild/angular/angular-host.d.ts +26 -0
  53. package/src/tools/esbuild/angular/angular-host.js +134 -0
  54. package/src/tools/esbuild/angular/compilation/angular-compilation.d.ts +42 -0
  55. package/src/tools/esbuild/angular/compilation/angular-compilation.js +94 -0
  56. package/src/tools/esbuild/angular/compilation/aot-compilation.d.ts +21 -0
  57. package/src/tools/esbuild/angular/compilation/aot-compilation.js +224 -0
  58. package/src/tools/esbuild/angular/compilation/factory.d.ts +16 -0
  59. package/src/tools/esbuild/angular/compilation/factory.js +56 -0
  60. package/src/tools/esbuild/angular/compilation/index.d.ts +10 -0
  61. package/src/tools/esbuild/angular/compilation/index.js +17 -0
  62. package/src/tools/esbuild/angular/compilation/jit-bootstrap-transformer.d.ts +10 -0
  63. package/src/tools/esbuild/angular/compilation/jit-bootstrap-transformer.js +182 -0
  64. package/src/tools/esbuild/angular/compilation/jit-compilation.d.ts +21 -0
  65. package/src/tools/esbuild/angular/compilation/jit-compilation.js +106 -0
  66. package/src/tools/esbuild/angular/compilation/noop-compilation.d.ts +20 -0
  67. package/src/tools/esbuild/angular/compilation/noop-compilation.js +26 -0
  68. package/src/tools/esbuild/angular/compilation/parallel-compilation.d.ts +42 -0
  69. package/src/tools/esbuild/angular/compilation/parallel-compilation.js +123 -0
  70. package/src/tools/esbuild/angular/compilation/parallel-worker.d.ts +33 -0
  71. package/src/tools/esbuild/angular/compilation/parallel-worker.js +90 -0
  72. package/src/tools/esbuild/angular/compilation-state.d.ts +15 -0
  73. package/src/tools/esbuild/angular/compilation-state.js +44 -0
  74. package/src/tools/esbuild/angular/compiler-plugin.d.ts +27 -0
  75. package/src/tools/esbuild/angular/compiler-plugin.js +441 -0
  76. package/src/tools/esbuild/angular/component-stylesheets.d.ts +44 -0
  77. package/src/tools/esbuild/angular/component-stylesheets.js +150 -0
  78. package/src/tools/esbuild/angular/diagnostics.d.ts +15 -0
  79. package/src/tools/esbuild/angular/diagnostics.js +69 -0
  80. package/src/tools/esbuild/angular/file-reference-tracker.d.ts +17 -0
  81. package/src/tools/esbuild/angular/file-reference-tracker.js +57 -0
  82. package/src/tools/esbuild/angular/jit-plugin-callbacks.d.ts +23 -0
  83. package/src/tools/esbuild/angular/jit-plugin-callbacks.js +117 -0
  84. package/src/tools/esbuild/angular/jit-resource-transformer.d.ts +17 -0
  85. package/src/tools/esbuild/angular/jit-resource-transformer.js +186 -0
  86. package/src/tools/esbuild/angular/source-file-cache.d.ts +18 -0
  87. package/src/tools/esbuild/angular/source-file-cache.js +65 -0
  88. package/src/tools/esbuild/angular/uri.d.ts +54 -0
  89. package/src/tools/esbuild/angular/uri.js +74 -0
  90. package/src/tools/esbuild/angular/web-worker-transformer.d.ts +17 -0
  91. package/src/tools/esbuild/angular/web-worker-transformer.js +94 -0
  92. package/src/tools/esbuild/application-code-bundle.d.ts +20 -0
  93. package/src/tools/esbuild/application-code-bundle.js +368 -0
  94. package/src/tools/esbuild/budget-stats.d.ts +19 -0
  95. package/src/tools/esbuild/budget-stats.js +59 -0
  96. package/src/tools/esbuild/bundler-context.d.ts +75 -0
  97. package/src/tools/esbuild/bundler-context.js +366 -0
  98. package/src/tools/esbuild/bundler-execution-result.d.ts +71 -0
  99. package/src/tools/esbuild/bundler-execution-result.js +131 -0
  100. package/src/tools/esbuild/cache.d.ts +88 -0
  101. package/src/tools/esbuild/cache.js +92 -0
  102. package/src/tools/esbuild/commonjs-checker.d.ts +28 -0
  103. package/src/tools/esbuild/commonjs-checker.js +151 -0
  104. package/src/tools/esbuild/compiler-plugin-options.d.ts +16 -0
  105. package/src/tools/esbuild/compiler-plugin-options.js +49 -0
  106. package/src/tools/esbuild/external-packages-plugin.d.ts +18 -0
  107. package/src/tools/esbuild/external-packages-plugin.js +70 -0
  108. package/src/tools/esbuild/global-scripts.d.ts +16 -0
  109. package/src/tools/esbuild/global-scripts.js +142 -0
  110. package/src/tools/esbuild/global-styles.d.ts +10 -0
  111. package/src/tools/esbuild/global-styles.js +74 -0
  112. package/src/tools/esbuild/i18n-inliner-worker.d.ts +42 -0
  113. package/src/tools/esbuild/i18n-inliner-worker.js +136 -0
  114. package/src/tools/esbuild/i18n-inliner.d.ts +44 -0
  115. package/src/tools/esbuild/i18n-inliner.js +150 -0
  116. package/src/tools/esbuild/i18n-locale-plugin.d.ts +22 -0
  117. package/src/tools/esbuild/i18n-locale-plugin.js +120 -0
  118. package/src/tools/esbuild/index-html-generator.d.ts +15 -0
  119. package/src/tools/esbuild/index-html-generator.js +81 -0
  120. package/src/tools/esbuild/javascript-transformer-worker.d.ts +19 -0
  121. package/src/tools/esbuild/javascript-transformer-worker.js +154 -0
  122. package/src/tools/esbuild/javascript-transformer.d.ts +54 -0
  123. package/src/tools/esbuild/javascript-transformer.js +147 -0
  124. package/src/tools/esbuild/license-extractor.d.ts +25 -0
  125. package/src/tools/esbuild/license-extractor.js +158 -0
  126. package/src/tools/esbuild/load-result-cache.d.ts +21 -0
  127. package/src/tools/esbuild/load-result-cache.js +75 -0
  128. package/src/tools/esbuild/profiling.d.ts +11 -0
  129. package/src/tools/esbuild/profiling.js +78 -0
  130. package/src/tools/esbuild/rxjs-esm-resolution-plugin.d.ts +18 -0
  131. package/src/tools/esbuild/rxjs-esm-resolution-plugin.js +44 -0
  132. package/src/tools/esbuild/sourcemap-ignorelist-plugin.d.ts +17 -0
  133. package/src/tools/esbuild/sourcemap-ignorelist-plugin.js +73 -0
  134. package/src/tools/esbuild/stylesheets/bundle-options.d.ts +35 -0
  135. package/src/tools/esbuild/stylesheets/bundle-options.js +64 -0
  136. package/src/tools/esbuild/stylesheets/css-inline-fonts-plugin.d.ts +25 -0
  137. package/src/tools/esbuild/stylesheets/css-inline-fonts-plugin.js +57 -0
  138. package/src/tools/esbuild/stylesheets/css-language.d.ts +9 -0
  139. package/src/tools/esbuild/stylesheets/css-language.js +15 -0
  140. package/src/tools/esbuild/stylesheets/css-resource-plugin.d.ts +18 -0
  141. package/src/tools/esbuild/stylesheets/css-resource-plugin.js +114 -0
  142. package/src/tools/esbuild/stylesheets/less-language.d.ts +9 -0
  143. package/src/tools/esbuild/stylesheets/less-language.js +155 -0
  144. package/src/tools/esbuild/stylesheets/sass-language.d.ts +10 -0
  145. package/src/tools/esbuild/stylesheets/sass-language.js +185 -0
  146. package/src/tools/esbuild/stylesheets/stylesheet-plugin-factory.d.ts +58 -0
  147. package/src/tools/esbuild/stylesheets/stylesheet-plugin-factory.js +282 -0
  148. package/src/tools/esbuild/utils.d.ts +49 -0
  149. package/src/tools/esbuild/utils.js +392 -0
  150. package/src/tools/esbuild/virtual-module-plugin.d.ts +33 -0
  151. package/src/tools/esbuild/virtual-module-plugin.js +43 -0
  152. package/src/tools/esbuild/watcher.d.ts +25 -0
  153. package/src/tools/esbuild/watcher.js +118 -0
  154. package/src/tools/sass/lexer.d.ts +18 -0
  155. package/src/tools/sass/lexer.js +161 -0
  156. package/src/tools/sass/rebasing-importer.d.ts +101 -0
  157. package/src/tools/sass/rebasing-importer.js +334 -0
  158. package/src/tools/sass/sass-service.d.ts +72 -0
  159. package/src/tools/sass/sass-service.js +213 -0
  160. package/src/tools/sass/worker.d.ts +8 -0
  161. package/src/tools/sass/worker.js +192 -0
  162. package/src/tools/vite/angular-memory-plugin.d.ts +24 -0
  163. package/src/tools/vite/angular-memory-plugin.js +268 -0
  164. package/src/tools/vite/i18n-locale-plugin.d.ts +18 -0
  165. package/src/tools/vite/i18n-locale-plugin.js +55 -0
  166. package/src/utils/bundle-calculator.d.ts +44 -0
  167. package/src/utils/bundle-calculator.js +303 -0
  168. package/src/utils/check-port.d.ts +8 -0
  169. package/src/utils/check-port.js +58 -0
  170. package/src/utils/color.d.ts +10 -0
  171. package/src/utils/color.js +63 -0
  172. package/src/utils/delete-output-dir.d.ts +11 -0
  173. package/src/utils/delete-output-dir.js +46 -0
  174. package/src/utils/environment-options.d.ts +16 -0
  175. package/src/utils/environment-options.js +79 -0
  176. package/src/utils/error.d.ts +10 -0
  177. package/src/utils/error.js +21 -0
  178. package/src/utils/format-bytes.d.ts +8 -0
  179. package/src/utils/format-bytes.js +22 -0
  180. package/src/utils/i18n-options.d.ts +33 -0
  181. package/src/utils/i18n-options.js +161 -0
  182. package/src/utils/index-file/add-event-dispatch-contract.d.ts +8 -0
  183. package/src/utils/index-file/add-event-dispatch-contract.js +28 -0
  184. package/src/utils/index-file/augment-index-html.d.ts +40 -0
  185. package/src/utils/index-file/augment-index-html.js +239 -0
  186. package/src/utils/index-file/html-rewriting-stream.d.ts +11 -0
  187. package/src/utils/index-file/html-rewriting-stream.js +28 -0
  188. package/src/utils/index-file/index-html-generator.d.ts +57 -0
  189. package/src/utils/index-file/index-html-generator.js +135 -0
  190. package/src/utils/index-file/inline-critical-css.d.ts +24 -0
  191. package/src/utils/index-file/inline-critical-css.js +179 -0
  192. package/src/utils/index-file/inline-fonts.d.ts +23 -0
  193. package/src/utils/index-file/inline-fonts.js +267 -0
  194. package/src/utils/index-file/nonce.d.ts +12 -0
  195. package/src/utils/index-file/nonce.js +55 -0
  196. package/src/utils/index-file/style-nonce.d.ts +12 -0
  197. package/src/utils/index-file/style-nonce.js +55 -0
  198. package/src/utils/index.d.ts +11 -0
  199. package/src/utils/index.js +27 -0
  200. package/src/utils/load-esm.d.ts +20 -0
  201. package/src/utils/load-esm.js +31 -0
  202. package/src/utils/load-proxy-config.d.ts +8 -0
  203. package/src/utils/load-proxy-config.js +189 -0
  204. package/src/utils/load-translations.d.ts +16 -0
  205. package/src/utils/load-translations.js +84 -0
  206. package/src/utils/normalize-asset-patterns.d.ts +14 -0
  207. package/src/utils/normalize-asset-patterns.js +96 -0
  208. package/src/utils/normalize-cache.d.ts +16 -0
  209. package/src/utils/normalize-cache.js +44 -0
  210. package/src/utils/normalize-optimization.d.ts +13 -0
  211. package/src/utils/normalize-optimization.js +42 -0
  212. package/src/utils/normalize-source-maps.d.ts +9 -0
  213. package/src/utils/normalize-source-maps.js +23 -0
  214. package/src/utils/postcss-configuration.d.ts +17 -0
  215. package/src/utils/postcss-configuration.js +86 -0
  216. package/src/utils/purge-cache.d.ts +10 -0
  217. package/src/utils/purge-cache.js +40 -0
  218. package/src/utils/resolve-assets.d.ts +18 -0
  219. package/src/utils/resolve-assets.js +35 -0
  220. package/src/utils/routes-extractor/extractor.d.ts +15 -0
  221. package/src/utils/routes-extractor/extractor.js +97 -0
  222. package/src/utils/server-rendering/esm-in-memory-loader/loader-hooks.d.ts +18 -0
  223. package/src/utils/server-rendering/esm-in-memory-loader/loader-hooks.js +129 -0
  224. package/src/utils/server-rendering/esm-in-memory-loader/register-hooks.d.ts +8 -0
  225. package/src/utils/server-rendering/esm-in-memory-loader/register-hooks.js +13 -0
  226. package/src/utils/server-rendering/fetch-patch.d.ts +8 -0
  227. package/src/utils/server-rendering/fetch-patch.js +66 -0
  228. package/src/utils/server-rendering/load-esm-from-memory.d.ts +10 -0
  229. package/src/utils/server-rendering/load-esm-from-memory.js +26 -0
  230. package/src/utils/server-rendering/main-bundle-exports.d.ts +27 -0
  231. package/src/utils/server-rendering/main-bundle-exports.js +9 -0
  232. package/src/utils/server-rendering/prerender.d.ts +23 -0
  233. package/src/utils/server-rendering/prerender.js +192 -0
  234. package/src/utils/server-rendering/render-page.d.ts +26 -0
  235. package/src/utils/server-rendering/render-page.js +110 -0
  236. package/src/utils/server-rendering/render-worker.d.ts +22 -0
  237. package/src/utils/server-rendering/render-worker.js +30 -0
  238. package/src/utils/server-rendering/routes-extractor-worker.d.ts +21 -0
  239. package/src/utils/server-rendering/routes-extractor-worker.js +53 -0
  240. package/src/utils/service-worker.d.ts +25 -0
  241. package/src/utils/service-worker.js +211 -0
  242. package/src/utils/spinner.d.ts +20 -0
  243. package/src/utils/spinner.js +55 -0
  244. package/src/utils/stats-table.d.ts +20 -0
  245. package/src/utils/stats-table.js +205 -0
  246. package/src/utils/supported-browsers.d.ts +10 -0
  247. package/src/utils/supported-browsers.js +42 -0
  248. package/src/utils/tty.d.ts +8 -0
  249. package/src/utils/tty.js +23 -0
  250. package/src/utils/url.d.ts +8 -0
  251. package/src/utils/url.js +18 -0
  252. package/src/utils/version.d.ts +8 -0
  253. package/src/utils/version.js +59 -0
@@ -0,0 +1,239 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright Google LLC All Rights Reserved.
5
+ *
6
+ * Use of this source code is governed by an MIT-style license that can be
7
+ * found in the LICENSE file at https://angular.io/license
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.augmentIndexHtml = void 0;
11
+ const node_crypto_1 = require("node:crypto");
12
+ const node_path_1 = require("node:path");
13
+ const load_esm_1 = require("../load-esm");
14
+ const html_rewriting_stream_1 = require("./html-rewriting-stream");
15
+ /*
16
+ * Helper function used by the IndexHtmlWebpackPlugin.
17
+ * Can also be directly used by builder, e. g. in order to generate an index.html
18
+ * after processing several configurations in order to build different sets of
19
+ * bundles for differential serving.
20
+ */
21
+ // eslint-disable-next-line max-lines-per-function
22
+ async function augmentIndexHtml(params) {
23
+ const { loadOutputFile, files, entrypoints, sri, deployUrl = '', lang, baseHref, html, imageDomains, } = params;
24
+ const warnings = [];
25
+ const errors = [];
26
+ let { crossOrigin = 'none' } = params;
27
+ if (sri && crossOrigin === 'none') {
28
+ crossOrigin = 'anonymous';
29
+ }
30
+ const stylesheets = new Set();
31
+ const scripts = new Map();
32
+ // Sort files in the order we want to insert them by entrypoint
33
+ for (const [entrypoint, isModule] of entrypoints) {
34
+ for (const { extension, file, name } of files) {
35
+ if (name !== entrypoint || scripts.has(file) || stylesheets.has(file)) {
36
+ continue;
37
+ }
38
+ switch (extension) {
39
+ case '.js':
40
+ // Also, non entrypoints need to be loaded as no module as they can contain problematic code.
41
+ scripts.set(file, isModule);
42
+ break;
43
+ case '.mjs':
44
+ if (!isModule) {
45
+ // It would be very confusing to link an `*.mjs` file in a non-module script context,
46
+ // so we disallow it entirely.
47
+ throw new Error('`.mjs` files *must* set `isModule` to `true`.');
48
+ }
49
+ scripts.set(file, true /* isModule */);
50
+ break;
51
+ case '.css':
52
+ stylesheets.add(file);
53
+ break;
54
+ }
55
+ }
56
+ }
57
+ let scriptTags = [];
58
+ for (const [src, isModule] of scripts) {
59
+ const attrs = [`src="${deployUrl}${src}"`];
60
+ // This is also need for non entry-points as they may contain problematic code.
61
+ if (isModule) {
62
+ attrs.push('type="module"');
63
+ }
64
+ else {
65
+ attrs.push('defer');
66
+ }
67
+ if (crossOrigin !== 'none') {
68
+ attrs.push(`crossorigin="${crossOrigin}"`);
69
+ }
70
+ if (sri) {
71
+ const content = await loadOutputFile(src);
72
+ attrs.push(generateSriAttributes(content));
73
+ }
74
+ scriptTags.push(`<script ${attrs.join(' ')}></script>`);
75
+ }
76
+ let linkTags = [];
77
+ for (const src of stylesheets) {
78
+ const attrs = [`rel="stylesheet"`, `href="${deployUrl}${src}"`];
79
+ if (crossOrigin !== 'none') {
80
+ attrs.push(`crossorigin="${crossOrigin}"`);
81
+ }
82
+ if (sri) {
83
+ const content = await loadOutputFile(src);
84
+ attrs.push(generateSriAttributes(content));
85
+ }
86
+ linkTags.push(`<link ${attrs.join(' ')}>`);
87
+ }
88
+ if (params.hints?.length) {
89
+ for (const hint of params.hints) {
90
+ const attrs = [`rel="${hint.mode}"`, `href="${deployUrl}${hint.url}"`];
91
+ if (hint.mode !== 'modulepreload' && crossOrigin !== 'none') {
92
+ // Value is considered anonymous by the browser when not present or empty
93
+ attrs.push(crossOrigin === 'anonymous' ? 'crossorigin' : `crossorigin="${crossOrigin}"`);
94
+ }
95
+ if (hint.mode === 'preload' || hint.mode === 'prefetch') {
96
+ switch ((0, node_path_1.extname)(hint.url)) {
97
+ case '.js':
98
+ attrs.push('as="script"');
99
+ break;
100
+ case '.css':
101
+ attrs.push('as="style"');
102
+ break;
103
+ default:
104
+ if (hint.as) {
105
+ attrs.push(`as="${hint.as}"`);
106
+ }
107
+ break;
108
+ }
109
+ }
110
+ if (sri &&
111
+ (hint.mode === 'preload' || hint.mode === 'prefetch' || hint.mode === 'modulepreload')) {
112
+ const content = await loadOutputFile(hint.url);
113
+ attrs.push(generateSriAttributes(content));
114
+ }
115
+ linkTags.push(`<link ${attrs.join(' ')}>`);
116
+ }
117
+ }
118
+ const dir = lang ? await getLanguageDirection(lang, warnings) : undefined;
119
+ const { rewriter, transformedContent } = await (0, html_rewriting_stream_1.htmlRewritingStream)(html);
120
+ const baseTagExists = html.includes('<base');
121
+ const foundPreconnects = new Set();
122
+ rewriter
123
+ .on('startTag', (tag) => {
124
+ switch (tag.tagName) {
125
+ case 'html':
126
+ // Adjust document locale if specified
127
+ if (isString(lang)) {
128
+ updateAttribute(tag, 'lang', lang);
129
+ }
130
+ if (dir) {
131
+ updateAttribute(tag, 'dir', dir);
132
+ }
133
+ break;
134
+ case 'head':
135
+ // Base href should be added before any link, meta tags
136
+ if (!baseTagExists && isString(baseHref)) {
137
+ rewriter.emitStartTag(tag);
138
+ rewriter.emitRaw(`<base href="${baseHref}">`);
139
+ return;
140
+ }
141
+ break;
142
+ case 'base':
143
+ // Adjust base href if specified
144
+ if (isString(baseHref)) {
145
+ updateAttribute(tag, 'href', baseHref);
146
+ }
147
+ break;
148
+ case 'link':
149
+ if (readAttribute(tag, 'rel') === 'preconnect') {
150
+ const href = readAttribute(tag, 'href');
151
+ if (href) {
152
+ foundPreconnects.add(href);
153
+ }
154
+ }
155
+ break;
156
+ }
157
+ rewriter.emitStartTag(tag);
158
+ })
159
+ .on('endTag', (tag) => {
160
+ switch (tag.tagName) {
161
+ case 'head':
162
+ for (const linkTag of linkTags) {
163
+ rewriter.emitRaw(linkTag);
164
+ }
165
+ if (imageDomains) {
166
+ for (const imageDomain of imageDomains) {
167
+ if (!foundPreconnects.has(imageDomain)) {
168
+ rewriter.emitRaw(`<link rel="preconnect" href="${imageDomain}" data-ngimg>`);
169
+ }
170
+ }
171
+ }
172
+ linkTags = [];
173
+ break;
174
+ case 'body':
175
+ // Add script tags
176
+ for (const scriptTag of scriptTags) {
177
+ rewriter.emitRaw(scriptTag);
178
+ }
179
+ scriptTags = [];
180
+ break;
181
+ }
182
+ rewriter.emitEndTag(tag);
183
+ });
184
+ const content = await transformedContent();
185
+ return {
186
+ content: linkTags.length || scriptTags.length
187
+ ? // In case no body/head tags are not present (dotnet partial templates)
188
+ linkTags.join('') + scriptTags.join('') + content
189
+ : content,
190
+ warnings,
191
+ errors,
192
+ };
193
+ }
194
+ exports.augmentIndexHtml = augmentIndexHtml;
195
+ function generateSriAttributes(content) {
196
+ const algo = 'sha384';
197
+ const hash = (0, node_crypto_1.createHash)(algo).update(content, 'utf8').digest('base64');
198
+ return `integrity="${algo}-${hash}"`;
199
+ }
200
+ function updateAttribute(tag, name, value) {
201
+ const index = tag.attrs.findIndex((a) => a.name === name);
202
+ const newValue = { name, value };
203
+ if (index === -1) {
204
+ tag.attrs.push(newValue);
205
+ }
206
+ else {
207
+ tag.attrs[index] = newValue;
208
+ }
209
+ }
210
+ function readAttribute(tag, name) {
211
+ const targetAttr = tag.attrs.find((attr) => attr.name === name);
212
+ return targetAttr ? targetAttr.value : undefined;
213
+ }
214
+ function isString(value) {
215
+ return typeof value === 'string';
216
+ }
217
+ async function getLanguageDirection(locale, warnings) {
218
+ const dir = await getLanguageDirectionFromLocales(locale);
219
+ if (!dir) {
220
+ warnings.push(`Locale data for '${locale}' cannot be found. 'dir' attribute will not be set for this locale.`);
221
+ }
222
+ return dir;
223
+ }
224
+ async function getLanguageDirectionFromLocales(locale) {
225
+ try {
226
+ const localeData = (await (0, load_esm_1.loadEsmModule)(`@angular/common/locales/${locale}`)).default;
227
+ const dir = localeData[localeData.length - 2];
228
+ return isString(dir) ? dir : undefined;
229
+ }
230
+ catch {
231
+ // In some cases certain locales might map to files which are named only with language id.
232
+ // Example: `en-US` -> `en`.
233
+ const [languageId] = locale.split('-', 1);
234
+ if (languageId !== locale) {
235
+ return getLanguageDirectionFromLocales(languageId);
236
+ }
237
+ }
238
+ return undefined;
239
+ }
@@ -0,0 +1,11 @@
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.io/license
7
+ */
8
+ export declare function htmlRewritingStream(content: string): Promise<{
9
+ rewriter: import('parse5-html-rewriting-stream').RewritingStream;
10
+ transformedContent: () => Promise<string>;
11
+ }>;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright Google LLC All Rights Reserved.
5
+ *
6
+ * Use of this source code is governed by an MIT-style license that can be
7
+ * found in the LICENSE file at https://angular.io/license
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.htmlRewritingStream = void 0;
11
+ const node_stream_1 = require("node:stream");
12
+ const promises_1 = require("node:stream/promises");
13
+ const load_esm_1 = require("../load-esm");
14
+ async function htmlRewritingStream(content) {
15
+ const { RewritingStream } = await (0, load_esm_1.loadEsmModule)('parse5-html-rewriting-stream');
16
+ const rewriter = new RewritingStream();
17
+ return {
18
+ rewriter,
19
+ transformedContent: () => (0, promises_1.pipeline)(node_stream_1.Readable.from(content), rewriter, async function (source) {
20
+ const chunks = [];
21
+ for await (const chunk of source) {
22
+ chunks.push(Buffer.from(chunk));
23
+ }
24
+ return Buffer.concat(chunks).toString('utf-8');
25
+ }),
26
+ };
27
+ }
28
+ exports.htmlRewritingStream = htmlRewritingStream;
@@ -0,0 +1,57 @@
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.io/license
7
+ */
8
+ import { NormalizedCachedOptions } from '../normalize-cache';
9
+ import { NormalizedOptimizationOptions } from '../normalize-optimization';
10
+ import { CrossOriginValue, Entrypoint, FileInfo } from './augment-index-html';
11
+ export type HintMode = 'prefetch' | 'preload' | 'modulepreload' | 'preconnect' | 'dns-prefetch';
12
+ export interface IndexHtmlGeneratorProcessOptions {
13
+ lang: string | undefined;
14
+ baseHref: string | undefined;
15
+ outputPath: string;
16
+ files: FileInfo[];
17
+ hints?: {
18
+ url: string;
19
+ mode: HintMode;
20
+ as?: string;
21
+ }[];
22
+ }
23
+ export interface IndexHtmlGeneratorOptions {
24
+ indexPath: string;
25
+ deployUrl?: string;
26
+ sri?: boolean;
27
+ entrypoints: Entrypoint[];
28
+ postTransform?: IndexHtmlTransform;
29
+ crossOrigin?: CrossOriginValue;
30
+ optimization?: NormalizedOptimizationOptions;
31
+ cache?: NormalizedCachedOptions;
32
+ imageDomains?: string[];
33
+ generateDedicatedSSRContent?: boolean;
34
+ }
35
+ export type IndexHtmlTransform = (content: string) => Promise<string>;
36
+ export interface IndexHtmlPluginTransformResult {
37
+ content: string;
38
+ warnings: string[];
39
+ errors: string[];
40
+ }
41
+ export interface IndexHtmlProcessResult {
42
+ csrContent: string;
43
+ ssrContent?: string;
44
+ warnings: string[];
45
+ errors: string[];
46
+ }
47
+ export declare class IndexHtmlGenerator {
48
+ readonly options: IndexHtmlGeneratorOptions;
49
+ private readonly plugins;
50
+ private readonly csrPlugins;
51
+ private readonly ssrPlugins;
52
+ constructor(options: IndexHtmlGeneratorOptions);
53
+ process(options: IndexHtmlGeneratorProcessOptions): Promise<IndexHtmlProcessResult>;
54
+ private runPlugins;
55
+ readAsset(path: string): Promise<string>;
56
+ protected readIndex(path: string): Promise<string>;
57
+ }
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright Google LLC All Rights Reserved.
5
+ *
6
+ * Use of this source code is governed by an MIT-style license that can be
7
+ * found in the LICENSE file at https://angular.io/license
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.IndexHtmlGenerator = void 0;
11
+ const promises_1 = require("node:fs/promises");
12
+ const node_path_1 = require("node:path");
13
+ const add_event_dispatch_contract_1 = require("./add-event-dispatch-contract");
14
+ const augment_index_html_1 = require("./augment-index-html");
15
+ const inline_critical_css_1 = require("./inline-critical-css");
16
+ const inline_fonts_1 = require("./inline-fonts");
17
+ const nonce_1 = require("./nonce");
18
+ class IndexHtmlGenerator {
19
+ options;
20
+ plugins;
21
+ csrPlugins = [];
22
+ ssrPlugins = [];
23
+ constructor(options) {
24
+ this.options = options;
25
+ const extraCommonPlugins = [];
26
+ if (options?.optimization?.fonts.inline) {
27
+ extraCommonPlugins.push(inlineFontsPlugin(this), nonce_1.addNonce);
28
+ }
29
+ // Common plugins
30
+ this.plugins = [augmentIndexHtmlPlugin(this), ...extraCommonPlugins, postTransformPlugin(this)];
31
+ // CSR plugins
32
+ if (options?.optimization?.styles?.inlineCritical) {
33
+ this.csrPlugins.push(inlineCriticalCssPlugin(this));
34
+ }
35
+ // SSR plugins
36
+ if (options.generateDedicatedSSRContent) {
37
+ this.ssrPlugins.push(addEventDispatchContractPlugin(), addNoncePlugin());
38
+ }
39
+ }
40
+ async process(options) {
41
+ let content = await this.readIndex(this.options.indexPath);
42
+ const warnings = [];
43
+ const errors = [];
44
+ content = await this.runPlugins(content, this.plugins, options, warnings, errors);
45
+ const [csrContent, ssrContent] = await Promise.all([
46
+ this.runPlugins(content, this.csrPlugins, options, warnings, errors),
47
+ this.ssrPlugins.length
48
+ ? this.runPlugins(content, this.ssrPlugins, options, warnings, errors)
49
+ : undefined,
50
+ ]);
51
+ return {
52
+ ssrContent,
53
+ csrContent,
54
+ warnings,
55
+ errors,
56
+ };
57
+ }
58
+ async runPlugins(content, plugins, options, warnings, errors) {
59
+ for (const plugin of plugins) {
60
+ const result = await plugin(content, options);
61
+ if (typeof result === 'string') {
62
+ content = result;
63
+ }
64
+ else {
65
+ content = result.content;
66
+ if (result.warnings.length) {
67
+ warnings.push(...result.warnings);
68
+ }
69
+ if (result.errors.length) {
70
+ errors.push(...result.errors);
71
+ }
72
+ }
73
+ }
74
+ return content;
75
+ }
76
+ async readAsset(path) {
77
+ try {
78
+ return await (0, promises_1.readFile)(path, 'utf-8');
79
+ }
80
+ catch {
81
+ throw new Error(`Failed to read asset "${path}".`);
82
+ }
83
+ }
84
+ async readIndex(path) {
85
+ try {
86
+ return new TextDecoder('utf-8').decode(await (0, promises_1.readFile)(path));
87
+ }
88
+ catch (cause) {
89
+ throw new Error(`Failed to read index HTML file "${path}".`, { cause });
90
+ }
91
+ }
92
+ }
93
+ exports.IndexHtmlGenerator = IndexHtmlGenerator;
94
+ function augmentIndexHtmlPlugin(generator) {
95
+ const { deployUrl, crossOrigin, sri = false, entrypoints, imageDomains } = generator.options;
96
+ return async (html, options) => {
97
+ const { lang, baseHref, outputPath = '', files, hints } = options;
98
+ return (0, augment_index_html_1.augmentIndexHtml)({
99
+ html,
100
+ baseHref,
101
+ deployUrl,
102
+ crossOrigin,
103
+ sri,
104
+ lang,
105
+ entrypoints,
106
+ loadOutputFile: (filePath) => generator.readAsset((0, node_path_1.join)(outputPath, filePath)),
107
+ imageDomains,
108
+ files,
109
+ hints,
110
+ });
111
+ };
112
+ }
113
+ function inlineFontsPlugin({ options }) {
114
+ const inlineFontsProcessor = new inline_fonts_1.InlineFontsProcessor({
115
+ minify: options.optimization?.styles.minify,
116
+ });
117
+ return async (html) => inlineFontsProcessor.process(html);
118
+ }
119
+ function inlineCriticalCssPlugin(generator) {
120
+ const inlineCriticalCssProcessor = new inline_critical_css_1.InlineCriticalCssProcessor({
121
+ minify: generator.options.optimization?.styles.minify,
122
+ deployUrl: generator.options.deployUrl,
123
+ readAsset: (filePath) => generator.readAsset(filePath),
124
+ });
125
+ return async (html, options) => inlineCriticalCssProcessor.process(html, { outputPath: options.outputPath });
126
+ }
127
+ function addNoncePlugin() {
128
+ return (html) => (0, nonce_1.addNonce)(html);
129
+ }
130
+ function postTransformPlugin({ options }) {
131
+ return async (html) => (options.postTransform ? options.postTransform(html) : html);
132
+ }
133
+ function addEventDispatchContractPlugin() {
134
+ return (html) => (0, add_event_dispatch_contract_1.addEventDispatchContract)(html);
135
+ }
@@ -0,0 +1,24 @@
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.io/license
7
+ */
8
+ export interface InlineCriticalCssProcessOptions {
9
+ outputPath: string;
10
+ }
11
+ export interface InlineCriticalCssProcessorOptions {
12
+ minify?: boolean;
13
+ deployUrl?: string;
14
+ readAsset?: (path: string) => Promise<string>;
15
+ }
16
+ export declare class InlineCriticalCssProcessor {
17
+ protected readonly options: InlineCriticalCssProcessorOptions;
18
+ constructor(options: InlineCriticalCssProcessorOptions);
19
+ process(html: string, options: InlineCriticalCssProcessOptions): Promise<{
20
+ content: string;
21
+ warnings: string[];
22
+ errors: string[];
23
+ }>;
24
+ }
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright Google LLC All Rights Reserved.
5
+ *
6
+ * Use of this source code is governed by an MIT-style license that can be
7
+ * found in the LICENSE file at https://angular.io/license
8
+ */
9
+ var __importDefault = (this && this.__importDefault) || function (mod) {
10
+ return (mod && mod.__esModule) ? mod : { "default": mod };
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.InlineCriticalCssProcessor = void 0;
14
+ const critters_1 = __importDefault(require("critters"));
15
+ const promises_1 = require("node:fs/promises");
16
+ /**
17
+ * Pattern used to extract the media query set by Critters in an `onload` handler.
18
+ */
19
+ const MEDIA_SET_HANDLER_PATTERN = /^this\.media=["'](.*)["'];?$/;
20
+ /**
21
+ * Name of the attribute used to save the Critters media query so it can be re-assigned on load.
22
+ */
23
+ const CSP_MEDIA_ATTR = 'ngCspMedia';
24
+ /**
25
+ * Script text used to change the media value of the link tags.
26
+ *
27
+ * NOTE:
28
+ * We do not use `document.querySelectorAll('link').forEach((s) => s.addEventListener('load', ...)`
29
+ * because this does not always fire on Chome.
30
+ * See: https://github.com/angular/angular-cli/issues/26932 and https://crbug.com/1521256
31
+ */
32
+ const LINK_LOAD_SCRIPT_CONTENT = [
33
+ '(() => {',
34
+ ` const CSP_MEDIA_ATTR = '${CSP_MEDIA_ATTR}';`,
35
+ ' const documentElement = document.documentElement;',
36
+ ' const listener = (e) => {',
37
+ ' const target = e.target;',
38
+ ` if (!target || target.tagName !== 'LINK' || !target.hasAttribute(CSP_MEDIA_ATTR)) {`,
39
+ ' return;',
40
+ ' }',
41
+ ' target.media = target.getAttribute(CSP_MEDIA_ATTR);',
42
+ ' target.removeAttribute(CSP_MEDIA_ATTR);',
43
+ // Remove onload listener when there are no longer styles that need to be loaded.
44
+ ' if (!document.head.querySelector(`link[${CSP_MEDIA_ATTR}]`)) {',
45
+ ` documentElement.removeEventListener('load', listener);`,
46
+ ' }',
47
+ ' };',
48
+ // We use an event with capturing (the true parameter) because load events don't bubble.
49
+ ` documentElement.addEventListener('load', listener, true);`,
50
+ '})();',
51
+ ].join('\n');
52
+ class CrittersExtended extends critters_1.default {
53
+ optionsExtended;
54
+ warnings = [];
55
+ errors = [];
56
+ initialEmbedLinkedStylesheet;
57
+ addedCspScriptsDocuments = new WeakSet();
58
+ documentNonces = new WeakMap();
59
+ constructor(optionsExtended) {
60
+ super({
61
+ logger: {
62
+ warn: (s) => this.warnings.push(s),
63
+ error: (s) => this.errors.push(s),
64
+ info: () => { },
65
+ },
66
+ logLevel: 'warn',
67
+ path: optionsExtended.outputPath,
68
+ publicPath: optionsExtended.deployUrl,
69
+ compress: !!optionsExtended.minify,
70
+ pruneSource: false,
71
+ reduceInlineStyles: false,
72
+ mergeStylesheets: false,
73
+ // Note: if `preload` changes to anything other than `media`, the logic in
74
+ // `embedLinkedStylesheetOverride` will have to be updated.
75
+ preload: 'media',
76
+ noscriptFallback: true,
77
+ inlineFonts: true,
78
+ });
79
+ this.optionsExtended = optionsExtended;
80
+ // We can't use inheritance to override `embedLinkedStylesheet`, because it's not declared in
81
+ // the `Critters` .d.ts which means that we can't call the `super` implementation. TS doesn't
82
+ // allow for `super` to be cast to a different type.
83
+ this.initialEmbedLinkedStylesheet = this.embedLinkedStylesheet;
84
+ this.embedLinkedStylesheet = this.embedLinkedStylesheetOverride;
85
+ }
86
+ readFile(path) {
87
+ const readAsset = this.optionsExtended.readAsset;
88
+ return readAsset ? readAsset(path) : (0, promises_1.readFile)(path, 'utf-8');
89
+ }
90
+ /**
91
+ * Override of the Critters `embedLinkedStylesheet` method
92
+ * that makes it work with Angular's CSP APIs.
93
+ */
94
+ embedLinkedStylesheetOverride = async (link, document) => {
95
+ if (link.getAttribute('media') === 'print' && link.next?.name === 'noscript') {
96
+ // Workaround for https://github.com/GoogleChromeLabs/critters/issues/64
97
+ // NB: this is only needed for the webpack based builders.
98
+ const media = link.getAttribute('onload')?.match(MEDIA_SET_HANDLER_PATTERN);
99
+ if (media) {
100
+ link.removeAttribute('onload');
101
+ link.setAttribute('media', media[1]);
102
+ link?.next?.remove();
103
+ }
104
+ }
105
+ const returnValue = await this.initialEmbedLinkedStylesheet(link, document);
106
+ const cspNonce = this.findCspNonce(document);
107
+ if (cspNonce) {
108
+ const crittersMedia = link.getAttribute('onload')?.match(MEDIA_SET_HANDLER_PATTERN);
109
+ if (crittersMedia) {
110
+ // If there's a Critters-generated `onload` handler and the file has an Angular CSP nonce,
111
+ // we have to remove the handler, because it's incompatible with CSP. We save the value
112
+ // in a different attribute and we generate a script tag with the nonce that uses
113
+ // `addEventListener` to apply the media query instead.
114
+ link.removeAttribute('onload');
115
+ link.setAttribute(CSP_MEDIA_ATTR, crittersMedia[1]);
116
+ this.conditionallyInsertCspLoadingScript(document, cspNonce, link);
117
+ }
118
+ // Ideally we would hook in at the time Critters inserts the `style` tags, but there isn't
119
+ // a way of doing that at the moment so we fall back to doing it any time a `link` tag is
120
+ // inserted. We mitigate it by only iterating the direct children of the `<head>` which
121
+ // should be pretty shallow.
122
+ document.head.children.forEach((child) => {
123
+ if (child.tagName === 'style' && !child.hasAttribute('nonce')) {
124
+ child.setAttribute('nonce', cspNonce);
125
+ }
126
+ });
127
+ }
128
+ return returnValue;
129
+ };
130
+ /**
131
+ * Finds the CSP nonce for a specific document.
132
+ */
133
+ findCspNonce(document) {
134
+ if (this.documentNonces.has(document)) {
135
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
136
+ return this.documentNonces.get(document);
137
+ }
138
+ // HTML attribute are case-insensitive, but the parser used by Critters is case-sensitive.
139
+ const nonceElement = document.querySelector('[ngCspNonce], [ngcspnonce]');
140
+ const cspNonce = nonceElement?.getAttribute('ngCspNonce') || nonceElement?.getAttribute('ngcspnonce') || null;
141
+ this.documentNonces.set(document, cspNonce);
142
+ return cspNonce;
143
+ }
144
+ /**
145
+ * Inserts the `script` tag that swaps the critical CSS at runtime,
146
+ * if one hasn't been inserted into the document already.
147
+ */
148
+ conditionallyInsertCspLoadingScript(document, nonce, link) {
149
+ if (this.addedCspScriptsDocuments.has(document)) {
150
+ return;
151
+ }
152
+ const script = document.createElement('script');
153
+ script.setAttribute('nonce', nonce);
154
+ script.textContent = LINK_LOAD_SCRIPT_CONTENT;
155
+ // Prepend the script to the head since it needs to
156
+ // run as early as possible, before the `link` tags.
157
+ document.head.insertBefore(script, link);
158
+ this.addedCspScriptsDocuments.add(document);
159
+ }
160
+ }
161
+ class InlineCriticalCssProcessor {
162
+ options;
163
+ constructor(options) {
164
+ this.options = options;
165
+ }
166
+ async process(html, options) {
167
+ const critters = new CrittersExtended({ ...this.options, ...options });
168
+ const content = await critters.process(html);
169
+ return {
170
+ // Clean up value from value less attributes.
171
+ // This is caused because parse5 always requires attributes to have a string value.
172
+ // nomodule="" defer="" -> nomodule defer.
173
+ content: content.replace(/(\s(?:defer|nomodule))=""/g, '$1'),
174
+ errors: critters.errors,
175
+ warnings: critters.warnings,
176
+ };
177
+ }
178
+ }
179
+ exports.InlineCriticalCssProcessor = InlineCriticalCssProcessor;