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

Unable to exclude certain css @import statements from getting inlined #18037

Open
7 tasks done
freshgiammi opened this issue Sep 5, 2024 · 3 comments
Open
7 tasks done

Comments

@freshgiammi
Copy link

freshgiammi commented Sep 5, 2024

Describe the bug

I'm building a library which exports some css, but also imports some more from a second library.

The objective I'm pursuing is not to extract CSS files coming from a component library and keep them as imports, since that library itself will be already used by all consumers. Doing so also gets rid of possible specificity issues as the component library is always imported before the library's own styles.

The CSS is simple enough:

/* src/style/styles.css */
@import "@org/componentLibrary/dist/style.css";

@tailwind base;
@tailwind components;
@tailwind utilities;

where an import is stated before tailwindcss' own styles.
Since Vite provides @import inlining (as stated here), this import statement gets extracted and the content is merged in the output .css file.

Currently, it seems as there's no way to exclude certain import statements from getting inlined. This should be doable with postcss-import (using filter), however Vite already uses this plugin internally, and it seems like there's no way to configure it. Additionally, providing the postcss plugin in the css.postCss.plugins array seems to ignore the plugin (possibly getting skipped as it's a duplicate plugin?).

Is there a way to achieve this result and if not, can a way to exclude imports be proposed so that there's more flexibility here?

These issues/questions seem related:

Reproduction

https://stackblitz.com/edit/vitejs-vite-t6nqaz

Steps to reproduce

  • run npm run build
  • see that the import statement has been extracted and inlined in the output css

System Info

System:
    OS: macOS 14.5
    CPU: (12) arm64 Apple M2 Pro
    Memory: 492.94 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
    pnpm: 9.0.5 - ~/.nvm/versions/node/v20.11.1/bin/pnpm
    bun: 1.1.21 - ~/.bun/bin/bun
  Browsers:
    Chrome: 128.0.6613.115
    Safari: 17.5

Used Package Manager

pnpm

Logs

No response

Validations

@sapphi-red
Copy link
Member

sapphi-red commented Sep 10, 2024

Duplicate of #13998 Nope, it's different.

@sapphi-red sapphi-red marked this as a duplicate of #13998 Sep 10, 2024
@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Sep 10, 2024
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Sep 10, 2024
@sapphi-red sapphi-red reopened this Sep 10, 2024
@sapphi-red sapphi-red added enhancement: pending triage and removed duplicate This issue or pull request already exists labels Sep 10, 2024
@sapphi-red sapphi-red marked this as not a duplicate of #13998 Sep 10, 2024
@freshgiammi
Copy link
Author

Duplicate of #13998 Nope, it's different.

Well they do seem to share the root issue: there's no way to filter out the @import statements.

I didn't come across that issue when searching before opening this one, I guess in a way it is a duplicate (unless we treat this as 'not being able to configure postcss-import' entirely)?

@sapphi-red
Copy link
Member

Yeah, it shares some parts. But I think it's fine to have it separately because #13998 wants to externalize a CSS file in both dev and build, and this issue wants to externalize a CSS file only in build.

lucaswerkmeister added a commit to wmde/new-lexeme-special-page that referenced this issue Sep 10, 2024
Use them as CSS variables, so they’ll automatically be updated when
MediaWiki ships a new Codex version without having to rebuild the app
(and they can depend on the skin too). To avoid including all the design
tokens in the build anyway (Vite doesn’t really support excluding CSS
imports yet, see vitejs/vite#18037), we have
to make sure to not import the tokens anywhere in the app (i.e. neither
in NewLexemeForm.vue nor in App.vue) – only directly in the index.html
(dev entry point).

Bug: T369505
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