Skip to content

Commit 0d49f86

Browse files
committed
fix(@angular/build): resolve style include paths relative to ng-package.json in unit-test builder
When using the `unit-test` builder with an `ng-packagr` build target, the `styleIncludePaths` from `ng-package.json` were being used relative to the workspace root instead of the library's directory. This caused build failures when libraries specified style include paths. This change ensures that these paths are correctly resolved relative to the directory containing `ng-package.json`. (cherry picked from commit ac065ad)
1 parent d173973 commit 0d49f86

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

packages/angular/build/src/builders/unit-test/builder.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,16 +373,15 @@ async function transformNgPackagrOptions(
373373
throw new Error(`Could not read ng-package.json at ${ngPackagePath}: ${e.message}`);
374374
}
375375

376-
const lib = ngPackageJson['lib'] || {};
377-
const styleIncludePaths = lib['styleIncludePaths'] || [];
378-
const assets = ngPackageJson['assets'] || [];
379-
const inlineStyleLanguage = ngPackageJson['inlineStyleLanguage'];
376+
const { lib: { styleIncludePaths = [] } = {}, assets = [], inlineStyleLanguage } = ngPackageJson;
377+
378+
const includePaths = styleIncludePaths.map((includePath: string) =>
379+
path.resolve(path.dirname(ngPackagePath), includePath),
380+
);
380381

381382
return {
382-
stylePreprocessorOptions: styleIncludePaths.length
383-
? { includePaths: styleIncludePaths }
384-
: undefined,
383+
stylePreprocessorOptions: includePaths.length ? { includePaths } : undefined,
385384
assets: assets.length ? assets : undefined,
386-
inlineStyleLanguage: typeof inlineStyleLanguage === 'string' ? inlineStyleLanguage : undefined,
385+
inlineStyleLanguage,
387386
} as ApplicationBuilderInternalOptions;
388387
}

tests/e2e/tests/vitest/library.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import assert from 'node:assert/strict';
22
import { updateJsonFile } from '../../utils/project';
3-
import { ng, silentNpm } from '../../utils/process';
4-
import { createDir, writeFile } from '../../utils/fs';
3+
import { ng } from '../../utils/process';
4+
import { appendToFile, createDir, writeFile } from '../../utils/fs';
55

66
export default async function (): Promise<void> {
7-
// Install Vitest deps
8-
await silentNpm('install', 'vitest@^4.0.8', 'jsdom@^27.1.0', '--save-dev');
9-
107
// Generate a library
118
await ng('generate', 'library', 'my-lib', '--test-runner', 'vitest');
129

@@ -25,17 +22,10 @@ export default async function (): Promise<void> {
2522

2623
// 3. Update the component to use SCSS and import the shared file
2724
// Rename CSS to SCSS
28-
await ng(
29-
'generate',
30-
'component',
31-
'styled-comp',
32-
'--project=my-lib',
33-
'--style=scss',
34-
'--skip-import',
35-
);
25+
await ng('generate', 'component', 'styled-comp', '--project=my-lib', '--style=scss');
3626

37-
await writeFile(
38-
'projects/my-lib/src/lib/styled-comp/styled-comp.component.scss',
27+
await appendToFile(
28+
'projects/my-lib/src/lib/styled-comp/styled-comp.scss',
3929
`
4030
@use 'vars';
4131
p { color: vars.$primary-color; }

0 commit comments

Comments
 (0)