Skip to content

Don't support import css files to javascript #65

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

Closed
marlonmleite opened this issue Sep 20, 2019 · 5 comments
Closed

Don't support import css files to javascript #65

marlonmleite opened this issue Sep 20, 2019 · 5 comments

Comments

@marlonmleite
Copy link

I need to import 1 css file into javascript and use my css content directly in Javascript (this makes it possible for me to get the CSS string and create 1 element style straight into shadowDom without sideEffect):

import a from 'external-lib/bundle.css';

console.log(a);

I need this, because the customElements not work correctly, see: sveltejs/svelte#3594

I need to create 1 customElement, where it will import 1 .css file from an external lib. But, this have a problem, this plugin not work with import a .css file to a string.

Because customElement totally isolates my component, there is no other way to import these external styles without having any side effects. The only way I got anything like this was this:

<style>
	@import '/external-file/lib.css';
</style>

The workround cause sideEffect, because flick my app when loading the css.

@marlonmleite
Copy link
Author

A good feature for svelte/compile: svelte should would read the file content in the compilation and automatically insert the content into shadowDom.

In MySvelte.svelte:

<style>
	@import '/external-file/lib.css';
</style>

Currently, after compilation looks like this:

    class App$1 extends SvelteElement {
    	constructor(options) {
    		super();

    		this.shadowRoot.innerHTML = `<style>@import '/external-lib.css</style>';

With the new feature:

    class App$1 extends SvelteElement {
    	constructor(options) {
    		super();

    		this.shadowRoot.innerHTML = `<style>div.class-external-lib {color: red}</style>';

@maxmilton
Copy link
Contributor

I'm not exactly sure what you're proposing here. Do you mean you want to inline a CSS @import?

You can already acheve this using a style preprocessor, e.g., something like PostCSS. Would thsese be helpful in your case?

@marlonmleite
Copy link
Author

marlonmleite commented Sep 25, 2019

@maxmilton and @Conduitry I try use svelte-preprocess and postcss-import, but the svelte compiler remove all css content, because the hash/content of css don't match the html elements of the current file.

Is there any way to block this behavior from svelte for the style element?

<style type="postcss">
	@import './../public/bundle.css'; 
</style>

<svelte:component this={MyBigComponent} />

in rollup:

    svelte({
      preprocess: [
        postcss({
          plugins: [
            require('postcss-import')
          ]
        })
      ],

But after compile the content of the import don't work, because the svelte force ignore the unlike styles.

@maxmilton
Copy link
Contributor

At this point in time, I don't believe there is a way to disable svelte's removal of "unused" styles. You do have the option of adding the global modifier (:global(.your-style)) to styles to keep svelte from discarding them.

It can be frustrating though when you have a lot of styles you want to inject. In the past I used a custom PostCSS plugin to automatically inject the global modifier for all styles... but now I tend to keep a boundary between global or common/reusable styles and component-specific styles. I don't use web components so I'm not sure what the current best practices are for sharing styles between different web components or instances of the same web component.

If you dig around in https://github.com/sveltejs/svelte/issues you'll find some proposals to change how the styles discarding works.

@Conduitry
Copy link
Member

This would be, if anything, the responsibility of a preprocessor, not of this plugin.

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

No branches or pull requests

3 participants