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

fix: support ?raw for css #2148

Closed
wants to merge 2 commits into from
Closed

fix: support ?raw for css #2148

wants to merge 2 commits into from

Conversation

CHOYSEN
Copy link
Contributor

@CHOYSEN CHOYSEN commented Feb 21, 2021

No description provided.

@underfin
Copy link
Member

Can you add test for it?

@CHOYSEN
Copy link
Contributor Author

CHOYSEN commented Feb 23, 2021

OK, done.

@yyx990803
Copy link
Member

Hmm, you don't need ?raw to get the CSS as a string - that's the default behavior already.

If ?raw is used, the returned css should be the raw, non-compiled CSS instead.

@CHOYSEN
Copy link
Contributor Author

CHOYSEN commented Feb 24, 2021

But if you don't add ?raw to get the CSS as a string, the css will change style

@yyx990803
Copy link
Member

That's why I'm considering a change:

// gets the string, but does not insert
import css from './foo.css'

// insert on import, does not generate `export default ...`
import './foo.css'

But semantically es import syntax should not affect behavior so I'm still debating.

@patak-dev
Copy link
Member

But semantically es import syntax should not affect behavior so I'm still debating.

I agree that it is better if they are semantically equivalent. Maybe adding a new special query for this?

// raw file content
import content from './foo.css?raw'

// gets the string, but does not insert
import css from './foo.css?detached'

// insert on import
import css from './foo.css'

@yyx990803
Copy link
Member

Yeah I think another query is somewhat required for now. How about ?inline instead of ?detached?

Also note in the future there might be native behavior for CSS imports: https://github.com/WICG/webcomponents/blob/gh-pages/proposals/css-modules-v1-explainer.md (it will likely require a breaking change or a global option like css.importAs but that's when it actually lands.)

@patak-dev
Copy link
Member

At least for me, ?inline makes me think the css will still be applied but it will be inlined in the file somehow (instead of having a separate .css)
Other options ?apart, ?disjoint, ?unused, ?inactive, ?unapply, ?separate, ?unconnect I saw while searching for a name in case they help

@yyx990803
Copy link
Member

I think it's fine if we document it:

?inline allows you to get the compiled CSS as an inlined JavaScript string, however does not inject it automatically. You can manually control how/when to inject it (either by creating a <style> tag or via Constructable Stylesheets)

@Shinigami92 Shinigami92 added p2-nice-to-have Not breaking anything but nice to have (priority) 🔍 review needed labels Mar 23, 2021
@aleclarson
Copy link
Member

That's why I'm considering a change:

// gets the string, but does not insert
import css from './foo.css'

// insert on import, does not generate `export default ...`
import './foo.css'

This seems fine to me. The proposed behavior of each is intuitive.

It's exactly what we do in vite-plugin-mdx for "pre-built transclusion".
https://github.com/brillout/vite-plugin-mdx#pre-built-transclusion

@lovetingyuan
Copy link

{
    name: 'raw-css',
    transform(code, id) {
        if (id.endsWith('.css?raw') && code.startsWith('export default ')) {
            return JSON.parse(code.slice('export default '.length))  
        }
    },
}

@franktopel
Copy link

franktopel commented Jul 19, 2021

Please consider implementing this in a way that is likely to be compatible with the upcoming CSS module scripts.

@hooguso
Copy link

hooguso commented Jul 21, 2021

Any news on this issue? The pull request is already 5 months old.
I would be really happy to see this issue be resolved. In its current state the import is broken for component development using shadow DOM.

@Shinigami92
Copy link
Member

This PR has been superseded by e1de8a8

There will be an ?inline in next release

@zanona
Copy link

zanona commented Mar 22, 2022

This PR has been superseded by e1de8a8

There will be an ?inline in next release

Perhaps it would be worth adding it to the docs?
https://vitejs.dev/guide/assets.html#importing-asset-as-string

@webdevnerdstuff
Copy link

webdevnerdstuff commented Mar 17, 2023

Has anybody been able to get this working when bundling with rollup (with typescript)? rollup-plugin-typescript2 will complain that the file doesn't exist, and it will give the exact location of the file... where it does in fact exist. I have tried @rollup/typescript but haven't been able to get that one to work for anything on multiple projects.

But I digress, using the inline I haven't been able to find a way to get rollup to work with it. During development with Vite it works no problem, but once you try to bundle it is when it won't cooperate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.