繁体   English   中英

如何将postcss.config.js 和tailwind.config.js 转成ES 模块?

[英]How to convert postcss.config.js and tailwind.config.js to ES modules?

我通过npm init vue@latest使用 Vite 创建了一个新的 Vue 应用程序。 我根据官方指南将TailwindCSS添加到项目中。

运行npm run lint抛出错误

错误“模块”未定义 no-undef

因为它希望postcss.config.jstailwind.config.js成为 ES 模块(我认为)。

postcss.config.js

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

export const plugins = {
  tailwindcss: {},
  autoprefixer: {},
};

tailwind.config.js来自

module.exports = {
  content: ["./index.html", "./src/**/*.{vue,js,ts,jsx,tsx}"],
  theme: {
    extend: {},
  },
  plugins: [],
};

export const content = ["./index.html", "./src/**/*.{vue,js,ts,jsx,tsx}"];
export const theme = {
  extend: {},
};
export const plugins = [];

并运行npm run dev应用程序崩溃并出现错误

[vite] 内部服务器错误:意外的令牌“导出”插件:vite:css

如何解决此 linting 错误?

您实际上可以将这些配置文件保留为 CommonJS。 ESLint 的env需要在这些配置文件中设置为node ,因为 Node 是构建期间的实际环境。

选项一:使用内联注释来配置 ESLint

postcss.config.jstailwind.config.js的顶部插入此注释:

/* eslint-env node */

选项 2:为*.config.js env环境

配置overrides以设置*.config.js env的环境:

// .eslintrc.cjs
module.exports = {
  ⋮
  overrides: [
    {
      files: ['*.config.js'],
      env: {
        node: true,
      },
    },
  ],
}

如果使用 VS Code,您可能需要重新启动 ESLint 服务器(或 IDE)以重新加载配置。

选项 3:禁用*.config.js的 ESLint 规则

ignorePatterns配置为不对这些配置文件进行 lint:

// .eslintrc.cjs
module.exports = {
  ⋮
  ignorePatterns: ['*.config.js'],
}

如果使用 VS Code,您可能需要重新启动 ESLint 服务器(或 IDE)以重新加载配置。

将文件重命名为postcss.config.cjs tailwind.config.cjs 请注意cjs中的c ,它代表 CommonJS 模块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM