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

推荐订阅源

V
Visual Studio Blog
Stack Overflow Blog
Stack Overflow Blog
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
L
LangChain Blog
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
Y
Y Combinator Blog
月光博客
月光博客
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Fortinet All Blogs
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
小众软件
小众软件
H
Help Net Security
Last Week in AI
Last Week in AI
B
Blog RSS Feed
宝玉的分享
宝玉的分享
N
Netflix TechBlog - Medium
博客园 - 叶小钗
The GitHub Blog
The GitHub 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'
        ]
      }
    ],
  },
})

这样就可以成功编译了