##2024/9/24 16:59:33:
webpack tailwind postcss 工程
项目地址:
https://github.com/qyzhizi/js-logical/blob/main/2024-09-20-tailwind-postcss-webpack
项目简介
这个项目在 webpack 中配置了 postcss 与 tialwind, 将源文件的 html js 中 tailwind css 以及自定义的 css 打包,并自动绑定到 index.html。
配置文件与解释
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
clean: true,
},
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader, // 使用 MiniCssExtractPlugin 提取 CSS
'css-loader',
'postcss-loader',
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
}),
new MiniCssExtractPlugin({
filename: 'styles.css', // 输出的 CSS 文件名
}),
],
devServer: {
static: {
directory: path.resolve(__dirname, 'dist'),
},
port: 3000,
open: true,
hot: true,
},
};
这里使用了 MiniCssExtractPlugin 插件,在 rules 配置了 'css-loader', 'postcss-loader', 在 postcss 的配置文件中又配置了 tailwind , 而 tialwind 配置文件中指定了 源文件夹中需要处理的 html 与 js文件
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
}
工程目录
-> % tree -I 'node_modules' -a
.
├── .gitignore
├── dist
│ ├── bundle.js
│ ├── index.html
│ └── styles.css
├── package-lock.json
├── package.json
├── postcss.config.js
├── src
│ ├── index.html
│ ├── index.js
│ └── styles.css
├── tailwind.config.js
└── webpack.config.js
安装依赖
npm install
构建并启动静态服务器
npm bulid
npx http-server dist
开发模式
npm dev