|
| 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 createAutoPrefixerPlugin from 'autoprefixer'; |
| 10 | +import type { OnLoadResult, Plugin, PluginBuild } from 'esbuild'; |
| 11 | +import assert from 'node:assert'; |
| 12 | +import { readFile } from 'node:fs/promises'; |
| 13 | + |
| 14 | +/** |
| 15 | + * The lazy-loaded instance of the postcss stylesheet postprocessor. |
| 16 | + * It is only imported and initialized if postcss is needed. |
| 17 | + */ |
| 18 | +let postcss: typeof import('postcss')['default'] | undefined; |
| 19 | + |
| 20 | +/** |
| 21 | + * An object containing the plugin options to use when processing CSS stylesheets. |
| 22 | + */ |
| 23 | +export interface CssPluginOptions { |
| 24 | + /** |
| 25 | + * Controls the use and creation of sourcemaps when processing the stylesheets. |
| 26 | + * If true, sourcemap processing is enabled; if false, disabled. |
| 27 | + */ |
| 28 | + sourcemap: boolean; |
| 29 | + /** |
| 30 | + * Optional component data for any inline styles from Component decorator `styles` fields. |
| 31 | + * The key is an internal angular resource URI and the value is the stylesheet content. |
| 32 | + */ |
| 33 | + inlineComponentData?: Record<string, string>; |
| 34 | + /** |
| 35 | + * The browsers to support in browserslist format when processing stylesheets. |
| 36 | + * Some postcss plugins such as autoprefixer require the raw browserslist information instead |
| 37 | + * of the esbuild formatted target. |
| 38 | + */ |
| 39 | + browsers: string[]; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Creates an esbuild plugin to process CSS stylesheets. |
| 44 | + * @param options An object containing the plugin options. |
| 45 | + * @returns An esbuild Plugin instance. |
| 46 | + */ |
| 47 | +export function createCssPlugin(options: CssPluginOptions): Plugin { |
| 48 | + return { |
| 49 | + name: 'angular-css', |
| 50 | + async setup(build: PluginBuild): Promise<void> { |
| 51 | + const autoprefixer = createAutoPrefixerPlugin({ |
| 52 | + overrideBrowserslist: options.browsers, |
| 53 | + ignoreUnknownVersions: true, |
| 54 | + }); |
| 55 | + |
| 56 | + // Autoprefixer currently does not contain a method to check if autoprefixer is required |
| 57 | + // based on the provided list of browsers. However, it does contain a method that returns |
| 58 | + // informational text that can be used as a replacement. The text "Awesome!" will be present |
| 59 | + // when autoprefixer determines no actions are needed. |
| 60 | + // ref: https://github.com/postcss/autoprefixer/blob/e2f5c26ff1f3eaca95a21873723ce1cdf6e59f0e/lib/info.js#L118 |
| 61 | + const autoprefixerInfo = autoprefixer.info({ from: build.initialOptions.absWorkingDir }); |
| 62 | + const skipAutoprefixer = autoprefixerInfo.includes('Awesome!'); |
| 63 | + |
| 64 | + if (skipAutoprefixer) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + postcss ??= (await import('postcss')).default; |
| 69 | + const postcssProcessor = postcss([autoprefixer]); |
| 70 | + |
| 71 | + // Add a load callback to support inline Component styles |
| 72 | + build.onLoad({ filter: /^css;/, namespace: 'angular:styles/component' }, async (args) => { |
| 73 | + const data = options.inlineComponentData?.[args.path]; |
| 74 | + assert(data, `component style name should always be found [${args.path}]`); |
| 75 | + |
| 76 | + const [, , filePath] = args.path.split(';', 3); |
| 77 | + |
| 78 | + return compileString(data, filePath, postcssProcessor, options); |
| 79 | + }); |
| 80 | + |
| 81 | + // Add a load callback to support files from disk |
| 82 | + build.onLoad({ filter: /\.css$/ }, async (args) => { |
| 83 | + const data = await readFile(args.path, 'utf-8'); |
| 84 | + |
| 85 | + return compileString(data, args.path, postcssProcessor, options); |
| 86 | + }); |
| 87 | + }, |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Compiles the provided CSS stylesheet data using a provided postcss processor and provides an |
| 93 | + * esbuild load result that can be used directly by an esbuild Plugin. |
| 94 | + * @param data The stylesheet content to process. |
| 95 | + * @param filename The name of the file that contains the data. |
| 96 | + * @param postcssProcessor A postcss processor instance to use. |
| 97 | + * @param options The plugin options to control the processing. |
| 98 | + * @returns An esbuild OnLoaderResult object with the processed content, warnings, and/or errors. |
| 99 | + */ |
| 100 | +async function compileString( |
| 101 | + data: string, |
| 102 | + filename: string, |
| 103 | + postcssProcessor: import('postcss').Processor, |
| 104 | + options: CssPluginOptions, |
| 105 | +): Promise<OnLoadResult> { |
| 106 | + try { |
| 107 | + const result = await postcssProcessor.process(data, { |
| 108 | + from: filename, |
| 109 | + to: filename, |
| 110 | + map: options.sourcemap && { |
| 111 | + inline: true, |
| 112 | + sourcesContent: true, |
| 113 | + }, |
| 114 | + }); |
| 115 | + |
| 116 | + const rawWarnings = result.warnings(); |
| 117 | + let warnings; |
| 118 | + if (rawWarnings.length > 0) { |
| 119 | + const lineMappings = new Map<string, string[] | null>(); |
| 120 | + warnings = rawWarnings.map((warning) => { |
| 121 | + const file = warning.node.source?.input.file; |
| 122 | + if (file === undefined) { |
| 123 | + return { text: warning.text }; |
| 124 | + } |
| 125 | + |
| 126 | + let lines = lineMappings.get(file); |
| 127 | + if (lines === undefined) { |
| 128 | + lines = warning.node.source?.input.css.split(/\r?\n/); |
| 129 | + lineMappings.set(file, lines ?? null); |
| 130 | + } |
| 131 | + |
| 132 | + return { |
| 133 | + text: warning.text, |
| 134 | + location: { |
| 135 | + file, |
| 136 | + line: warning.line, |
| 137 | + column: warning.column - 1, |
| 138 | + lineText: lines?.[warning.line - 1], |
| 139 | + }, |
| 140 | + }; |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + return { |
| 145 | + contents: result.css, |
| 146 | + loader: 'css', |
| 147 | + warnings, |
| 148 | + }; |
| 149 | + } catch (error) { |
| 150 | + postcss ??= (await import('postcss')).default; |
| 151 | + if (error instanceof postcss.CssSyntaxError) { |
| 152 | + const lines = error.source?.split(/\r?\n/); |
| 153 | + |
| 154 | + return { |
| 155 | + errors: [ |
| 156 | + { |
| 157 | + text: error.reason, |
| 158 | + location: { |
| 159 | + file: error.file, |
| 160 | + line: error.line, |
| 161 | + column: error.column && error.column - 1, |
| 162 | + lineText: error.line === undefined ? undefined : lines?.[error.line - 1], |
| 163 | + }, |
| 164 | + }, |
| 165 | + ], |
| 166 | + }; |
| 167 | + } |
| 168 | + |
| 169 | + throw error; |
| 170 | + } |
| 171 | +} |
0 commit comments