Skip to content

esbuild-plugin-css-import ​

Resolve CSS imports using the node resolution algorithm and the style field in package.json.

DANGER

This plugin development is deprecated and discontinued, as we are moving away from the custom style resolution field in package.json in favor of the ./style export specifier.

Why ​

CSS modules resolution is not standardized, so it's not possible to natively import CSS files from node_modules/ using the @import statement.

This plugin enables the node resolution algorithm for CSS files. That means that @import and @url() statements can refer to both relative files and NPM packages. CSS modules must have the style field in their package.json in order to correctly pickup the CSS entrypoint.

Install ​

sh
npm i -D @chialab/esbuild-plugin-css-import
sh
yarn add -D @chialab/esbuild-plugin-css-import
sh
pnpm add -D @chialab/esbuild-plugin-css-import

Usage ​

ts
import cssImportPlugin from '@chialab/esbuild-plugin-css-import';
import esbuild from 'esbuild';

await esbuild.build({
    plugins: [cssImportPlugin()],
});

Example ​

css
@import 'toggle';

body {
    color: var(--accent-color);
}
json
{
    "name": "toggle",
    "main": "index.js",
    "style": "index.css",
    "exports": {
        ".": {
            "style": "./index.css",
            "default": "./index.js"
        }
    }
}
css
:root {
    --accent-color: #000;
}

Build tools for Web Components.