在Vite2中,如何在tailwind.config.js中导入ESModule

Sho*_*ngs 17 javascript tailwind-css vite

构建 Vite2 应用程序。

试图导入一个ESModulein tailwind.config.js. 该模块的导出方式如下:

export default xxx;
Run Code Online (Sandbox Code Playgroud)

然后我导入了该模块,如下tailwind.config.js所示:

const xx = require('./xx/xxx');
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误:

[plugin:vite:css] Cannot use import statement outside a module
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

Sho*_*ngs 37

我从 Vite Discord 频道得到了答复。这是将 postcss 和 tailwindcss 配置文件转换为 ESModule 的解决方案。

执行此操作,您就可以import在这些配置文件中使用。

tailwind.config.js

export default {
  purge: ['./*.html', './src/**/*.{vue,js,ts,jsx,tsx,css}'],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}
Run Code Online (Sandbox Code Playgroud)

postcss.config.js

import tailwind from 'tailwindcss'
import autoprefixer from 'autoprefixer'
import tailwindConfig from './tailwind.config.js'

export default {
  plugins: [tailwind(tailwindConfig), autoprefixer],
}
Run Code Online (Sandbox Code Playgroud)

我添加了 vite.config.jsimport postcss from './postcss.config.js'

css: {
  postcss,
},
Run Code Online (Sandbox Code Playgroud)

  • 我不知道为什么Tailwind Vue 3和Vite官方安装指南https://tailwindcss.com/docs/guides/vite没有提到这些必要的步骤? (3认同)