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

Optional StyleInject — just the compiled CSS as an output #54

Closed
ciampo opened this issue Jan 8, 2018 · 12 comments
Closed

Optional StyleInject — just the compiled CSS as an output #54

ciampo opened this issue Jan 8, 2018 · 12 comments

Comments

@ciampo
Copy link

ciampo commented Jan 8, 2018

I'm trying to use this plugin to compile the styles of a Web Component.

These are the relevant bits of code in the JS entry point:

import styles from './styles.css';
// ...
template.innerHTML = `<style>${styles}</style> ${html}`;

As you can see, I just need the imported styles to be the compiled CSS string, but so far I haven't managed to get rid of the call to the __$styleInject function.

Is there a way to obtain, as an output, just the processed CSS string?

ciampo added a commit to ciampo/macro-carousel that referenced this issue Jan 8, 2018
…ix while waiting for egoist/rollup-plugin-postcss#54)

This is required to make the inline-svg plugin work.
@egoist
Copy link
Owner

egoist commented Jan 9, 2018

cc @lmihaidaniel maybe add styleInject: false to fix this?

@lmihaidaniel
Copy link
Collaborator

that will do, was next on my to-do list

@egoist
Copy link
Owner

egoist commented Jan 10, 2018

Check out inject option in v1.0

@egoist egoist closed this as completed Jan 10, 2018
@ciampo
Copy link
Author

ciampo commented Jan 11, 2018

Hi @egoist and @lmihaidaniel,

I tried to add the inject: false option, but I still get the __$$styleInject function added to my output code.

This is the configuration that I'm using:

//...
  plugins: [
    postcss({
      inject: false,
      plugins: [
        inlineSvg,
        autoprefixer({browsers: 'last 2 versions'}),
      ],
    }),

and this is the output code generated:

function __$$styleInject(css, ref) {
  if ( ref === void 0 ) ref = {};
  var insertAt = ref.insertAt;

  if (!css) { return }

  var head = document.head || document.getElementsByTagName('head')[0];
  var style = document.createElement('style');
  style.type = 'text/css';

  if (insertAt === 'top') {
    if (head.firstChild) {
      head.insertBefore(style, head.firstChild);
    } else {
      head.appendChild(style);
    }
  } else {
    head.appendChild(style);
  }

  if (style.styleSheet) {
    style.styleSheet.cssText = css;
  } else {
    style.appendChild(document.createTextNode(css));
  }
}

var css = "[...]";
__$$styleInject(css);

Ideally, I'd just like to get rid of the __$$styleInject function definition and the __$$styleInject(css); call completely, keeping only var css = ....

Would that be possible?


EDIT: looking at the source code, it looks like the inferOption(option, defaultValue) function may not behave as expected: if the value of option is false (which is what I'm setting for the inject option), then its value is set to {} (see https://github.com/egoist/rollup-plugin-postcss/blob/master/src/index.js#L15)

egoist added a commit that referenced this issue Jan 12, 2018
@egoist
Copy link
Owner

egoist commented Jan 12, 2018

Fixed in v1.0.5

@ciampo
Copy link
Author

ciampo commented Jan 12, 2018

It works now — thank you!

@nikolay-borzov
Copy link

Could you please update README to mention this behavior?

@egoist
Copy link
Owner

egoist commented Jan 12, 2018

@nikolay-borzov what needs To be updated? it's already in the options section.

@nikolay-borzov
Copy link

Right, but it doesn't say that setting inject: false excludes styleInject and keeps varaible with CSS string

@egoist
Copy link
Owner

egoist commented Jan 13, 2018

It will always export a variable with the CSS string unless extract: true, now mentioned it in usage: https://github.com/egoist/rollup-plugin-postcss#usage

@nikolay-borzov
Copy link

Thanks!

@alex996
Copy link

alex996 commented Dec 23, 2018

Is there a way to get hold of the generated styles when modules: true? Currently, it only exposes the object with class names; I'd like the actual CSS too. If you set inject: false and modules: true, the CSS gets lost and is never bundled.

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

5 participants