|
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 { |
| 15 | + SassStylesheetLanguage, |
| 16 | + isPackageUrl, |
| 17 | + resetSassWorkerPoolCaches, |
| 18 | + shutdownSassWorkerPool, |
| 19 | +} from './sass-language'; |
10 | 20 |
|
11 | 21 | describe('sass-language', () => { |
12 | 22 | describe('isPackageUrl', () => { |
@@ -46,4 +56,142 @@ describe('sass-language', () => { |
46 | 56 | expect(isPackageUrl('')).toBeFalse(); |
47 | 57 | }); |
48 | 58 | }); |
| 59 | + |
| 60 | + describe('package resolution caching', () => { |
| 61 | + let temporaryRoot: string; |
| 62 | + let projectRoot: string; |
| 63 | + let resolveRequests: string[]; |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a build stub that resolves a package specifier by searching the `node_modules` |
| 67 | + * directories visible from the resolve directory, which is how esbuild resolves the |
| 68 | + * package specifiers of a stylesheet. |
| 69 | + */ |
| 70 | + function createBuildStub(): PluginBuild { |
| 71 | + return { |
| 72 | + initialOptions: { absWorkingDir: projectRoot }, |
| 73 | + resolve: async (path: string, options: { resolveDir: string }) => { |
| 74 | + resolveRequests.push(`${options.resolveDir}:${path}`); |
| 75 | + |
| 76 | + for (let directory = options.resolveDir; ; directory = dirname(directory)) { |
| 77 | + // A package specifier resolves to the index file of the package, and an explicit file |
| 78 | + // within it to that file. A deeper subpath is left unresolved, as esbuild leaves one |
| 79 | + // that the `exports` of the package does not name; the Sass importer then resolves it |
| 80 | + // against the package root instead. |
| 81 | + for (const candidate of [ |
| 82 | + join(directory, 'node_modules', path, '_index.scss'), |
| 83 | + join(directory, 'node_modules', path), |
| 84 | + ]) { |
| 85 | + if (statSync(candidate, { throwIfNoEntry: false })?.isFile()) { |
| 86 | + return { path: candidate, errors: [], warnings: [] }; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (dirname(directory) === directory) { |
| 91 | + return { path: undefined, errors: [], warnings: [] }; |
| 92 | + } |
| 93 | + } |
| 94 | + }, |
| 95 | + } as unknown as PluginBuild; |
| 96 | + } |
| 97 | + |
| 98 | + async function compile(stylesheet: string, source = "@use 'theme';"): Promise<string> { |
| 99 | + const result = await SassStylesheetLanguage.process?.( |
| 100 | + source, |
| 101 | + stylesheet, |
| 102 | + 'scss', |
| 103 | + { sourcemap: false }, |
| 104 | + createBuildStub(), |
| 105 | + ); |
| 106 | + if (!result) { |
| 107 | + throw new Error('The Sass stylesheet language has no process function.'); |
| 108 | + } |
| 109 | + |
| 110 | + if (result.errors?.length) { |
| 111 | + return `error: ${result.errors[0].text}`; |
| 112 | + } |
| 113 | + |
| 114 | + return (result.contents as string).trim(); |
| 115 | + } |
| 116 | + |
| 117 | + beforeAll(async () => { |
| 118 | + temporaryRoot = await mkdtemp(join(tmpdir(), 'angular-cli-sass-language-')); |
| 119 | + projectRoot = join(temporaryRoot, 'project'); |
| 120 | + |
| 121 | + // A project with a `theme` package, plus a component directory with its own copy of it. |
| 122 | + for (const [directory, marker] of [ |
| 123 | + [join(projectRoot, 'node_modules', 'theme'), 'project'], |
| 124 | + [join(projectRoot, 'nested', 'node_modules', 'theme'), 'nested'], |
| 125 | + ]) { |
| 126 | + await mkdir(join(directory, 'sub'), { recursive: true }); |
| 127 | + await writeFile(join(directory, 'package.json'), '{ "name": "theme" }'); |
| 128 | + await writeFile(join(directory, '_index.scss'), `.marker { content: "${marker}"; }`); |
| 129 | + await writeFile( |
| 130 | + join(directory, 'sub', '_other.scss'), |
| 131 | + `.deep { content: "${marker} deep"; }`, |
| 132 | + ); |
| 133 | + } |
| 134 | + for (const directory of ['component', 'sibling', 'nested']) { |
| 135 | + await mkdir(join(projectRoot, directory), { recursive: true }); |
| 136 | + } |
| 137 | + // A directory outside the project with no `theme` package visible to it. |
| 138 | + await mkdir(join(temporaryRoot, 'isolated'), { recursive: true }); |
| 139 | + }); |
| 140 | + |
| 141 | + afterAll(async () => { |
| 142 | + shutdownSassWorkerPool(); |
| 143 | + await rm(temporaryRoot, { force: true, recursive: true }); |
| 144 | + }); |
| 145 | + |
| 146 | + beforeEach(() => { |
| 147 | + resetSassWorkerPoolCaches(); |
| 148 | + resolveRequests = []; |
| 149 | + }); |
| 150 | + |
| 151 | + it('should not use a nested package resolution for a stylesheet that cannot see it', async () => { |
| 152 | + const nested = await compile(join(projectRoot, 'nested', 'styles.scss')); |
| 153 | + const component = await compile(join(projectRoot, 'component', 'styles.scss')); |
| 154 | + |
| 155 | + expect(nested).toContain('content: "nested";'); |
| 156 | + expect(component).toContain('content: "project";'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should not use a package resolution from a stylesheet with additional nested packages', async () => { |
| 160 | + // The reverse order of the above. The resolution of the first stylesheet must not be |
| 161 | + // reused for the second, which has an additional `node_modules` directory to search. |
| 162 | + const component = await compile(join(projectRoot, 'component', 'styles.scss')); |
| 163 | + const nested = await compile(join(projectRoot, 'nested', 'styles.scss')); |
| 164 | + |
| 165 | + expect(component).toContain('content: "project";'); |
| 166 | + expect(nested).toContain('content: "nested";'); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should reuse a package resolution for stylesheets that search the same directories', async () => { |
| 170 | + const component = await compile(join(projectRoot, 'component', 'styles.scss')); |
| 171 | + const sibling = await compile(join(projectRoot, 'sibling', 'styles.scss')); |
| 172 | + |
| 173 | + expect(component).toContain('content: "project";'); |
| 174 | + expect(sibling).toContain('content: "project";'); |
| 175 | + expect(resolveRequests.length).toBe(1); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should not use a nested package root for a deep import that cannot see it', async () => { |
| 179 | + // A subpath that resolves to no file of its own is located through the root of the package, |
| 180 | + // which is cached separately from the resolution of the specifier. |
| 181 | + const deep = "@use 'theme/sub/other';"; |
| 182 | + const nested = await compile(join(projectRoot, 'nested', 'styles.scss'), deep); |
| 183 | + const component = await compile(join(projectRoot, 'component', 'styles.scss'), deep); |
| 184 | + |
| 185 | + expect(nested).toContain('content: "nested deep";'); |
| 186 | + expect(component).toContain('content: "project deep";'); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should not reuse a failed package resolution for a stylesheet that can resolve it', async () => { |
| 190 | + const isolated = await compile(join(temporaryRoot, 'isolated', 'styles.scss')); |
| 191 | + const component = await compile(join(projectRoot, 'component', 'styles.scss')); |
| 192 | + |
| 193 | + expect(isolated).toContain("Can't find stylesheet to import."); |
| 194 | + expect(component).toContain('content: "project";'); |
| 195 | + }); |
| 196 | + }); |
49 | 197 | }); |
0 commit comments