|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { isPackageUrl } from './sass-language'; |
| 9 | +import type { PluginBuild } from 'esbuild'; |
| 10 | +import { statSync } from 'node:fs'; |
| 11 | +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; |
| 12 | +import { tmpdir } from 'node:os'; |
| 13 | +import { dirname, join } from 'node:path'; |
| 14 | +import { pathToFileURL } from 'node:url'; |
| 15 | +import type { FileImporter } from 'sass-embedded'; |
| 16 | +import { SassCompiler } from '../../sass/sass-service'; |
| 17 | +import { |
| 18 | + SassStylesheetLanguage, |
| 19 | + isPackageUrl, |
| 20 | + resetSassWorkerPoolCaches, |
| 21 | + shutdownSassWorkerPool, |
| 22 | +} from './sass-language'; |
10 | 23 |
|
11 | 24 | describe('sass-language', () => { |
12 | 25 | describe('isPackageUrl', () => { |
@@ -46,4 +59,168 @@ describe('sass-language', () => { |
46 | 59 | expect(isPackageUrl('')).toBeFalse(); |
47 | 60 | }); |
48 | 61 | }); |
| 62 | + |
| 63 | + describe('package resolution caching', () => { |
| 64 | + let temporaryRoot: string; |
| 65 | + let projectRoot: string; |
| 66 | + let buttonStylesheet: string; |
| 67 | + let cardStylesheet: string; |
| 68 | + let dependencyStylesheet: string; |
| 69 | + let resolveRequests: string[]; |
| 70 | + |
| 71 | + /** |
| 72 | + * Creates a build stub that resolves a package specifier by searching the `node_modules` |
| 73 | + * directories visible from the resolve directory, which is how esbuild resolves the |
| 74 | + * package specifiers of a stylesheet. |
| 75 | + */ |
| 76 | + function createBuildStub(): PluginBuild { |
| 77 | + return { |
| 78 | + initialOptions: { absWorkingDir: projectRoot }, |
| 79 | + resolve: async (path: string, options: { resolveDir: string }) => { |
| 80 | + resolveRequests.push(`${options.resolveDir}:${path}`); |
| 81 | + |
| 82 | + for (let directory = options.resolveDir; ; directory = dirname(directory)) { |
| 83 | + // A package specifier resolves to the index file of the package, and an explicit file |
| 84 | + // within it to that file. A deeper subpath is left unresolved, as esbuild leaves one |
| 85 | + // that the `exports` of the package does not name; the Sass importer then resolves it |
| 86 | + // against the package root instead. |
| 87 | + for (const candidate of [ |
| 88 | + join(directory, 'node_modules', path, '_index.scss'), |
| 89 | + join(directory, 'node_modules', path), |
| 90 | + ]) { |
| 91 | + if (statSync(candidate, { throwIfNoEntry: false })?.isFile()) { |
| 92 | + return { path: candidate, errors: [], warnings: [] }; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (dirname(directory) === directory) { |
| 97 | + return { path: undefined, errors: [], warnings: [] }; |
| 98 | + } |
| 99 | + } |
| 100 | + }, |
| 101 | + } as unknown as PluginBuild; |
| 102 | + } |
| 103 | + |
| 104 | + async function compile(stylesheet: string, source = "@use 'theme';"): Promise<string> { |
| 105 | + const result = await SassStylesheetLanguage.process?.( |
| 106 | + source, |
| 107 | + stylesheet, |
| 108 | + 'scss', |
| 109 | + { sourcemap: false }, |
| 110 | + createBuildStub(), |
| 111 | + ); |
| 112 | + if (!result) { |
| 113 | + throw new Error('The Sass stylesheet language has no process function.'); |
| 114 | + } |
| 115 | + |
| 116 | + if (result.errors?.length) { |
| 117 | + return `error: ${result.errors[0].text}`; |
| 118 | + } |
| 119 | + |
| 120 | + return (result.contents as string).trim(); |
| 121 | + } |
| 122 | + |
| 123 | + async function writePackage(directory: string, marker: string): Promise<void> { |
| 124 | + await mkdir(join(directory, 'sub'), { recursive: true }); |
| 125 | + await writeFile(join(directory, 'package.json'), '{}'); |
| 126 | + await writeFile(join(directory, '_index.scss'), `.marker { content: "${marker}"; }`); |
| 127 | + await writeFile( |
| 128 | + join(directory, 'sub', '_other.scss'), |
| 129 | + `.deep { content: "${marker} deep"; }`, |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + beforeAll(async () => { |
| 134 | + temporaryRoot = await mkdtemp(join(tmpdir(), 'angular-cli-sass-language-')); |
| 135 | + projectRoot = join(temporaryRoot, 'project'); |
| 136 | + const dependencyRoot = join(projectRoot, 'node_modules', 'dependency'); |
| 137 | + |
| 138 | + // An application using a `theme` package, and a dependency with its own nested version of |
| 139 | + // `theme` plus an `extra` package that only the dependency can see. |
| 140 | + await writePackage(join(projectRoot, 'node_modules', 'theme'), 'project'); |
| 141 | + await writePackage(join(dependencyRoot, 'node_modules', 'theme'), 'dependency'); |
| 142 | + await writePackage(join(dependencyRoot, 'node_modules', 'extra'), 'extra'); |
| 143 | + |
| 144 | + buttonStylesheet = join(projectRoot, 'src', 'app', 'button', 'button.scss'); |
| 145 | + cardStylesheet = join(projectRoot, 'src', 'app', 'card', 'card.scss'); |
| 146 | + dependencyStylesheet = join(dependencyRoot, 'styles.scss'); |
| 147 | + for (const stylesheet of [buttonStylesheet, cardStylesheet]) { |
| 148 | + await mkdir(dirname(stylesheet), { recursive: true }); |
| 149 | + } |
| 150 | + }); |
| 151 | + |
| 152 | + afterAll(async () => { |
| 153 | + shutdownSassWorkerPool(); |
| 154 | + await rm(temporaryRoot, { force: true, recursive: true }); |
| 155 | + }); |
| 156 | + |
| 157 | + beforeEach(() => { |
| 158 | + resetSassWorkerPoolCaches(); |
| 159 | + resolveRequests = []; |
| 160 | + }); |
| 161 | + |
| 162 | + it('should not use the package resolution of a dependency for the application', async () => { |
| 163 | + const dependency = await compile(dependencyStylesheet); |
| 164 | + const application = await compile(buttonStylesheet); |
| 165 | + |
| 166 | + expect(dependency).toContain('content: "dependency";'); |
| 167 | + expect(application).toContain('content: "project";'); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should not use the package resolution of the application for a dependency', async () => { |
| 171 | + const application = await compile(buttonStylesheet); |
| 172 | + const dependency = await compile(dependencyStylesheet); |
| 173 | + |
| 174 | + expect(application).toContain('content: "project";'); |
| 175 | + expect(dependency).toContain('content: "dependency";'); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should not reuse a failed package resolution of the application for a dependency', async () => { |
| 179 | + const source = "@use 'extra';"; |
| 180 | + const application = await compile(buttonStylesheet, source); |
| 181 | + const dependency = await compile(dependencyStylesheet, source); |
| 182 | + |
| 183 | + expect(application).toContain("Can't find stylesheet to import."); |
| 184 | + expect(dependency).toContain('content: "extra";'); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should not use the package root of a dependency for a deep import of the application', async () => { |
| 188 | + // A subpath that resolves to no file of its own is located through the root of the package, |
| 189 | + // which is cached separately from the resolution of the specifier. |
| 190 | + const source = "@use 'theme/sub/other';"; |
| 191 | + const dependency = await compile(dependencyStylesheet, source); |
| 192 | + const application = await compile(buttonStylesheet, source); |
| 193 | + |
| 194 | + expect(dependency).toContain('content: "dependency deep";'); |
| 195 | + expect(application).toContain('content: "project deep";'); |
| 196 | + }); |
| 197 | + |
| 198 | + it('should share a package resolution between the stylesheets of different components', async () => { |
| 199 | + const button = await compile(buttonStylesheet); |
| 200 | + const card = await compile(cardStylesheet); |
| 201 | + |
| 202 | + expect(button).toContain('content: "project";'); |
| 203 | + expect(card).toContain('content: "project";'); |
| 204 | + expect(resolveRequests.length).toBe(1); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should resolve a package url of a non-file containing URL from the working directory', async () => { |
| 208 | + // The stylesheets of a build have file URLs, but Sass does not limit a containing URL to them. |
| 209 | + spyOn(SassCompiler.prototype, 'compileStringAsync').and.callFake(async (_, options) => { |
| 210 | + const importer = options.importers?.[0] as FileImporter<'async'>; |
| 211 | + const url = await importer.findFileUrl('theme', { |
| 212 | + containingUrl: new URL('custom:styles.scss'), |
| 213 | + fromImport: false, |
| 214 | + }); |
| 215 | + |
| 216 | + return { css: url?.href ?? '', loadedUrls: [] }; |
| 217 | + }); |
| 218 | + |
| 219 | + const result = await compile(buttonStylesheet); |
| 220 | + |
| 221 | + expect(result).toBe( |
| 222 | + pathToFileURL(join(projectRoot, 'node_modules', 'theme', '_index.scss')).href, |
| 223 | + ); |
| 224 | + }); |
| 225 | + }); |
49 | 226 | }); |
0 commit comments