Tailwind在webpack现有项目中不工作

plicqrtu  于 2023-08-06  发布在  Webpack
关注(0)|答案(1)|浏览(139)

我已经通过tailwind网站上提到的官方安装指南使用npm在现有的webpack项目中安装了tailwind,但是当我在html页面中应用属性时,它就不起作用了。
webpack.prod.js文件:

const { merge } = require('webpack-merge')
const commonConfiguration = require('./webpack.common.js')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')

module.exports = merge(
    commonConfiguration,
    {
        mode: 'production',
        plugins:
        [
            new CleanWebpackPlugin()
        ]
    }
)

字符串
webpack.dev.js文件:

const path = require('path')
const { merge } = require('webpack-merge')
const commonConfiguration = require('./webpack.common.js')
const ip = require('internal-ip')
const portFinderSync = require('portfinder-sync')

const infoColor = (_message) =>
{
    return `\u001b[1m\u001b[34m${_message}\u001b[39m\u001b[22m`
}

module.exports = merge(
    commonConfiguration,
    {
        stats: 'errors-warnings',
        mode: 'development',
        devServer:
        {
            host: 'local-ip',
            port: portFinderSync.getPort(8080),
            open: true,
            https: false,
            allowedHosts: 'all',
            hot: false,
            watchFiles: ['src/**', 'static/**'],
            static:
            {
                watch: true,
                directory: path.join(__dirname, '../static')
            },
            client:
            {
                logging: 'none',
                overlay: true,
                progress: false
            },
            onAfterSetupMiddleware: function(devServer)
            {
                const port = devServer.options.port
                const https = devServer.options.https ? 's' : ''
                const localIp = ip.v4.sync()
                const domain1 = `http${https}://${localIp}:${port}`
                const domain2 = `http${https}://localhost:${port}`
                
                console.log(`Project running at:\n  - ${infoColor(domain1)}\n  - ${infoColor(domain2)}`)
            }
        }
    }
)


webpack.common.js文件:

const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCSSExtractPlugin = require('mini-css-extract-plugin')
const path = require('path')

module.exports = {
    entry: path.resolve(__dirname, '../src/script.js'),
    output:
    {
        hashFunction: 'xxhash64',
        filename: 'bundle.[contenthash].js',
        path: path.resolve(__dirname, '../dist')
    },
    devtool: 'source-map',
    plugins:
    [
        new CopyWebpackPlugin({
            patterns: [
                { from: path.resolve(__dirname, '../static') }
            ]
        }),
        new HtmlWebpackPlugin({
            template: path.resolve(__dirname, '../src/index.html'),
            minify: true
        }),
        new MiniCSSExtractPlugin()
    ],
    module:
    {
        rules:
        [
            // HTML
            {
                test: /\.(html)$/,
                use:
                [
                    'html-loader'
                ]
            },

            // JS
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use:
                [
                    'babel-loader'
                ]
            },

            // CSS
            {
                test: /\.css$/,
                use:
                [
                    MiniCSSExtractPlugin.loader,
                    'css-loader'
                ]
            },

            // Images
            {
                test: /\.(jpg|png|gif|svg)$/,
                type: 'asset/resource',
                generator:
                {
                    filename: 'assets/images/[hash][ext]'
                }
            },

            // Fonts
            {
                test: /\.(ttf|eot|woff|woff2)$/,
                type: 'asset/resource',
                generator:
                {
                    filename: 'assets/fonts/[hash][ext]'
                }
            },
            //shaders
            {
                test: /\.(glsl|vs|fs|vert|frag)$/,
                exclude: /node_modules/,
                use:[ 
                    'raw-loader'
                ]
            }
        ]
    }
}


package.json文件:

{
    "repository": "#",
    "license": "UNLICENSED",
    "scripts": {
        "build": "webpack --config ./bundler/webpack.prod.js",
        "dev": "webpack serve --config ./bundler/webpack.dev.js"
    },
    "dependencies": {
        "@babel/core": "^7.15.8",
        "@babel/preset-env": "^7.15.8",
        "babel-loader": "^8.2.3",
        "clean-webpack-plugin": "^4.0.0",
        "copy-webpack-plugin": "^9.0.1",
        "css-loader": "^6.5.0",
        "dat.gui": "^0.7.9",
        "file-loader": "^6.2.0",
        "gsap": "^3.10.4",
        "html-loader": "^3.0.0",
        "html-webpack-plugin": "^5.5.0",
        "mini-css-extract-plugin": "^2.4.3",
        "portfinder-sync": "0.0.2",
        "raw-loader": "^4.0.2",
        "stats.js": "^0.17.0",
        "style-loader": "^3.3.1",
        "three": "^0.134.0",
        "webpack": "^5.60.0",
        "webpack-cli": "^4.9.1",
        "webpack-dev-server": "^4.4.0",
        "webpack-merge": "^5.8.0"
    },
    "devDependencies": {
        "autoprefixer": "^10.4.14",
        "postcss": "^8.4.24",
        "tailwindcss": "^3.3.2"
    }
}


postcss.config.js文件:

module.exports = {
    plugins: {
      tailwindcss: {},
      autoprefixer: {},
    }
  }


tailwind.config.js文件:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ["./src/**/*.{html,js}"],
  theme: {
    extend: {},
  },
  plugins: [],
}


我想我确实有不同的webpack配置,但不知道如何根据顺风css修复。

gmol1639

gmol16391#

你需要在构建过程中运行PostCSS,这样Tailwind才能运行。目前,这不会发生,但我们可以通过以下方式集成PostCSS:
1.安装postcss-loader

$ npm install postcss-loader --save-dev

字符串
1.将postcss-loader添加到CSS的加载器中,以便在css-loader之前运行:

{
  test: /\.css$/,
  use:
  [
    MiniCSSExtractPlugin.loader,
    'css-loader',
    'postcss-loader'
  ]
},


作为其他健全性检查:

  • 确保在导入的CSS中,有@tailwind语句:
@tailwind base;
@tailwind components;
@tailwind utilities;

  • 确保content文件的globs充分覆盖了包含Tailwind类的源代码文件。

相关问题