简体   繁体   English

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

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

I created a new Vue app using Vite via npm init vue@latest .我通过npm init vue@latest使用 Vite 创建了一个新的 Vue 应用程序。 I added TailwindCSS to the project based on the official guide .我根据官方指南将TailwindCSS添加到项目中。

Running npm run lint throws errors运行npm run lint抛出错误

error 'module' is not defined no-undef错误“模块”未定义 no-undef

because it wants postcss.config.js and tailwind.config.js to be ES modules (I think).因为它希望postcss.config.jstailwind.config.js成为 ES 模块(我认为)。

When converting postcss.config.js frompostcss.config.js

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

to

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

and tailwind.config.js fromtailwind.config.js来自

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

to

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

and running npm run dev the app crashes with the error并运行npm run dev应用程序崩溃并出现错误

[vite] Internal server error: Unexpected token 'export' Plugin: vite:css [vite] 内部服务器错误:意外的令牌“导出”插件:vite:css

How do I solve this linting error?如何解决此 linting 错误?

You can actually keep those config files as CommonJS.您实际上可以将这些配置文件保留为 CommonJS。 ESLint's env needs to be set to node in those config files because Node is the actual environment during the build. ESLint 的env需要在这些配置文件中设置为node ,因为 Node 是构建期间的实际环境。

Option 1: Use inline comment to configure ESLint选项一:使用内联注释来配置 ESLint

Insert this comment at the top of postcss.config.js and tailwind.config.js :postcss.config.jstailwind.config.js的顶部插入此注释:

/* eslint-env node */

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

Configure overrides to set env for *.config.js files:配置overrides以设置*.config.js env的环境:

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

If using VS Code, you'll likely need to restart the ESLint server (or the IDE) to reload the configuration.如果使用 VS Code,您可能需要重新启动 ESLint 服务器(或 IDE)以重新加载配置。

Option 3: Disable ESLint rules for *.config.js选项 3:禁用*.config.js的 ESLint 规则

Configure ignorePatterns to not lint these config files: ignorePatterns配置为不对这些配置文件进行 lint:

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

If using VS Code, you'll likely need to restart the ESLint server (or the IDE) to reload the configuration.如果使用 VS Code,您可能需要重新启动 ESLint 服务器(或 IDE)以重新加载配置。

Rename the files to postcss.config.cjs resp.将文件重命名为postcss.config.cjs tailwind.config.cjs . tailwind.config.cjs Note the c in cjs , which stands fro CommonJS Module.请注意cjs中的c ,它代表 CommonJS 模块。

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

相关问题 将文件导入 Tailwind.Config.JS - Importing a File Into Tailwind.Config.JS 在 Vite2 中,如何在 tailwind.config.js 中导入 ESModule - In Vite2, How to import an ESModule in tailwind.config.js webpack4如何在生产模式下配置postcss.config.js文件 - webpack4 how to config postcss.config.js file in production mode 未生成来自 tailwind.config.js 的自定义顺风 colors - Custom tailwind colors from tailwind.config.js not generated 如何在 postcss.config.js 中为不同的 Webpack 入口点配置不同的 customProperties? - How can I configure different customProperties in postcss.config.js for different Webpack entry points? 如何在我的“tailwind.config.js”中添加 2 个 html 路径? 尾风css - How can I add 2 html paths in my "tailwind.config.js"? Tailwindcss Webpack 2-导入会在postcss.config.js和其他js文件上引发错误 - Webpack 2 - import throws error on postcss.config.js and other js files X [错误] [plugin esbuild-style-plugin] E:\...\postcss.config.js 的 PostCSS 配置文件无法加载 - X [ERROR] [plugin esbuild-style-plugin] PostCSS config file at E:\...\postcss.config.js can't load Nuxt 中的 Tailwind PostCss 配置 - Tailwind PostCss Config in Nuxt 如何在 Next js 自定义 postCSS 配置中配置 Scss 和 purgeCss - How to config Scss and purgeCss in Next js custom postCSS config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM