How to compile taiwindcss v4 programatically using esbuild (or by other means)? #15881
-
Greetings. I am looking to update my tooling to support tailwindcss version 4. Previously I was compiling it with esbuild via the following script which I can run from node directly (not from the command line): import { build } from 'esbuild';
import autoprefixer from 'autoprefixer';
import { readFile } from 'node:fs/promises';
import postcss from 'postcss';
import postcssImport from 'postcss-import';
import tailwindcss from 'tailwindcss';
function postcssPlugin({ configFile }) {
return {
name: 'postcss-plugin',
setup(build) {
build.onLoad({ filter: /.\.css$/ }, async (args) => {
const css = await readFile(args.path, { encoding: 'utf-8' });
const result = await postcss(
[
postcssImport(),
tailwindcss({ config: configFile }),
autoprefixer()
])
.process(css, { from: args.path })
return {
loader: 'css',
contents: result.css
}
})
}
}
}
async function buildTailwind({ outFile, entryPoints, configFile = './tailwind.config.js' }) {
return await build({
entryPoints: entryPoints,
bundle: true,
outfile: outFile,
plugins: [postcssPlugin({ configFile })],
});
}
await buildTailwind({
entryPoints: ['path/to/tailwind.css'],
outfile: 'path/to/compiled.css'
}) Since the configuration now lives in the main tailwind.css file itself, and I also read somewhere that postcss-import is no longer needed, I am looking to migrate and update my scripts accordingly. I can't find much if any documentation on this topic, so I am asking here for help if someone can point me in the right direction. Also, please forgive my ignorance on the topic, but I've read in the v4 announcement post that lighting-css is now bundled within tailwind itself, or something along those lines. If I can use that directly for compilation, then I don't insist on using esbuild. Cheers! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 12 replies
-
You could consider using the CLI, either through |
Beta Was this translation helpful? Give feedback.
You could consider using the CLI, either through
node:child_process
or taking its code as inspiration. Otherwise, you can use the@tailwindcss/postcss
PostCSS plugin.