To make esbuild compile CSS variables (custom properties) into their actual values in the output CSS file, you need to use a post-processing step or a plugin that resolves these variables. Esbuild itself does not natively resolve CSS variables, but you can achieve this by combining esbuild with other tools or plugins.
Here’s how you can do it:
1. Using postcss with postcss-custom-properties
One of the most common ways to resolve CSS variables is by using PostCSS with the postcss-custom-properties plugin.
Steps:
-
Install the necessary packages:
npm install --save-dev postcss postcss-custom-properties postcss-preset-env
-
Create a PostCSS configuration file (postcss.config.js):
module.exports = {
plugins: {
'postcss-custom-properties': {}, // Resolves CSS variables
},
};
-
Use esbuild with a PostCSS loader. You can use the esbuild-postcss plugin or manually integrate PostCSS into your build process.
Example using esbuild-postcss:
npm install --save-dev esbuild-postcss
Then, in your esbuild script:
const { build } = require('esbuild');
const postcss = require('esbuild-postcss');
build({
entryPoints: ['src/styles/main.css'],
outfile: 'dist/styles/main.css',
plugins: [postcss()], // Apply PostCSS transformations
}).catch(() => process.exit(1));
-
Write your CSS with variables:
:root {
--body-color: #c5c8c6;
}
body {
color: var(--body-color);
}
After processing, the output will be:
body {
color: #c5c8c6;
}
2. Using lightningcss
Another efficient way to resolve CSS variables is by using lightningcss, a fast CSS parser and transformer written in Rust.
Steps:
-
Install lightningcss:
npm install --save-dev lightningcss
-
Use lightningcss in your build script to resolve CSS variables:
const { transform } = require('lightningcss');
const { code } = transform({
filename: 'src/styles/main.css',
code: Buffer.from(
`:root { --body-color: #c5c8c6; } body { color: var(--body-color); }`
),
minify: true,
resolveVariables: true, // This resolves CSS variables
});
require('fs').writeFileSync('dist/styles/main.css', code);
Output:
body{color:#c5c8c6}
3. Combining esbuild with Custom Plugins
If you want to keep using esbuild for CSS bundling and add variable resolution, you can create a custom plugin or use a third-party plugin that handles CSS variable resolution.
Example of a custom esbuild plugin:
const { transform } = require('lightningcss');
const resolveCssVariables = () => ({
name: 'resolve-css-variables',
setup(build) {
build.onLoad({ filter: /\.css$/ }, async (args) => {
const { code } = await transform({
filename: args.path,
code: Buffer.from(await require('fs').promises.readFile(args.path)),
resolveVariables: true,
});
return { contents: code.toString(), loader: 'css' };
});
},
});
require('esbuild').build({
entryPoints: ['src/styles/main.css'],
outfile: 'dist/styles/main.css',
plugins: [resolveCssVariables()],
}).catch(() => process.exit(1));
Summary
- PostCSS with
postcss-custom-properties: A flexible and widely used solution.
- Lightningcss: A fast and efficient alternative.
- Custom esbuild plugins: For more control over the build process.
Choose the method that best fits your project's needs!