Skip to content

In application builder allow for custom styles plugins, e.g. for SCSS paths mapping needed in monorepo with many libraries #30180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Platonn opened this issue Apr 25, 2025 · 0 comments

Comments

@Platonn
Copy link
Contributor

Platonn commented Apr 25, 2025

Command

build

Description

Context - our usecase:
Were' developing a set of Angular libraries in a monorepo, where some libs can cross-import SCSS files from other libs, and they should @import them using "symbolic" library paths (not relative paths). e.g. in a SCSS file inside the lib A (@my-org/*lib-A*/styles ) we'd like to import SCSS from lib B (e.g. @import '@my-org/*lib-B*/styles' ).

Solution with old webpack builder:
With the old Angular browser builder it was possible to apply SCSS paths mapping by applying a custom webpack plugin (e.g. using a 3rd party builder @angular-builders/custom-webpack:browser which extended the default Angular browser builder). See the example defining of paths mapping:

Problem after migrating to esbuild application builder:
After migrating to the new Angular application builder, which is based on esbuild, such a paths mapping no longer works (even when passing analogical custom plugin e.g. via the 3rd party @angular-builders/custom-esbuild:application).

Note: we tried passing a custom plugin to buildApplication()'s 3rd argument extensions.codePlugins, but it didn't affect the SCSS paths resolution. Maybe we did something wrong. I guess it's becasue the styles plugins defined in the array below don't take the ambient extensions.codePlugins into account. See the source code of Angular CLI:

const plugins: Plugin[] = [
pluginFactory.create(SassStylesheetLanguage),
pluginFactory.create(LessStylesheetLanguage),
pluginFactory.create(CssStylesheetLanguage),
createCssResourcePlugin(cache),
];

Note 2: I understand that buildApplication() has a JSDoc comment "@experimental Direct usage of this function is considered experimental.". That said, I'm still hoping we can help improve this function with our feedback, so it addresses needs of authors of custom Angular libraries developed in monorepos.

Describe the solution you'd like

It would be nice to be able to inject into buildApplication() custom plugins affecting SCSS paths resolution e.g. into the plugins array in ..../tools/esbuild/stylesheets/bundle-options.ts.

Describe alternatives you've considered

Workaround ideas:
Our ultimate goal is to keep our example app in our monorepo working. And publish our libs so their consumers can import them and it should work in their apps.

So alternatively we can keep relative paths in our monorepo (e.g. @import '*lib-b*/....' . And as a post-build step of a production build of an Angular library (with ng-packgr) we could search-replace with a regex in the /dist folder all the cross-library scss import paths to relevant "symbolic" paths (.e.g @import *@my-org/lib-b*/....
Thanks to this, the example app in our monorepo using relative paths would still work (no need for paths mapping in the application builder). And users of our libraries would get shipped SCSS sources which are cross-importing other libs' styles via symbolic paths, which will resolve successfully (using their node_modules/*@my-org/lib-b*/... )
The downside of it is that our example app in our monorepo would differ from our user's apps - which would be a bit confusing to read (and ambigious to diffrentiate it from non-cross-lib other relative imports from inside the same library itself).

Note: I've found a related issue #23552 but it's old and related to the webpack builder, where workaround with custom webpack plugin was possible. I've created this issue for the purpose of application builder, where I hope we could add an API for injecting a custom plugin affecting SCSS (e.g. for a custom paths mapping).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant