Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split generated CSS code form Tailwind?

I am looking for a solution to split the big CSS file generated by Tailwind PostCSS plugin (combined with purgecss plugin) into multiple CSS files (one CSS file per consumer JS file). Consumed CSS rules by JS files can be detected by looking into used selectors in JS files (i.e. class names such as p-1 and m-1).

Any idea how to achieve this or something similar?

like image 645
geeko Avatar asked Jun 22 '19 10:06

geeko


People also ask

How do you split a row in Tailwind CSS?

Tailwind lets you conditionally apply utility classes in different states using variant modifiers. For example, use hover:divide-y-8 to only apply the divide-y-8 utility on hover. For a complete list of all available state modifiers, check out the Hover, Focus, & Other States documentation.

Is Tailwind CSS heavy?

The default Tailwind configuration comes with 36.4KB minified and g-zipped. Compared to Bootstrap at 22.1KB , Tailwind is 14.3KB heavier.

What is CSS code splitting?

Code splitting is the splitting of code into various bundles or components which can then be loaded on demand or in parallel. As an application grows in complexity or is maintained, CSS and JavaScripts files or bundles grow in byte size, especially as the number and size of included third-party libraries increases.

Can you mix CSS with Tailwind?

We use CSS variables extensively within Tailwind itself, so if you can use Tailwind, you can use native CSS variables.


1 Answers

I was partially able to solve this.

First of all, remove all tailwind code from postcss.config.js. Then create tailwind.config.js in the folder with the file, that is using tailwind classes.

Here's my basic demo example.

In the folder there are two files:

App.vue
tailwind.config.js

App.vue:

<template>
  <div
    id="app"
    class="flex flex-col flex-no-wrap items-center content-center justify-center"
  >
    {{ message }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: "I'm from vue file"
    };
  }
};
</script>

<style lang="scss">
@import "scss/dynamic/tailwind.scss";

#app {
  color: $red;
}
</style>

scss/dynamic/tailwind.scss (to have easy acces to tailwind in any file):

@tailwind components;
@tailwind utilities;

tailwind.config.js:

module.exports = {
  purge: {
    mode: "all",
    content: [__dirname + "/App.vue"]
  },
  theme: {},
  variants: {},
  plugins: []
};

webpack.config.js (only css part):

...
{
  test: /\.(sa|sc|c)ss$/,
  use: [
    MiniCssExtractPlugin.loader,
    "css-loader",
    {
      loader: "postcss-loader",
      options: {
        plugins: env => {
          const dir = env._module.context;
          const config = dir + "/tailwind.config.js";

          return fs.existsSync(config)
            ? [require("tailwindcss")(config)]
            : [];
        }
      }
    },
    {
      loader: "sass-loader",
      options: {
        data: `@import "scss/static/abstracts";`
      }
    }
  ]
}
...

In the end I have a css file, that is loaded dynamically, and looks like this:

.flex{display:flex}.flex-col{flex-direction:column}.flex-no-wrap{flex-wrap:nowrap}.items-center{align-items:center}.justify-center{justify-content:center}.content-center{align-content:center}#app{color:red}

Problems, that I still have:

  • If multiple files (that are loaded dynamically on one page) use same classes, css code is repeated
  • I need to have multiple config files + only one config file per folder.
  • The config file for main app.scss doesn't see classes in .blade.php files, maybe it has something to do with the files path:
const path = require("path");

module.exports = {
  purge: [
    path.resolve("../../../../views/base.blade.php"),
    path.resolve("../../../../views/page/home.blade.php")
  ],
  theme: {},
  variants: {},
  plugins: []
};
like image 177
Prolomzzz Avatar answered Sep 21 '22 21:09

Prolomzzz