Skip to content

Module resolution used to load styles from angular.json doesn't work with package exports #23568

Closed
@michaelfaith

Description

@michaelfaith

🐞 Bug report

Command (mark with an x)

  • new
  • build
  • serve
  • test
  • e2e
  • generate
  • add
  • update
  • lint
  • extract-i18n
  • run
  • config
  • help
  • version
  • doc

Is this a regression?

No

Description

Per this discussion: #16980 (comment)
module resolution currently works for entries in the angular.json styles array only if the style file is declared as the package's main entry, but doesn't currently support package exports. This is ultimately problematic for working in a Yarn pnp environment. In fact, Angular Material's current ng-add logic adds one of their prebuilt styles to the angular.json using a relative path to node_modules, which is problematic on its own, but even if they adjusted it to be a package-relative path, it wouldn't work since their prebuilts are made accessible through package exports.
e.g.:

"styles": [
    "@angular/material/prebuilt-themes/indigo-pink.css",
    "src/styles.scss"
  ],

The request is that module resolution for loading styles (and scripts) from the angular.json is expanded to support package exports, which would allow Angular Material to adjust their ng-add to use a package-relative path instead of a node_modules relative one.

🔬 Minimal Reproduction

Within a new project, run ng add @angular/material
After completion, change the styles array in the angular.json to this:

"styles": [
    "@angular/material/prebuilt-themes/indigo-pink.css",
    "src/styles.scss"
  ],

Run ng serve

🔥 Exception or Error

When attempting to use the above, you get this error on build/serve, since it's treating it as a file path:

An unhandled exception occurred: ENOENT: no such file or directory, lstat 'C:\Users\<user>\Desktop\test147\@angular'

🌍 Your Environment


Angular CLI: 14.0.5
Node: 16.14.2
Package Manager: yarn 1.22.18
OS: win32 x64

Angular: 14.0.5
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1400.5
@angular-devkit/build-angular   14.0.5
@angular-devkit/core            14.0.5
@angular-devkit/schematics      14.0.5
@angular/cdk                    14.0.4
@angular/flex-layout            14.0.0-beta.40
@angular/material               14.0.4
@schematics/angular             14.0.5
rxjs                            7.5.5
typescript                      4.7.4

Anything else relevant?
I opened a related bug in the components repo for the update to their ng-add schematic to change the prebuilt styles entry to be package-relative, once this is possible: angular/components#25242

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions