mrnocreativity / postcss-critical-split

A PostCSS plugin that takes existing CSS files and splits out the annotated critical styles into a seperate file.
MIT License
89 stars 6 forks source link

Empty files created #15

Closed patric-eberle closed 5 years ago

patric-eberle commented 5 years ago

It seems to me like postcss-critical-split also creates files without content. We have a setup with print styles. While the /* print */ blocks are extracted correctly, when working with webpack chunks, some of the chunks don't contain this blocks, but I get still a x.print.css file without content. Is there a way to prevent "empty file output" or is this probably even a bug?

My postcss-critical-split setup in combination with post-css-pipeline-webpack-plugin

      // Create print CSS
      pluginCollection.push(new PostCssPipelineWebpackPlugin({
        suffix: 'print',
        pipeline: getPostCSSPipeline('critical', 'print', 'print:start', 'print:end'),
      }));

      // Create app CSS
      pluginCollection.push(new PostCssPipelineWebpackPlugin({
        suffix: '', 
        pipeline: getPostCSSPipeline('rest', 'print'),
      }));
patric-eberle commented 5 years ago

Example for output:

shop.app.2a79319698422fa1b963.css
shop.app.2a79319698422fa1b963.print.css
shop.module.cart.d03322c1b9a00b22bd72.css
shop.module.cart.d03322c1b9a00b22bd72.print.css

where the module.cart.x.print.css has no content.

mrnocreativity commented 5 years ago

To my mind that is expected behavior: the postcss plugin will always return a result (even if that result is 'no css at all'). I think you should check for the output before creating the files (or delete empty files later on).

I'm not sure how the pipeline plugin works so I can't speak for them. Perhaps, they could provide an option that disallows creation of empty files?

mrnocreativity commented 5 years ago

@patric-eberle, what do you think?

patric-eberle commented 5 years ago

@mrnocreativity thank you for your answer. If it was a configurable option (drop output if no content is available) I would have seen it more on this side, since the pipline only executes mutiple postCSS scripts and most likely won‘t care what they are doing. I guess we will just have to deal with this behaviour then.