|
| 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.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import mergeSourceMaps, { type DecodedSourceMap, type RawSourceMap } from '@ampproject/remapping'; |
| 10 | +import { dirname } from 'node:path'; |
| 11 | +import { fileURLToPath } from 'node:url'; |
| 12 | +import type { |
| 13 | + AsyncCompiler, |
| 14 | + CanonicalizeContext, |
| 15 | + CompileResult, |
| 16 | + FileImporter, |
| 17 | + Importer, |
| 18 | + NodePackageImporter, |
| 19 | + StringOptions, |
| 20 | +} from 'sass-embedded'; |
| 21 | +import { |
| 22 | + AsyncModuleUrlRebasingImporter, |
| 23 | + DirectoryEntry, |
| 24 | + LoadPathsUrlRebasingImporter, |
| 25 | + RelativeUrlRebasingImporter, |
| 26 | +} from './rebasing-importer'; |
| 27 | +import { type SassServiceImplementation, isFileImporter } from './sass-service'; |
| 28 | + |
| 29 | +/** |
| 30 | + * A Sass renderer implementation that uses the persistent Dart Sass embedded compiler |
| 31 | + * daemon (`sass-embedded`) communicating over standard input/output with protocol buffers. |
| 32 | + */ |
| 33 | +export class SassAsyncCompilerImplementation implements SassServiceImplementation { |
| 34 | + #asyncCompiler: AsyncCompiler | undefined; |
| 35 | + #asyncCompilerPromise: Promise<AsyncCompiler> | undefined; |
| 36 | + |
| 37 | + async #ensureAsyncCompiler(): Promise<AsyncCompiler> { |
| 38 | + if (this.#asyncCompiler) { |
| 39 | + return this.#asyncCompiler; |
| 40 | + } |
| 41 | + |
| 42 | + // Import and initialize the async compiler on the main thread. |
| 43 | + this.#asyncCompilerPromise ??= import('sass-embedded').then(({ initAsyncCompiler }) => |
| 44 | + initAsyncCompiler(), |
| 45 | + ); |
| 46 | + |
| 47 | + try { |
| 48 | + this.#asyncCompiler = await this.#asyncCompilerPromise; |
| 49 | + } finally { |
| 50 | + this.#asyncCompilerPromise = undefined; |
| 51 | + } |
| 52 | + |
| 53 | + return this.#asyncCompiler; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Provides information about the Sass implementation. |
| 58 | + * This mimics enough of the `sass-embedded` value to be used with the `sass-loader`. |
| 59 | + */ |
| 60 | + get info(): string { |
| 61 | + return 'sass-embedded\tasync-compiler'; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * The synchronous render function is not used by the `sass-loader`. |
| 66 | + */ |
| 67 | + compileString(): never { |
| 68 | + throw new Error('Sass compileString is not supported.'); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Asynchronously request a Sass stylesheet to be rendered using the native embedded compiler. |
| 73 | + * |
| 74 | + * @param source The contents to compile. |
| 75 | + * @param options The `sass-embedded` options to use when rendering the stylesheet. |
| 76 | + */ |
| 77 | + async compileStringAsync( |
| 78 | + source: string, |
| 79 | + options: StringOptions<'async'>, |
| 80 | + ): Promise<CompileResult> { |
| 81 | + // The CLI's configuration does not use or expose the ability to define custom Sass functions |
| 82 | + if (options.functions && Object.keys(options.functions).length > 0) { |
| 83 | + throw new Error('Sass custom functions are not supported.'); |
| 84 | + } |
| 85 | + |
| 86 | + const { functions, importers, importer, url, logger, ...serializableOptions } = options; |
| 87 | + |
| 88 | + let finalImporters: |
| 89 | + (Importer<'async'> | FileImporter<'async'> | NodePackageImporter)[] | undefined; |
| 90 | + let loadPaths = options.loadPaths; |
| 91 | + const entryDirectory = url ? dirname(fileURLToPath(url)) : process.cwd(); |
| 92 | + const directoryCache = new Map<string, DirectoryEntry>(); |
| 93 | + const rebaseSourceMaps = options.sourceMap ? new Map<string, DecodedSourceMap>() : undefined; |
| 94 | + |
| 95 | + if (importers?.length) { |
| 96 | + for (const importer of importers) { |
| 97 | + if (!isFileImporter(importer)) { |
| 98 | + throw new Error('Only File Importers are supported.'); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + finalImporters = [ |
| 103 | + new AsyncModuleUrlRebasingImporter( |
| 104 | + entryDirectory, |
| 105 | + directoryCache, |
| 106 | + rebaseSourceMaps, |
| 107 | + async (specifier: string, options: CanonicalizeContext): Promise<URL | null> => { |
| 108 | + for (const importer of importers) { |
| 109 | + const result = await (importer as FileImporter<'async'>).findFileUrl( |
| 110 | + specifier, |
| 111 | + options, |
| 112 | + ); |
| 113 | + if (result) { |
| 114 | + return result; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return null; |
| 119 | + }, |
| 120 | + ), |
| 121 | + ]; |
| 122 | + } |
| 123 | + |
| 124 | + if (loadPaths?.length) { |
| 125 | + finalImporters ??= []; |
| 126 | + finalImporters.push( |
| 127 | + new LoadPathsUrlRebasingImporter( |
| 128 | + entryDirectory, |
| 129 | + directoryCache, |
| 130 | + rebaseSourceMaps, |
| 131 | + loadPaths, |
| 132 | + ), |
| 133 | + ); |
| 134 | + loadPaths = undefined; |
| 135 | + } |
| 136 | + |
| 137 | + const relativeImporter = new RelativeUrlRebasingImporter( |
| 138 | + entryDirectory, |
| 139 | + directoryCache, |
| 140 | + rebaseSourceMaps, |
| 141 | + ); |
| 142 | + |
| 143 | + const compiler = await this.#ensureAsyncCompiler(); |
| 144 | + const result = await compiler.compileStringAsync(source, { |
| 145 | + ...serializableOptions, |
| 146 | + url, |
| 147 | + loadPaths, |
| 148 | + importers: finalImporters, |
| 149 | + importer: relativeImporter, |
| 150 | + logger, |
| 151 | + }); |
| 152 | + |
| 153 | + if (result.sourceMap && rebaseSourceMaps?.size) { |
| 154 | + result.sourceMap = mergeSourceMaps( |
| 155 | + result.sourceMap as unknown as RawSourceMap, |
| 156 | + (file, context) => (file !== context.importer ? rebaseSourceMaps.get(file) : null), |
| 157 | + ) as unknown as typeof result.sourceMap; |
| 158 | + } |
| 159 | + |
| 160 | + return result; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Shutdown the native embedded Sass compiler daemon. |
| 165 | + * @returns A void promise that resolves when closing is complete. |
| 166 | + */ |
| 167 | + async close(): Promise<void> { |
| 168 | + if (this.#asyncCompilerPromise) { |
| 169 | + try { |
| 170 | + await this.#ensureAsyncCompiler(); |
| 171 | + } catch { |
| 172 | + // Ignore compiler initialization failures on shutdown |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + if (this.#asyncCompiler) { |
| 177 | + const compiler = this.#asyncCompiler; |
| 178 | + this.#asyncCompiler = undefined; |
| 179 | + await compiler.dispose(); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments