Skip to content

CSS imported from an injectScript('page') script is dropped from the build #18137

Description

@KayleeWilliams

Astro Info

Astro                    v7.3.5
Vite                     v8.3.1
Node                     v24.19.0
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             injected

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

An integration adds a script with injectScript('page', ...). That script imports a module, and the module imports a CSS file. After astro build, the JS is there but the CSS file is not. The class names ship with no styles.

The same import from a <script> in a page keeps its CSS.

It works in 5.16.11 and breaks in 5.16.12. It's still broken in 7.3.5. Static and dynamic imports both break.

I think the cause is the orphan cleanup in plugin-css.ts, added in #14607. It deletes a stylesheet when no page uses it and no chunk has it in viteMetadata.importedAssets. Vite records CSS for a JS chunk in viteMetadata.importedCss, which the check doesn't read, so the stylesheet is treated as unused.

What's the expected result?

The CSS file is written to dist/ and loads with the chunk that imports it, like it did in 5.16.11.

Link to Minimal Reproducible Example

https://github.com/KayleeWilliams/astro-injected-script-css

Run npm install && npm run build. No .css file is written to dist/_astro/. Run npm run preview and click both buttons. The injected one should be red and is black.

Participation

  • I am willing to submit a pull request for this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P4: importantViolate documented behavior or significantly impacts performance (priority)pkg: astroRelated to the core `astro` package (scope)triage: fix verifiedReporter confirmed the triage bot fix works

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions