【问题标题】:How to convert postcss.config.js and tailwind.config.js to ES modules?如何将 postcss.config.js 和 tailwind.config.js 转换为 ES 模块?
【发布时间】:2022-02-04 10:10:17
【问题描述】:

我通过npm init vue@latest 使用 Vite 创建了一个新的 Vue 应用程序。我基于the official guide在项目中添加了TailwindCSS。

运行npm run lint 会抛出错误

error 'module' is not defined 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 错误?

【问题讨论】:

  • 你为什么要这样做?您使用的工具是否在其配置中支持 ESM?我建议不要使用与您的代码相同的规则对配置文件进行 linting。
  • 为什么?它会增加什么价值?也许这可能会有所帮助:stackoverflow.com/questions/45854169/…?
  • ok 其他问题:您将如何解决此 linting 错误? linting 时忽略这些文件?
  • 没错,你为什么要检查配置文件呢?请记住,这些文件是配置工具。可能有一些价值,但我认为现在你应该忽略它们

标签: javascript vue.js eslint tailwind-css


【解决方案1】:

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

选项 1:使用内联注释配置 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)以重新加载配置。

【讨论】:

    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 2021-06-22
    • 2022-07-11
    • 2012-02-19
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多