Skip to content

feat: deprecated default export of css imports#10762

Closed
manucorporat wants to merge 2 commits intovitejs:mainfrom
BuilderIO:deprecated-css-default-exports
Closed

feat: deprecated default export of css imports#10762
manucorporat wants to merge 2 commits intovitejs:mainfrom
BuilderIO:deprecated-css-default-exports

Conversation

@manucorporat
Copy link
Contributor

@manucorporat manucorporat commented Nov 2, 2022

Description

Importing a css like:

import thing from './global.css';

Will fundamentally cause a double loading of CSS, since vite will emit a .css file, but it;s likely that the CSS will also be used by the application code, being most likely used by the framework runtime to inject the style.

This is actually hard to detect since, the API does not indicate there is a side effect anywhere. Today's solution is to add ?inline, but it's a lie, since the original API is also inlined.

?inline actually means (ONLY inline, no side effect). Imo, this is wrong.

This PR will not cause any breaking changes, but deprecated the usage of:

import stuff from './global.css'

giving a recomendation to use:

import stuff from './global.css?inline'

Additional context

#10235

QwikDev/qwik#1932
QwikDev/qwik#749 (comment)
QwikDev/qwik#1959


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@patak-cat patak-cat added the p3-significant High priority enhancement (priority) label Nov 2, 2022
@patak-cat
Copy link
Member

I think this is not a bad idea. To avoid disruption, if we go this way, maybe we should merge this PR in Vite 4, and then remove support next year with a hard error in Vite 5. @benmccann @matthewp @danielroe interested in your thoughts about this one.

@benmccann
Copy link
Collaborator

The ?inline syntax doesn't seem to be documented on https://vitejs.dev/ or at least doesn't seem to be coming up on the Algolia search unless I'm missing it. We should probably add it to the docs if we're recommending people use it. I'll wait to weigh in until then as I'm not exactly sure what this is doing.

@manucorporat
Copy link
Contributor Author

manucorporat commented Nov 3, 2022

@benmccann indeed! just to clarify this PR is about reducing the duplicate loading of CSS, if developer are already doing side effect import of CSS, it would still work! but if they wish to consume the CSS as a JS string, then they need to use ?inline if they don't the app will still work, but most likely CSS is loaded twice!

@patak-dev fan to avoid disruption as much as possible, current PR will generate something like this in users code if merged for Vite 4:
Screenshot 2022-11-03 at 10 26 38

but it will still work, purely a TS advisory

Looked in the while, and seems like 90% of the usage of CSS is already side-effect only, so hopefully affects very little users

@danielroe
Copy link
Contributor

Agreed. I don't think this move (deprecating in this PR and then removing support in Vite 5) poses any issues from a Nuxt perspective.

As I may have mentioned before, we are currently using ?inline&used within our server builds to emit CSS outside the main entrypoint, as otherwise it gets stripped out on the assumption that no server build would want CSS strings. I wonder if there is an opportunity to document these CSS APIs further?

@patak-cat
Copy link
Member

We discussed the proposal in today's team meeting and we think this is a good idea, we should avoid potential unintended duplicated CSS here.

@manucorporat we would want to merge this for Vite 4, and then remove support in Vite 5 to avoid disruption. We think that the typescript error is a great idea, but we should also add a runtime log when it is used. Let us know if you want to tackle this one, or if not we can add the log in this PR ourselves.

I think it is also a good idea to use this PR to properly document ?inline as @benmccann suggests.

@danielroe @sapphi-red mentioned we may be able to remove the used query (that it is an internal hack) if we merge this, so we should check what Nuxt needs there and implement a proper feature for that case. This would only be changed in Vite 4, so don't worry about the breaking change about using that internal though.

@patak-cat patak-cat added this to the 4.0 milestone Nov 3, 2022
@bluwy
Copy link
Member

bluwy commented Nov 20, 2022

@manucorporat can you allow maintainers to push up to the fork? I've added the deprecation messages locally

@manucorporat
Copy link
Contributor Author

mmm how do i do that?

@patak-cat
Copy link
Member

@manucorporat check for docs here. I think if next time you do this when you create a PR it should remember it moving forward

image

@bluwy
Copy link
Member

bluwy commented Nov 21, 2022

I'm not sure if the permissions are different for org forks, but if it's not possible I could open a new PR if you dont mind.

@GabrielHangor
Copy link

It is breaking some styling when injected into shadow dom of a custom element...

@patak-cat
Copy link
Member

@GabrielHangor please create an issue with a minimal reproduction if you think we could improve this so we can track it, your comment is going to be lost here in the PR

kensternberg-authentik added a commit to goauthentik/authentik that referenced this pull request Dec 11, 2023
This commit follows the [patch for Turnstile](#7854) and
performs a similar operation for the Storybook build, which failed after the latest `npm audit` and
`npm update` passes.

[This patch to Vite](vitejs/vite#10762) fixes a problem with the Vite build
in that Vite could not resolve if a CSS import was strictly at the module level or if it was
necessary to include the imported CSS at the document level.  The fix is to hack a query, `?inline`,
to the end of the import string, to indicate that it's a module-only import.

The Storybook for Web Components build recommended by the Open Webcomponent Consortium is a
Storybook-Vite implementation.  The latest update fully deprecated undecorated CSS imports, and
Storybook broke, unable to reconcile the CSS imports.

This patch inlines the inlining of the CSS automatically for Storybook by using the Rollup
`modify()` plug-in which performs string substitutions on the source code before it's presented to
the compiler and bundler; it recognizes the strings that require inlining, those that match the
regex:

``` JavaScript
/^(import \w+ from .*\.css)";/
```

... and replaces them with a version ending in `.css?inline`.  Because the actual recognizer inside
`modify()` recognizes strings and not regular expressions, a script to build the strings has been
added to the `scripts` folder.

Just like locales, you will have to re-run and re-build `build-storybook-import-maps` script if you
add a new CSS file to the source tree.
BeryJu pushed a commit to goauthentik/authentik that referenced this pull request Dec 18, 2023
This commit follows the [patch for Turnstile](#7854) and
performs a similar operation for the Storybook build, which failed after the latest `npm audit` and
`npm update` passes.

[This patch to Vite](vitejs/vite#10762) fixes a problem with the Vite build
in that Vite could not resolve if a CSS import was strictly at the module level or if it was
necessary to include the imported CSS at the document level.  The fix is to hack a query, `?inline`,
to the end of the import string, to indicate that it's a module-only import.

The Storybook for Web Components build recommended by the Open Webcomponent Consortium is a
Storybook-Vite implementation.  The latest update fully deprecated undecorated CSS imports, and
Storybook broke, unable to reconcile the CSS imports.

This patch inlines the inlining of the CSS automatically for Storybook by using the Rollup
`modify()` plug-in which performs string substitutions on the source code before it's presented to
the compiler and bundler; it recognizes the strings that require inlining, those that match the
regex:

``` JavaScript
/^(import \w+ from .*\.css)";/
```

... and replaces them with a version ending in `.css?inline`.  Because the actual recognizer inside
`modify()` recognizes strings and not regular expressions, a script to build the strings has been
added to the `scripts` folder.

Just like locales, you will have to re-run and re-build `build-storybook-import-maps` script if you
add a new CSS file to the source tree.
BeryJu pushed a commit to goauthentik/authentik that referenced this pull request Dec 18, 2023
* web: fix storybookbuild build after npm update

This commit follows the [patch for Turnstile](#7854) and
performs a similar operation for the Storybook build, which failed after the latest `npm audit` and
`npm update` passes.

[This patch to Vite](vitejs/vite#10762) fixes a problem with the Vite build
in that Vite could not resolve if a CSS import was strictly at the module level or if it was
necessary to include the imported CSS at the document level.  The fix is to hack a query, `?inline`,
to the end of the import string, to indicate that it's a module-only import.

The Storybook for Web Components build recommended by the Open Webcomponent Consortium is a
Storybook-Vite implementation.  The latest update fully deprecated undecorated CSS imports, and
Storybook broke, unable to reconcile the CSS imports.

This patch inlines the inlining of the CSS automatically for Storybook by using the Rollup
`modify()` plug-in which performs string substitutions on the source code before it's presented to
the compiler and bundler; it recognizes the strings that require inlining, those that match the
regex:

``` JavaScript
/^(import \w+ from .*\.css)";/
```

... and replaces them with a version ending in `.css?inline`.  Because the actual recognizer inside
`modify()` recognizes strings and not regular expressions, a script to build the strings has been
added to the `scripts` folder.

Just like locales, you will have to re-run and re-build `build-storybook-import-maps` script if you
add a new CSS file to the source tree.

* web: prettier had opinions

* web: apply eslint + sonarjs check to the scripts folder.

* Google recaptcha (aka Turnstile) doesn't understand the "invisible" setting; that's purely
an HCaptcha thing.

* web: removing the typecast means I no longer need the type.

* web: prettier is still having opinions, dammit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p3-significant High priority enhancement (priority)

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

6 participants