I got an answer from Vite Discord channel. This is the solution to convert postcss and tailwindcss config files to ESModule.
Do this, and you can use import in those config files.
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: [],
}
postcss.config.js
import tailwind from 'tailwindcss'
import autoprefixer from 'autoprefixer'
import tailwindConfig from './tailwind.config.js'
export default {
plugins: [tailwind(tailwindConfig), autoprefixer],
}
vite.config.js I added import postcss from './postcss.config.js' and
css: {
postcss,
},
Answer from Shook Lyngs on Stack OverflowTailwind.config.js and require vs import / ESM
How to import ES module in Tailwind config file?
Require() of ES Module prettier-plugin-tailwindcss/dist/index.mjs not supported
ESM Syntax causing extension to not work.
According to the team, they won't be supporting ESM anytime soon.
https://github.com/tailwindlabs/tailwindcss/issues/8029#issuecomment-1086875656
Using ESM is now supported in tailwind.config.js. However, it is still the case that the configuration file needs to be called tailwind.config.js, so renaming it to tailwind.config.mjs will cause it not to load.
So, the solution is adding "type": "module" to package.json, and making sure that all your other dependencies use ESM modules. If they don’t, you can try opening a GitHub issue asking for it to be implemented…