Skip to content
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

CSS from entrypoints is not extracted correctly #17527

Open
7 tasks done
susnux opened this issue Jun 19, 2024 · 4 comments
Open
7 tasks done

CSS from entrypoints is not extracted correctly #17527

susnux opened this issue Jun 19, 2024 · 4 comments

Comments

@susnux
Copy link
Contributor

susnux commented Jun 19, 2024

Describe the bug

I am using vite with JavaScript entry point.

If I have entry points with styles (e.g. Vue components) and also async styles (e.g. async Vue component),
and I set build.cssCodeSplit: false then vite will not correctly extract the CSS.

For the async import the styles are loaded using module preload correctly.
But for the entry points with static imports no CSS entry point is created, so I do not know which CSS file I need to add to my application.

Expected behavior:

  • Either duplicate the styles in entry-point-name.css for the styles synchronously used by that entry point
  • Or create a entry-point-name.css with @import for the css chunks used synchronously by that entry point

Reproduction

https://stackblitz.com/edit/vitejs-vite-bagjfe?file=vite.config.js

Steps to reproduce

  1. Run npm ci && npx vite build
  2. See that there is neither a main.css nor a second.css but just some "randomly" named assets¹

¹ For bigger projects this might contain a lot of css assets with random names.

System Info

Does not matter, but:

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^5.3.1 => 5.3.1

Used Package Manager

npm

Logs

No response

Validations

Copy link

stackblitz bot commented Jun 19, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@susnux
Copy link
Contributor Author

susnux commented Jun 20, 2024

I currently working on a workaround, but it is very unexpected that vite only can handle this with HTML input and not with JS input (meaning you currently have to guess which CSS to use with your JS entry).

My workaround plugin solution: nextcloud-libraries/nextcloud-vite-config#204

@sapphi-red
Copy link
Member

sapphi-red commented Aug 21, 2024

The manifest includes the information about which CSS files needs to be loaded for each entrypoint. The manifest generation can be enabled with build.manifest.
https://vitejs.dev/guide/backend-integration.html has a bit more detailed explanation about using the manifest.

For your reproduction the manifest will be:

{
  "_style.js": {
    "file": "chunks/style.js",
    "name": "style",
    "css": [
      "assets/style.css"
    ]
  },
  "css.js": {
    "file": "chunks/css.js",
    "name": "css",
    "src": "css.js",
    "isDynamicEntry": true,
    "css": [
      "assets/css.css"
    ]
  },
  "main.js": {
    "file": "main.js",
    "name": "main",
    "src": "main.js",
    "isEntry": true,
    "imports": [
      "_style.js",
      "css.js"
    ]
  },
  "second.js": {
    "file": "second.js",
    "name": "second",
    "src": "second.js",
    "isEntry": true,
    "imports": [
      "_style.js"
    ],
    "dynamicImports": [
      "css.js"
    ]
  }
}

From this file, you can calculate which CSS files needs to be loaded for each entry:

  • main.js: assets/style.css, assets/css.css
  • second.js: assets/style.css

@susnux
Copy link
Contributor Author

susnux commented Aug 22, 2024

Maybe one should consider this a missing feature, as for HTML this is already done correctly (it is injected in the HTML entry) but for everything that is not HTML this requires manual parsing then.

Would you accept this feature, like having a cssBuildEntrypoints option with this feature?
While it is possible in a plugin (see my linked work above) there are things not possible in plugins that would only work in core. One of that is full control over the location and naming of CSS entry points vs CSS "chunks".

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

No branches or pull requests

2 participants