|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import type { Plugin, PluginBuild } from 'esbuild'; |
10 | | -import type { LegacyResult } from 'sass'; |
11 | | -import { SassWorkerImplementation } from '../../sass/sass-service'; |
| 9 | +import type { PartialMessage, Plugin, PluginBuild } from 'esbuild'; |
| 10 | +import type { CompileResult } from 'sass'; |
| 11 | +import { fileURLToPath } from 'url'; |
12 | 12 |
|
13 | | -export function createSassPlugin(options: { sourcemap: boolean; includePaths?: string[] }): Plugin { |
| 13 | +export function createSassPlugin(options: { sourcemap: boolean; loadPaths?: string[] }): Plugin { |
14 | 14 | return { |
15 | 15 | name: 'angular-sass', |
16 | 16 | setup(build: PluginBuild): void { |
17 | | - let sass: SassWorkerImplementation; |
| 17 | + let sass: typeof import('sass'); |
18 | 18 |
|
19 | | - build.onStart(() => { |
20 | | - sass = new SassWorkerImplementation(); |
| 19 | + build.onStart(async () => { |
| 20 | + // Lazily load Sass |
| 21 | + sass = await import('sass'); |
21 | 22 | }); |
22 | 23 |
|
23 | | - build.onEnd(() => { |
24 | | - sass?.close(); |
25 | | - }); |
26 | | - |
27 | | - build.onLoad({ filter: /\.s[ac]ss$/ }, async (args) => { |
28 | | - const result = await new Promise<LegacyResult>((resolve, reject) => { |
29 | | - sass.render( |
30 | | - { |
31 | | - file: args.path, |
32 | | - includePaths: options.includePaths, |
33 | | - indentedSyntax: args.path.endsWith('.sass'), |
34 | | - outputStyle: 'expanded', |
35 | | - sourceMap: options.sourcemap, |
36 | | - sourceMapContents: options.sourcemap, |
37 | | - sourceMapEmbed: options.sourcemap, |
38 | | - quietDeps: true, |
39 | | - }, |
40 | | - (error, result) => { |
41 | | - if (error) { |
42 | | - reject(error); |
43 | | - } |
44 | | - if (result) { |
45 | | - resolve(result); |
46 | | - } |
| 24 | + build.onLoad({ filter: /\.s[ac]ss$/ }, (args) => { |
| 25 | + try { |
| 26 | + const warnings: PartialMessage[] = []; |
| 27 | + // Use sync version as async version is slower. |
| 28 | + const { css, sourceMap, loadedUrls } = sass.compile(args.path, { |
| 29 | + style: 'expanded', |
| 30 | + loadPaths: options.loadPaths, |
| 31 | + sourceMap: options.sourcemap, |
| 32 | + sourceMapIncludeSources: options.sourcemap, |
| 33 | + quietDeps: true, |
| 34 | + logger: { |
| 35 | + warn: (text, _options) => { |
| 36 | + warnings.push({ |
| 37 | + text, |
| 38 | + }); |
| 39 | + }, |
47 | 40 | }, |
48 | | - ); |
49 | | - }); |
50 | | - |
51 | | - return { |
52 | | - contents: result.css, |
53 | | - loader: 'css', |
54 | | - watchFiles: result.stats.includedFiles, |
55 | | - }; |
| 41 | + }); |
| 42 | + |
| 43 | + return { |
| 44 | + loader: 'css', |
| 45 | + contents: css + sourceMapToUrlComment(sourceMap), |
| 46 | + watchFiles: loadedUrls.map((url) => fileURLToPath(url)), |
| 47 | + warnings, |
| 48 | + }; |
| 49 | + } catch (error) { |
| 50 | + if (error instanceof sass.Exception) { |
| 51 | + const file = error.span.url ? fileURLToPath(error.span.url) : undefined; |
| 52 | + |
| 53 | + return { |
| 54 | + loader: 'css', |
| 55 | + errors: [ |
| 56 | + { |
| 57 | + text: error.toString(), |
| 58 | + }, |
| 59 | + ], |
| 60 | + watchFiles: file ? [file] : undefined, |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + throw error; |
| 65 | + } |
56 | 66 | }); |
57 | 67 | }, |
58 | 68 | }; |
59 | 69 | } |
| 70 | + |
| 71 | +function sourceMapToUrlComment(sourceMap: CompileResult['sourceMap']): string { |
| 72 | + if (!sourceMap) { |
| 73 | + return ''; |
| 74 | + } |
| 75 | + |
| 76 | + const urlSourceMap = Buffer.from(JSON.stringify(sourceMap), 'utf-8').toString('base64'); |
| 77 | + |
| 78 | + return `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${urlSourceMap}`; |
| 79 | +} |
0 commit comments