nano-css icon indicating copy to clipboard operation
nano-css copied to clipboard

Webpack plugin for extract

Open MindaugasVarkalys opened this issue 6 years ago • 4 comments

Is there any plugin for Webpack to extract static CSS?

The current solution to extract CSS using Node script is very inconvenient because it cannot be added to Webpack build.

MindaugasVarkalys avatar Jan 16 '19 15:01 MindaugasVarkalys

I was looking into this a bit to possibly make my own plugin, but unfortunately it seems like nano-css isn't really statically analyzable, which makes it difficult to put together into a webpack plugin.

Looking at the example, the nano-css/addon/extract addon relies on the code to actually execute for the addon to generate the CSS. I don't think there's really a way for this to work nicely inside a webpack plugin to my knowledge.

I don't have too much experience with webpack plugins, so perhaps there's another, better way to go about this?

devmattrick avatar Mar 20 '19 06:03 devmattrick

A "simple" way of achieving it would be:

  1. Put all nano-css styling in separate files, with *.css.js extension.
  2. Write a script or a Webpack plugin that executes all *.css.js files.
  3. Once executed, you will have raw CSS in nano.raw property.

As a side not, you may not need to "extract" CSS into a separate *.css file. If you do static file generation or server side rendering you may want to consider inlining the CSS by simply:

const html = `
  <html>
    ...
    <style>${nano-raw}</style>
    ...
  </html>
`;

streamich avatar Mar 20 '19 10:03 streamich

It's quite possible to make one. I made one for Rollup: https://bitbucket.org/TheBosZ/rollup-plugin-nano-css/

TheBosZ avatar Apr 05 '19 16:04 TheBosZ

I've created a loader that does the same thing as my Rollup plugin: https://www.npmjs.com/package/nano-css-loader

TheBosZ avatar May 09 '19 16:05 TheBosZ