惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
宝玉的分享
宝玉的分享
量子位
博客园 - Franky
The Cloudflare Blog
博客园 - 【当耐特】
Jina AI
Jina AI
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
V
V2EX
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Help Net Security
博客园 - 聂微东
F
Fortinet All Blogs
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
博客园_首页
人人都是产品经理
人人都是产品经理

不明のNetWorkOS

Libmodsecurity 的安装与使用 使用 GitHub 部署密钥部署代码 Supervisor 的安装与使用 PHP 中的 JIT 配置 IOC 服务容器 在 Nginx 中为 PHP 添加环境变量 Python 使用 virtualenv 部署 flask 项目 Git 基础命令 [转载] 免费 VPS 大全
在 Laravel 中的 Vue 组件无法使用 @apply 的解决方案
不明 · 2021-05-30 · via 不明のNetWorkOS

前写时间在写项目的时候使用到了 tailwindcss,该框架使用 PostCSS 作为预处理器,然而在使用 tailwindcss 的 @apply 方法时编译器出现了错误。

我的配置如下:

  • laravel 8.4
  • vue 3.0.11
  • postcss 8.3
  • tailwindcss 2.0.2

解决方案

这似乎是因为没有 postcss.config.js 文件造成的(在 tailwindcss 的官方安装方法里并没有这个在 Laravel 中安装 TailwindCSS),最终在 laravel-mix 的 GitHub 上找到了解决方案。
创建一个 postcss.config.js 文件在项目根目录中

const purgecss = require('@fullhuman/postcss-purgecss')({
  content: [
    './resources/**/*.blade.php',
    './resources/**/*.js',
    './resources/**/*.vue',
  ],
  whitelistPatterns: [ /-(leave|enter|appear)(|-(to|from|active))$/, /data-v-.*/, /v-deep/ ],
  whitelistPatternsChildren: [ /pretty$/, /xmx-.*$/, /^(.*?)\.tooltip/ ],
  defaultExtractor: content => content.match(/[\w-/.:]+(?<!:)/g) || []
})

module.exports = {
  plugins: [
      require('tailwindcss'),
      require('autoprefixer'),
      //...process.env.NODE_ENV === 'production' ? [purgecss] : []
  ]
}

请注释掉最后的生产优化,启用的话会导致预设的生产优化不起作用

然后将以下代码加入到你的 mix 配置中

.webpackConfig({
  module: {
    rules: [
      {
        test: /\.(postcss)$/,
        use: [
          'vue-style-loader',
          { loader: 'css-loader', options: { importLoaders: 1 } },
          'postcss-loader'
        ]
      }
    ],
  },
})

这样就可以成功编译了