Giter VIP home page Giter VIP logo

windicss-webpack-plugin's Introduction

windicss-webpack-plugin

๐Ÿƒ Windi CSS for webpack๏ธ
Next generation utility-first CSS framework.

Features

  • ๐Ÿงฉ On-demand CSS utilities (Compatible with Tailwind CSS v2)
  • ๐Ÿ“ฆ On-demand native elements style reseting
  • ๐Ÿ”ฅ Hot module replacement (HMR)
  • ๐Ÿƒ Load configurations from tailwind.config.js
  • ๐Ÿค Framework-agnostic: Vue CLI, Nuxt, Next, UmiJS, etc!
  • ๐Ÿ“„ Use @apply / @screen directives in any file: Less, SCSS, SASS, PostCSS, Stylus
  • ๐ŸŽณ Support Utility Groups - e.g. bg-gray-200 hover:(bg-gray-100 text-red-300)

Install

yarn add windicss-webpack-plugin -D 
# npm i windicss-webpack-plugin -D

webpack.config.js

If you have access to modify the webpack.config.js directly, then you can do the following.

// webpack.config.js
import WebpackWindiCSSPlugin from 'windicss-webpack-plugin'

export default {
  // ...
  plugins: [
    new WebpackWindiCSSPlugin()
  ],
};
// main.js
import 'windi.css'

Other examples

See ./example

That's all. Build your app just like what you would do with Tailwind CSS, but much faster! โšก๏ธ

Migration

If you are already using Tailwind CSS for your app, please consult the documentation on migrating.

All set.

That's all, fire up your app and enjoy the speed!

TypeScript

You can use TypeScript for your config file if you're using esbuild.

Simply rename your config it to tailwind.config.ts.

// tailwind.config.ts
import { defineConfig } from 'windicss-webpack-plugin'

export default defineConfig({
  darkMode: 'class',
  theme: {
    extend: {
      colors: {
        teal: {
          100: '#096',
        },
      },
    },
  },
})

Safelist

By default, we scan your source code statically and find all the usages of the utilities then generated corresponding CSS on-demand. However, there is some limitation that utilities that decided in the runtime can not be matched efficiently, for example

<!-- will not be detected -->
<div className={`p-${size}`}>

For that, you will need to specify the possible combinations in the safelist options of windi.config.ts.

// windi.config.ts
import { defineConfig } from 'windicss-webpack-plugin'

export default defineConfig({
  safelist: 'p-1 p-2 p-3 p-4'
})

Or you can do it this way

// windi.config.ts
import { defineConfig } from 'windicss-webpack-plugin'

function range(size, startAt = 1) {
  return Array.from(Array(size).keys()).map(i => i + startAt);
}

export default defineConfig({
  safelist: [
    range(30).map(i => `p-${i}`), // p-1 to p-3
    range(10).map(i => `mt-${i}`) // mt-1 to mt-10
  ]
})

Scanning

On server start, windicss-webpack-plugin will scan your source code and extract the utilities usages. By default, only files under src/ with extensions vue, html, mdx, pug, jsx, tsx will be included. If you want to enable scanning for other file type of locations, you can configure it via:

// windi.config.js
import { defineConfig } from 'windcss/helpers'

export default defineConfig({
  extract: {
    include: ['src/**/*.{vue,html,jsx,tsx}'],
    exclude: ['node_modules', '.git']
  }
})

Or in plugin options:

// webpack.config.js
import WebpackWindiCSSPlugin from 'windicss-webpack-plugin'

export default {
  // ...
  plugins: [
    new WebpackWindiCSSPlugin({
      scan: {
        dirs: ['.'], // all files in the cwd
        fileExtensions: ['vue', 'js', 'ts'], // also enabled scanning for js/ts
      },
    })
  ],
};

More

See options.ts for more configuration reference.

Credits

  • Windy team
  • @antfu Based on his Rollup / Vite implementation & his util package

License

MIT License ยฉ 2021 Harlan Wilton

windicss-webpack-plugin's People

Contributors

albert-cord avatar dependabot-preview[bot] avatar hannoeru avatar harlan-zw avatar mohatt avatar noxify avatar schelmo avatar zhuangya avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.