What is postcss-modules-extract-imports?
The postcss-modules-extract-imports npm package is a PostCSS plugin that helps in transforming CSS modules by extracting all local imports and creating a mapping for them. It is commonly used in a CSS module system where CSS is composed in a modular and reusable way. This plugin processes CSS files and rewrites import statements so that they can be bundled correctly by a module bundler.
What are postcss-modules-extract-imports's main functionalities?
Extracting local imports
This feature allows the plugin to extract local imports from a CSS file. When a CSS class composes from another, this plugin rewrites the import to ensure that the composed class includes the necessary styles from the imported class.
import postcss from 'postcss';
import extractImports from 'postcss-modules-extract-imports';
const css = `.className { color: green; } .anotherClass { composes: className; }`;
postcss([extractImports()])
.process(css, { from: undefined })
.then(result => {
console.log(result.css);
});
Other packages similar to postcss-modules-extract-imports
postcss-import
postcss-import is a PostCSS plugin that allows you to transform @import rules by inlining content. It is similar to postcss-modules-extract-imports in that it processes imports, but it focuses on inlining the imported content rather than extracting and mapping it for CSS modules.
postcss-easy-import
postcss-easy-import is a wrapper around postcss-import, providing additional features like glob pattern matching and the ability to prefix imports. It simplifies the import process but does not specifically target the CSS modules pattern like postcss-modules-extract-imports.
postcss-advanced-variables
postcss-advanced-variables is a plugin that allows you to use variables, conditionals, and iterators in your CSS. While it does not directly handle imports, it provides a modular approach to CSS that can complement the functionality provided by postcss-modules-extract-imports.
Transforms:
:local(.continueButton) {
composes: button from "library/button.css";
color: green;
}
into:
:import("library/button.css") {
button: __tmp_487387465fczSDGHSABb;
}
:local(.continueButton) {
composes: __tmp_487387465fczSDGHSABb;
color: green;
}
Specification
- Only a certain whitelist of properties are inspected. Currently, that whitelist is
['composes']
alone. - An extend-import has the following format:
composes: className [... className] from "path/to/file.css";
Options
failOnWrongOrder
bool
generates exception for unpredictable imports order.
.aa {
composes: b from "./b.css";
composes: c from "./c.css";
}
.bb {
composes: c from "./c.css";
composes: b from "./b.css";
}
Building
npm install
npm test
- Lines:
- Statements:
License
ISC
With thanks
- Mark Dalgleish
- Tobias Koppers
- Guy Bedford
Glen Maddern, 2015.