|
| 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 | + |
| 9 | +import type { OutputFile, PluginBuild } from 'esbuild'; |
| 10 | +import { readFile } from 'node:fs/promises'; |
| 11 | +import path from 'node:path'; |
| 12 | +import { BundleStylesheetOptions, bundleComponentStylesheet } from '../stylesheets'; |
| 13 | +import { |
| 14 | + JIT_NAMESPACE_REGEXP, |
| 15 | + JIT_STYLE_NAMESPACE, |
| 16 | + JIT_TEMPLATE_NAMESPACE, |
| 17 | + parseJitUri, |
| 18 | +} from './uri'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Loads/extracts the contents from a load callback Angular JIT entry. |
| 22 | + * An Angular JIT entry represents either a file path for a component resource or base64 |
| 23 | + * encoded data for an inline component resource. |
| 24 | + * @param entry The value that represents content to load. |
| 25 | + * @param root The absolute path for the root of the build (typically the workspace root). |
| 26 | + * @param skipRead If true, do not attempt to read the file; if false, read file content from disk. |
| 27 | + * This option has no effect if the entry does not originate from a file. Defaults to false. |
| 28 | + * @returns An object containing the absolute path of the contents and optionally the actual contents. |
| 29 | + * For inline entries the contents will always be provided. |
| 30 | + */ |
| 31 | +async function loadEntry( |
| 32 | + entry: string, |
| 33 | + root: string, |
| 34 | + skipRead?: boolean, |
| 35 | +): Promise<{ path: string; contents?: string }> { |
| 36 | + if (entry.startsWith('file:')) { |
| 37 | + const specifier = path.join(root, entry.slice(5)); |
| 38 | + |
| 39 | + return { |
| 40 | + path: specifier, |
| 41 | + contents: skipRead ? undefined : await readFile(specifier, 'utf-8'), |
| 42 | + }; |
| 43 | + } else if (entry.startsWith('inline:')) { |
| 44 | + const [importer, data] = entry.slice(7).split(';', 2); |
| 45 | + |
| 46 | + return { |
| 47 | + path: path.join(root, importer), |
| 48 | + contents: Buffer.from(data, 'base64').toString(), |
| 49 | + }; |
| 50 | + } else { |
| 51 | + throw new Error('Invalid data for Angular JIT entry.'); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Sets up esbuild resolve and load callbacks to support Angular JIT mode processing |
| 57 | + * for both Component stylesheets and templates. These callbacks work alongside the JIT |
| 58 | + * resource TypeScript transformer to convert and then bundle Component resources as |
| 59 | + * static imports. |
| 60 | + * @param build An esbuild {@link PluginBuild} instance used to add callbacks. |
| 61 | + * @param styleOptions The options to use when bundling stylesheets. |
| 62 | + * @param stylesheetResourceFiles An array where stylesheet resources will be added. |
| 63 | + */ |
| 64 | +export function setupJitPluginCallbacks( |
| 65 | + build: PluginBuild, |
| 66 | + styleOptions: BundleStylesheetOptions & { inlineStyleLanguage: string }, |
| 67 | + stylesheetResourceFiles: OutputFile[], |
| 68 | +): void { |
| 69 | + const root = build.initialOptions.absWorkingDir ?? ''; |
| 70 | + |
| 71 | + // Add a resolve callback to capture and parse any JIT URIs that were added by the |
| 72 | + // JIT resource TypeScript transformer. |
| 73 | + // Resources originating from a file are resolved as relative from the containing file (importer). |
| 74 | + build.onResolve({ filter: JIT_NAMESPACE_REGEXP }, (args) => { |
| 75 | + const parsed = parseJitUri(args.path); |
| 76 | + if (!parsed) { |
| 77 | + return undefined; |
| 78 | + } |
| 79 | + |
| 80 | + const { namespace, origin, specifier } = parsed; |
| 81 | + |
| 82 | + if (origin === 'file') { |
| 83 | + return { |
| 84 | + // Use a relative path to prevent fully resolved paths in the metafile (JSON stats file). |
| 85 | + // This is only necessary for custom namespaces. esbuild will handle the file namespace. |
| 86 | + path: 'file:' + path.relative(root, path.join(path.dirname(args.importer), specifier)), |
| 87 | + namespace, |
| 88 | + }; |
| 89 | + } else { |
| 90 | + // Inline data may need the importer to resolve imports/references within the content |
| 91 | + const importer = path.relative(root, args.importer); |
| 92 | + |
| 93 | + return { |
| 94 | + path: `inline:${importer};${specifier}`, |
| 95 | + namespace, |
| 96 | + }; |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + // Add a load callback to handle Component stylesheets (both inline and external) |
| 101 | + build.onLoad({ filter: /./, namespace: JIT_STYLE_NAMESPACE }, async (args) => { |
| 102 | + // skipRead is used here because the stylesheet bundling will read a file stylesheet |
| 103 | + // directly either via a preprocessor or esbuild itself. |
| 104 | + const entry = await loadEntry(args.path, root, true /* skipRead */); |
| 105 | + |
| 106 | + const { contents, resourceFiles, errors, warnings } = await bundleComponentStylesheet( |
| 107 | + styleOptions.inlineStyleLanguage, |
| 108 | + // The `data` parameter is only needed for a stylesheet if it was inline |
| 109 | + entry.contents ?? '', |
| 110 | + entry.path, |
| 111 | + entry.contents !== undefined, |
| 112 | + styleOptions, |
| 113 | + ); |
| 114 | + |
| 115 | + stylesheetResourceFiles.push(...resourceFiles); |
| 116 | + |
| 117 | + return { |
| 118 | + errors, |
| 119 | + warnings, |
| 120 | + contents, |
| 121 | + loader: 'text', |
| 122 | + }; |
| 123 | + }); |
| 124 | + |
| 125 | + // Add a load callback to handle Component templates |
| 126 | + // NOTE: While this callback supports both inline and external templates, the transformer |
| 127 | + // currently only supports generating URIs for external templates. |
| 128 | + build.onLoad({ filter: /./, namespace: JIT_TEMPLATE_NAMESPACE }, async (args) => { |
| 129 | + const { contents } = await loadEntry(args.path, root); |
| 130 | + |
| 131 | + return { |
| 132 | + contents, |
| 133 | + loader: 'text', |
| 134 | + }; |
| 135 | + }); |
| 136 | +} |
0 commit comments