配置postcss.config.js(主要是tailwndcss)的规则到底是什么?

Rob*_*ing 20 vue.js postcss vue-cli tailwind-css

postcss.config.js用于展示如何配置的变体数量非常令人困惑。有一些tailwindcss使用此功能的示例(例如文档中的示例):

// Example 1:
module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}
Run Code Online (Sandbox Code Playgroud)

然后是那些需要库的:

// Example 2:
module.exports = {
  plugins: {
    require('tailwindcss'),
    require('postcss-preset-env')({
      stage: 0,
      'nesting-rules': true
    })
  },
}
Run Code Online (Sandbox Code Playgroud)

其他人在配置之前需要外部库module.exports

// Example 3:

const tailwindcss = require('tailwindcss');
const postcssPresetEnv = require('postcss-preset-env');


module.exports = {
  plugins: {
    tailwindcss,
    postcssPresetEnv
  },
}
Run Code Online (Sandbox Code Playgroud)

当必须合并未根据默认值命名的配置文件时,还有更多必要的内容。

yarn dev今天,当使用 postcss.config.js运行时,我收到此错误,如示例 2 所示:

Syntax Error: /[path]/_pod-test/postcss.config.js:3
    require('tailwindcss'),
             ^^^^^^^^^^^

SyntaxError: Unexpected string
Run Code Online (Sandbox Code Playgroud)

当我删除带有“tailwindcss”的行时,“postcss-preset-env”也会发生同样的情况:

Syntax Error: /Volumes/_III_/Z_WWW/_ZZZ PoD/_pod-test/postcss.config.js:3
    require('postcss-preset-env')({
            ^^^^^^^^^^^^^^^^^^^^

SyntaxError: Unexpected string
Run Code Online (Sandbox Code Playgroud)

当我切换到示例 1 所示的设置时,出现以下错误:

Syntax Error: Error: PostCSS plugin tailwindcss requires PostCSS 8.
Migration guide for end-users:
https://github.com/postcss/postcss/wiki/PostCSS-8-for-end-users
Run Code Online (Sandbox Code Playgroud)

我确实使用 postcss 8.3.9!

这一切都发生在一个作为 Vue2 项目设置的项目中vue-cli

我必须应用哪种巫术才能使此设置发挥作用?

Rob*_*ing 4

我发布的示例 1 和示例 2 之间有一个微妙但非常重要的区别。

例子2实际上是错误的!

示例 1 使用对象来配置插件的参数,而第二个示例则使用函数调用。这些必须放在一个数组中(意思是:用方括号代替花括号)。

这将是示例 2 的正确版本:

// Example 2 fixed:

module.exports = {
  plugins: [  // <= here we MUST use brackets!
    ... [function calls] ...
  ],
}
Run Code Online (Sandbox Code Playgroud)

我还没有测试示例 3 是否也是如此(但我假设是这样)。

希望这对某人有帮助!