|
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 assert from 'node:assert'; |
| 11 | +import { statSync } from 'node:fs'; |
| 12 | +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; |
| 13 | +import { dirname, join } from 'node:path'; |
| 14 | +import { pathToFileURL } from 'node:url'; |
| 15 | +import { SassCompiler } from '../../sass/sass-service'; |
| 16 | +import { |
| 17 | + SassStylesheetLanguage, |
| 18 | + getPackageScope, |
| 19 | + isPackageUrl, |
| 20 | + resetSassWorkerPoolCaches, |
| 21 | + shutdownSassWorkerPool, |
| 22 | +} from './sass-language'; |
10 | 23 |
|
11 | 24 | describe('sass-language', () => { |
12 | 25 | describe('isPackageUrl', () => { |
@@ -46,4 +59,232 @@ describe('sass-language', () => { |
46 | 59 | expect(isPackageUrl('')).toBeFalse(); |
47 | 60 | }); |
48 | 61 | }); |
| 62 | + |
| 63 | + describe('getPackageScope', () => { |
| 64 | + const scope = (containingPath?: string) => getPackageScope(containingPath, '/app'); |
| 65 | + |
| 66 | + it('should use the working directory for a stylesheet outside node_modules', () => { |
| 67 | + expect(scope('/app/src/app.scss')).toBe('/app'); |
| 68 | + expect(scope('/app/src/foo-node_modules/a.scss')).toBe('/app'); |
| 69 | + expect(scope('/app/src/node_modules.scss')).toBe('/app'); |
| 70 | + expect(scope(undefined)).toBe('/app'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should use the enclosing package root for a stylesheet within node_modules', () => { |
| 74 | + expect(scope('/app/node_modules/pkg/a.scss')).toBe('/app/node_modules/pkg'); |
| 75 | + expect(scope('/app/node_modules/pkg/sub/a.scss')).toBe('/app/node_modules/pkg'); |
| 76 | + expect(scope('/app/node_modules/@scope/pkg/sub/a.scss')).toBe('/app/node_modules/@scope/pkg'); |
| 77 | + expect(scope('/app/node_modules/foo-node_modules/a.scss')).toBe( |
| 78 | + '/app/node_modules/foo-node_modules', |
| 79 | + ); |
| 80 | + expect(scope('/app/node_modules/a.scss')).toBe('/app/node_modules'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should use the innermost package root for a nested dependency', () => { |
| 84 | + expect(scope('/app/node_modules/dep/node_modules/pkg/a.scss')).toBe( |
| 85 | + '/app/node_modules/dep/node_modules/pkg', |
| 86 | + ); |
| 87 | + expect(scope('/app/node_modules/.pnpm/pkg@1.0.0/node_modules/pkg/a.scss')).toBe( |
| 88 | + '/app/node_modules/.pnpm/pkg@1.0.0/node_modules/pkg', |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should support Windows path separators', () => { |
| 93 | + expect(getPackageScope('C:\\app\\node_modules\\pkg\\sub\\a.scss', 'C:\\app')).toBe( |
| 94 | + 'C:/app/node_modules/pkg', |
| 95 | + ); |
| 96 | + expect(getPackageScope('C:\\app\\src\\app.scss', 'C:\\app')).toBe('C:\\app'); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('package resolution caching', () => { |
| 101 | + let temporaryRoot: string; |
| 102 | + let projectRoot: string; |
| 103 | + let buttonStylesheet: string; |
| 104 | + let cardStylesheet: string; |
| 105 | + let dependencyStylesheet: string; |
| 106 | + let resolveRequests: string[]; |
| 107 | + |
| 108 | + /** |
| 109 | + * Creates a build stub that resolves a package specifier by searching the `node_modules` |
| 110 | + * directories visible from the resolve directory, which is how esbuild resolves the |
| 111 | + * package specifiers of a stylesheet. |
| 112 | + */ |
| 113 | + function createBuildStub(): PluginBuild { |
| 114 | + return { |
| 115 | + initialOptions: { absWorkingDir: projectRoot }, |
| 116 | + resolve: async (path: string, options: { resolveDir: string }) => { |
| 117 | + resolveRequests.push(`${options.resolveDir}:${path}`); |
| 118 | + |
| 119 | + for (let directory = options.resolveDir; ; directory = dirname(directory)) { |
| 120 | + // A package specifier resolves to the index file of the package, and an explicit file |
| 121 | + // within it to that file. A deeper subpath is left unresolved, as esbuild leaves one |
| 122 | + // that the `exports` of the package does not name; the Sass importer then resolves it |
| 123 | + // against the package root instead. |
| 124 | + for (const candidate of [ |
| 125 | + join(directory, 'node_modules', path, '_index.scss'), |
| 126 | + join(directory, 'node_modules', path), |
| 127 | + ]) { |
| 128 | + if (statSync(candidate, { throwIfNoEntry: false })?.isFile()) { |
| 129 | + return { path: candidate, errors: [], warnings: [] }; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (dirname(directory) === directory) { |
| 134 | + return { path: undefined, errors: [], warnings: [] }; |
| 135 | + } |
| 136 | + } |
| 137 | + }, |
| 138 | + } as unknown as PluginBuild; |
| 139 | + } |
| 140 | + |
| 141 | + async function compile(stylesheet: string, source = "@use 'theme';"): Promise<string> { |
| 142 | + const result = await SassStylesheetLanguage.process?.( |
| 143 | + source, |
| 144 | + stylesheet, |
| 145 | + 'scss', |
| 146 | + { sourcemap: false }, |
| 147 | + createBuildStub(), |
| 148 | + ); |
| 149 | + if (!result) { |
| 150 | + throw new Error('The Sass stylesheet language has no process function.'); |
| 151 | + } |
| 152 | + |
| 153 | + if (result.errors?.length) { |
| 154 | + return `error: ${result.errors[0].text}`; |
| 155 | + } |
| 156 | + |
| 157 | + return (result.contents as string).trim(); |
| 158 | + } |
| 159 | + |
| 160 | + async function writePackage(directory: string, marker: string): Promise<void> { |
| 161 | + await mkdir(join(directory, 'sub'), { recursive: true }); |
| 162 | + await writeFile(join(directory, 'package.json'), '{}'); |
| 163 | + await writeFile(join(directory, '_index.scss'), `.marker { content: "${marker}"; }`); |
| 164 | + await writeFile( |
| 165 | + join(directory, 'sub', '_other.scss'), |
| 166 | + `.deep { content: "${marker} deep"; }`, |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + beforeAll(async () => { |
| 171 | + const baseTmpDir = process.env['TEST_TMPDIR']; |
| 172 | + assert(baseTmpDir, 'TEST_TMPDIR is not set'); |
| 173 | + temporaryRoot = await mkdtemp(join(baseTmpDir, 'angular-cli-sass-language-')); |
| 174 | + projectRoot = join(temporaryRoot, 'project'); |
| 175 | + const dependencyRoot = join(projectRoot, 'node_modules', 'dependency'); |
| 176 | + |
| 177 | + // An application using a `theme` package, and a dependency with its own nested version of |
| 178 | + // `theme` plus an `extra` package that only the dependency can see. |
| 179 | + await writePackage(join(projectRoot, 'node_modules', 'theme'), 'project'); |
| 180 | + await writePackage(join(dependencyRoot, 'node_modules', 'theme'), 'dependency'); |
| 181 | + await writePackage(join(dependencyRoot, 'node_modules', 'extra'), 'extra'); |
| 182 | + |
| 183 | + buttonStylesheet = join(projectRoot, 'src', 'app', 'button', 'button.scss'); |
| 184 | + cardStylesheet = join(projectRoot, 'src', 'app', 'card', 'card.scss'); |
| 185 | + dependencyStylesheet = join(dependencyRoot, 'styles.scss'); |
| 186 | + for (const stylesheet of [buttonStylesheet, cardStylesheet]) { |
| 187 | + await mkdir(dirname(stylesheet), { recursive: true }); |
| 188 | + } |
| 189 | + }); |
| 190 | + |
| 191 | + afterAll(async () => { |
| 192 | + shutdownSassWorkerPool(); |
| 193 | + await rm(temporaryRoot, { force: true, recursive: true }); |
| 194 | + }); |
| 195 | + |
| 196 | + beforeEach(() => { |
| 197 | + resetSassWorkerPoolCaches(); |
| 198 | + resolveRequests = []; |
| 199 | + }); |
| 200 | + |
| 201 | + it('should not use the package resolution of a dependency for the application', async () => { |
| 202 | + const dependency = await compile(dependencyStylesheet); |
| 203 | + const application = await compile(buttonStylesheet); |
| 204 | + |
| 205 | + expect(dependency).toContain('content: "dependency";'); |
| 206 | + expect(application).toContain('content: "project";'); |
| 207 | + }); |
| 208 | + |
| 209 | + it('should not use the package resolution of the application for a dependency', async () => { |
| 210 | + const application = await compile(buttonStylesheet); |
| 211 | + const dependency = await compile(dependencyStylesheet); |
| 212 | + |
| 213 | + expect(application).toContain('content: "project";'); |
| 214 | + expect(dependency).toContain('content: "dependency";'); |
| 215 | + }); |
| 216 | + |
| 217 | + it('should not reuse a failed package resolution of the application for a dependency', async () => { |
| 218 | + const source = "@use 'extra';"; |
| 219 | + const application = await compile(buttonStylesheet, source); |
| 220 | + const dependency = await compile(dependencyStylesheet, source); |
| 221 | + |
| 222 | + expect(application).toContain("Can't find stylesheet to import."); |
| 223 | + expect(dependency).toContain('content: "extra";'); |
| 224 | + }); |
| 225 | + |
| 226 | + it('should not use the package root of a dependency for a deep import of the application', async () => { |
| 227 | + // A subpath that resolves to no file of its own is located through the root of the package, |
| 228 | + // which is cached separately from the resolution of the specifier. |
| 229 | + const source = "@use 'theme/sub/other';"; |
| 230 | + const dependency = await compile(dependencyStylesheet, source); |
| 231 | + const application = await compile(buttonStylesheet, source); |
| 232 | + |
| 233 | + expect(dependency).toContain('content: "dependency deep";'); |
| 234 | + expect(application).toContain('content: "project deep";'); |
| 235 | + }); |
| 236 | + |
| 237 | + it('should share a package resolution between the stylesheets of different components', async () => { |
| 238 | + const button = await compile(buttonStylesheet); |
| 239 | + const card = await compile(cardStylesheet); |
| 240 | + |
| 241 | + expect(button).toContain('content: "project";'); |
| 242 | + expect(card).toContain('content: "project";'); |
| 243 | + expect(resolveRequests.length).toBe(1); |
| 244 | + }); |
| 245 | + |
| 246 | + it('should share a package resolution between the stylesheets of different folders of a dependency', async () => { |
| 247 | + const dependencyRoot = join(projectRoot, 'node_modules', 'dependency'); |
| 248 | + const first = await compile(join(dependencyRoot, 'sub1', 'styles.scss')); |
| 249 | + const second = await compile(join(dependencyRoot, 'sub2', 'styles.scss')); |
| 250 | + |
| 251 | + expect(first).toContain('content: "dependency";'); |
| 252 | + expect(second).toContain('content: "dependency";'); |
| 253 | + expect(resolveRequests.length).toBe(1); |
| 254 | + }); |
| 255 | + |
| 256 | + it('should share a package resolution between the stylesheets of different folders of a scoped package', async () => { |
| 257 | + const packageRoot = join(projectRoot, 'node_modules', '@scope', 'pkg'); |
| 258 | + const first = await compile(join(packageRoot, 'sub1', 'styles.scss')); |
| 259 | + const second = await compile(join(packageRoot, 'sub2', 'styles.scss')); |
| 260 | + |
| 261 | + expect(first).toContain('content: "project";'); |
| 262 | + expect(second).toContain('content: "project";'); |
| 263 | + expect(resolveRequests.length).toBe(1); |
| 264 | + }); |
| 265 | + |
| 266 | + it('should resolve a package url of a non-file containing URL from the working directory', async () => { |
| 267 | + // The stylesheets of a build have file URLs, but Sass does not limit a containing URL to them. |
| 268 | + spyOn(SassCompiler.prototype, 'compileStringAsync').and.callFake(async (_, options) => { |
| 269 | + const importer = options.importers?.[0] as { |
| 270 | + findFileUrl( |
| 271 | + url: string, |
| 272 | + context: { containingUrl: URL; fromImport: boolean }, |
| 273 | + ): Promise<URL | null>; |
| 274 | + }; |
| 275 | + const url = await importer.findFileUrl('theme', { |
| 276 | + containingUrl: new URL('custom:styles.scss'), |
| 277 | + fromImport: false, |
| 278 | + }); |
| 279 | + |
| 280 | + return { css: url?.href ?? '', loadedUrls: [] }; |
| 281 | + }); |
| 282 | + |
| 283 | + const result = await compile(buttonStylesheet); |
| 284 | + |
| 285 | + expect(result).toBe( |
| 286 | + pathToFileURL(join(projectRoot, 'node_modules', 'theme', '_index.scss')).href, |
| 287 | + ); |
| 288 | + }); |
| 289 | + }); |
49 | 290 | }); |
0 commit comments