You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Angular never loads an external library .css asset #31017
Yes, this behavior used to work in the previous version
The previous version in which this bug was not present was
No response
Description
If external library has a CSS import, the asset will be processed by Angular (in both dev server and build), but never actually loaded in the browser.
Minimal Reproduction
Clone this repo
git clone https://github.com/maxpatiiuk/angular-css-asset-lost
cd angular-css-asset-lost
Install dependencies
# To keep reproduction size minimal, I hardcoded a minimal root-level node_modules# So, install Angular dependencies only in the app folder:cd app
npm install
Start the development server in the app folder
npx ng serve
See that the CSS is never loaded in the browser (page would turn green if it was loaded).
Build the app
npx ng build
Build output:
> ng build
Initial chunk files | Names | Raw size | Estimated transfer size
main.js | main | 91.03 kB | 27.28 kB
| Initial total | 91.03 kB | 27.28 kB
Lazy chunk files | Names | Raw size | Estimated transfer size
main.css | - | 56 bytes | 56 bytes
The lazy chunk is created, but never referenced in the index.html or any .js file.
You can serve the build in the browser to verify:
npx serve dist/reproduction/browser -p 4200
See node_modules/external-library/ for the dummy external library with a CSS file.
My component library has global css (for css variables). It also consumes several other component libraries, each of which also has global css. I wish to auto-load the styles for my consumers to simplify the "Get started" instructions.
Assuming that this is expected behavior, could you please advise how should an end user handle this?
Should the user manually import such CSS file in their index.html? Unfortunately, while that fixes the build, it causes a 404 error in the dev server.
Moreover, if outputHashing is enabled, the output CSS file name is not stable, so manually importing it is not possible.
For reference, here is how other bundlers & frameworks handled such case:
Vite, Webpack (css-loader or experiments.css), Rspack, AnalogJS, Next.js, Parcel: css is bundled & auto-loaded automatically
ESBuild: all CSS is bundled into a single CSS file with a controllable name (regardless if the CSS file was imported in CSS or in JS). Plus, the user can customize this with a plugin.
Rollup, Rolldown: a community plugin can be used (I am not sure if this is possible in angular-cli)
IMHO, importing CSS as a JavaScript module is not to be considered a good practice and should be avoided, especially when building a library. This approach relies on specific build tools to transform the CSS into a valid JS module, and different tools may handle this process in different ways, leading to inconsistent behavior.
Instead, a better approach is to provide a pre-compiled CSS file or a SCSS file. Users can then import and include these files as part of their own build pipeline, which gives them more control and ensures consistency. In Angular users can use the style option. This is also approach that is used by the majority of JS and CSS frameworks.
Please let me tell you more about our use case for this.
Our component library (includes css) depends on other component libraries (which also include css).
The sub-libraries may only be needed by some entrypoints so always importing their css is wasteful.
In addition, if we wish to add a new sub-library dependency in the future, if css is not imported automatically, we would have to declare a breaking change and make our consumers manually add the new css import.
As it stands, we have a nice story "css is handled automatically, don't worry" for most users, with a red warning message for Angular users.
In Angular users can use the style option. This is also approach that is used by the majority of JS and CSS frameworks.
Per my testing, a library .js file importing .css is handled correctly in:
React (Next.js, React-router)
SvelteKit
AnalogJS
Nuxt. Vanilla Vue (Vite)
Preact (default Vite starter)
I was not able to find any other framework where this use case is not handled.
monaco-editor, a 2 million weekly downloads library, include .css imports in hundreds of their .css files. This ensures that only the CSS for the bundles you imported will be bundled in.
My component library uses monaco-editor. For most of my users, the CSS just works automatically. However, Angular users face issue with CSS not working.
The only way I could think of working around this is if I bundle in the monaco-editor's css - but that means users of every other bundler will get CSS twice (the one I bundled in, and the one from .css imports in monaco-editor's .js files).
For now, I have to tell my users to explicitly import monaco's css. This is not scalable as it means when I add a new UI dependency, I have to declare a breaking change, forcing Angular users to add yet another CSS import.
For reference, monaco's get started does not mention css at all because it "just works" for most users.
I hope you reconsider the position given the use cases I provided. Importing .css in .js is very useful for library developers.
Let me know if I can provide more examples.
Thank you for your time.
angular-automatic-lock-bot commented on Sep 30, 2025
Activity
maxpatiiuk commentedon Aug 22, 2025
Use case:
This works in most bundlers, but not in Angular.
alan-agius4 commentedon Aug 22, 2025
This expected and is the same behaviour of esbuild which is documented in their documentation: https://esbuild.github.io/content-types/#css-from-js
The fact that under certain conditions it works in
ng serveseems to be an unexpected behaviour.maxpatiiuk commentedon Aug 24, 2025
Thanks for a quick reply!
Assuming that this is expected behavior, could you please advise how should an end user handle this?
Should the user manually import such CSS file in their index.html? Unfortunately, while that fixes the build, it causes a 404 error in the dev server.
Moreover, if outputHashing is enabled, the output CSS file name is not stable, so manually importing it is not possible.
For reference, here is how other bundlers & frameworks handled such case:
imaksp commentedon Aug 25, 2025
Hi, you can include it in angular.json file like this:
you can also import it in main style.css file using css @import.
alan-agius4 commentedon Aug 25, 2025
IMHO, importing CSS as a JavaScript module is not to be considered a good practice and should be avoided, especially when building a library. This approach relies on specific build tools to transform the CSS into a valid JS module, and different tools may handle this process in different ways, leading to inconsistent behavior.
Instead, a better approach is to provide a pre-compiled CSS file or a SCSS file. Users can then import and include these files as part of their own build pipeline, which gives them more control and ensures consistency. In Angular users can use the
styleoption. This is also approach that is used by the majority of JS and CSS frameworks.maxpatiiuk commentedon Aug 26, 2025
Please let me tell you more about our use case for this.
Our component library (includes css) depends on other component libraries (which also include css).
The sub-libraries may only be needed by some entrypoints so always importing their css is wasteful.
In addition, if we wish to add a new sub-library dependency in the future, if css is not imported automatically, we would have to declare a breaking change and make our consumers manually add the new css import.
As it stands, we have a nice story "css is handled automatically, don't worry" for most users, with a red warning message for Angular users.
Per my testing, a library .js file importing .css is handled correctly in:
I was not able to find any other framework where this use case is not handled.
maxpatiiuk commentedon Aug 30, 2025
monaco-editor, a 2 million weekly downloads library, include .css imports in hundreds of their .css files. This ensures that only the CSS for the bundles you imported will be bundled in.
My component library uses monaco-editor. For most of my users, the CSS just works automatically. However, Angular users face issue with CSS not working.
The only way I could think of working around this is if I bundle in the monaco-editor's css - but that means users of every other bundler will get CSS twice (the one I bundled in, and the one from .css imports in monaco-editor's .js files).
For now, I have to tell my users to explicitly import monaco's css. This is not scalable as it means when I add a new UI dependency, I have to declare a breaking change, forcing Angular users to add yet another CSS import.
For reference, monaco's get started does not mention css at all because it "just works" for most users.
I hope you reconsider the position given the use cases I provided. Importing .css in .js is very useful for library developers.
Let me know if I can provide more examples.
Thank you for your time.
angular-automatic-lock-bot commentedon Sep 30, 2025
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.