|
| 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 { OnLoadResult, Plugin, PluginBuild } from 'esbuild'; |
| 10 | +import assert from 'node:assert'; |
| 11 | +import { readFile } from 'node:fs/promises'; |
| 12 | + |
| 13 | +/** |
| 14 | + * The lazy-loaded instance of the less stylesheet preprocessor. |
| 15 | + * It is only imported and initialized if a less stylesheet is used. |
| 16 | + */ |
| 17 | +let lessPreprocessor: typeof import('less') | undefined; |
| 18 | + |
| 19 | +export interface LessPluginOptions { |
| 20 | + sourcemap: boolean; |
| 21 | + includePaths?: string[]; |
| 22 | + inlineComponentData?: Record<string, string>; |
| 23 | +} |
| 24 | + |
| 25 | +interface LessException extends Error { |
| 26 | + filename: string; |
| 27 | + line: number; |
| 28 | + column: number; |
| 29 | + extract?: string[]; |
| 30 | +} |
| 31 | + |
| 32 | +function isLessException(error: unknown): error is LessException { |
| 33 | + return !!error && typeof error === 'object' && 'column' in error; |
| 34 | +} |
| 35 | + |
| 36 | +export function createLessPlugin(options: LessPluginOptions): Plugin { |
| 37 | + return { |
| 38 | + name: 'angular-less', |
| 39 | + setup(build: PluginBuild): void { |
| 40 | + // Add a load callback to support inline Component styles |
| 41 | + build.onLoad({ filter: /^less;/, namespace: 'angular:styles/component' }, async (args) => { |
| 42 | + const data = options.inlineComponentData?.[args.path]; |
| 43 | + assert(data, `component style name should always be found [${args.path}]`); |
| 44 | + |
| 45 | + const [, , filePath] = args.path.split(';', 3); |
| 46 | + |
| 47 | + return compileString(data, filePath, options); |
| 48 | + }); |
| 49 | + |
| 50 | + // Add a load callback to support files from disk |
| 51 | + build.onLoad({ filter: /\.less$/ }, async (args) => { |
| 52 | + const data = await readFile(args.path, 'utf-8'); |
| 53 | + |
| 54 | + return compileString(data, args.path, options); |
| 55 | + }); |
| 56 | + }, |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +async function compileString( |
| 61 | + data: string, |
| 62 | + filename: string, |
| 63 | + options: LessPluginOptions, |
| 64 | +): Promise<OnLoadResult> { |
| 65 | + const less = (lessPreprocessor ??= (await import('less')).default); |
| 66 | + |
| 67 | + try { |
| 68 | + const result = await less.render(data, { |
| 69 | + filename, |
| 70 | + paths: options.includePaths, |
| 71 | + rewriteUrls: 'all', |
| 72 | + sourceMap: options.sourcemap |
| 73 | + ? { |
| 74 | + sourceMapFileInline: true, |
| 75 | + outputSourceFiles: true, |
| 76 | + } |
| 77 | + : undefined, |
| 78 | + } as Less.Options); |
| 79 | + |
| 80 | + return { |
| 81 | + contents: result.css, |
| 82 | + loader: 'css', |
| 83 | + }; |
| 84 | + } catch (error) { |
| 85 | + if (isLessException(error)) { |
| 86 | + return { |
| 87 | + errors: [ |
| 88 | + { |
| 89 | + text: error.message, |
| 90 | + location: { |
| 91 | + file: error.filename, |
| 92 | + line: error.line, |
| 93 | + column: error.column, |
| 94 | + // Middle element represents the line containing the error |
| 95 | + lineText: error.extract && error.extract[Math.trunc(error.extract.length / 2)], |
| 96 | + }, |
| 97 | + }, |
| 98 | + ], |
| 99 | + loader: 'css', |
| 100 | + }; |
| 101 | + } |
| 102 | + |
| 103 | + throw error; |
| 104 | + } |
| 105 | +} |
0 commit comments