• 首页

  • 写作

  • 文章归档

  • 照片

  • 友情链接

  • 旅行

  • 读书

  • 日志

  • 随记

  • 人文历史

  • linux

  • 前端
b l o g
b l o g

admin

lzp

01月
06
前端

webpack tailwind postcss 工程

发表于 2025-01-06 • 字数统计 1492 • 被 10 人看爆

##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
分享到:
build tailwind css 项目
webpack image 是如何打包的 #webpack #react #image
  • 文章目录
  • 站点概览
admin

! lzp

hello

Github Twitter QQ Email Telegram RSS
看爆 Top5
  • 历史与人文 视频链接 189次看爆
  • 2022日志随笔 175次看爆
  • 我的青海湖骑行 164次看爆
  • 读书随笔 124次看爆
  • rs2 设置教程 97次看爆

站点已萌萌哒运行 00 天 00 小时 00 分 00 秒(●'◡'●)ノ♥

Copyright © 2025 admin

由 Halo 强力驱动 · Theme by Sagiri · 站点地图