Gathering detailed insights and metrics for postcss-nested-once
Gathering detailed insights and metrics for postcss-nested-once
Gathering detailed insights and metrics for postcss-nested-once
Gathering detailed insights and metrics for postcss-nested-once
npm install postcss-nested-once
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1 Stars
6 Commits
1 Watching
1 Branches
1 Contributors
Updated on 06 Jan 2024
JavaScript (94.95%)
CSS (5.05%)
Cumulative downloads
Total Downloads
Last day
-56.4%
65
Compared to previous day
Last week
-19.1%
500
Compared to previous week
Last month
-18.9%
2,362
Compared to previous month
Last year
560%
15,246
Compared to previous year
1
1
This plugin allows using Sass-like nested rules in combination with css-modules by rollup-plugin-styles.
Specifically, it solves the problem of the ampersand-combined selectors, i.e.:
1// styles.css 2.list { 3 color: red; 4 5 &_item { 6 color: green; 7 } 8}
Results in:
1// some-module.js 2import styles from "./styles.css"; 3 4// with any setup: 5console.log(styles.list); // => "styles_list__HASH" 6 7// with postcss-nested plugin: 8console.log(styles.list_item); // => undefined , 9 10// with postcss-nested-once plugin: 11console.log(styles.list_item); // => "styles_list_item__HASH"
Install:
1yarn add postcss-nested-once -D
It's intended to replace postcss-nested for the following rollup configuration:
1// rollup.config.js 2 3// ... 4const stylesRollupPlugin = require("rollup-plugin-styles"); 5const postcssNestedOncePlugin = require("postcss-nested-once"); 6 7module.exports = { 8 // ... 9 plugins: [ 10 // ... 11 stylesRollupPlugin({ 12 // ... 13 mode: "inject", 14 modules: true, 15 plugins: [ 16 // ... 17 postcssNestedOnce(), 18 ], 19 }), 20 ], 21};
Assuming the following source:
1// styles.css 2.parent { 3 color: red; 4 5 & .child { 6 color: green; 7 } 8} 9 10.list { 11 color: red; 12 13 &_item { 14 color: green; 15 } 16}
This will produce:
1// styles.js 2// ... 3var css = 4 ".styles_parent__HASH {" + 5 " color: red" + 6 "}" + 7 "" + 8 " .styles_parent__HASH .styles_child__HASH {" + 9 " color: green;" + 10 " }" + 11 "" + 12 ".styles_list__HASH {" + 13 " color: red" + 14 "}" + 15 "" + 16 ".styles_list_item__HASH {" + 17 " color: green;" + 18 " }" + 19 ""; 20var modules = { 21 parent: "styles_parent__HASH", 22 child: "styles_child__HASH", 23 list: "styles_list__HASH", 24 list_item: "styles_list_item__HASH", 25}; 26injectCss["default"](css, {}); 27 28exports.css = css; 29exports.default = modules;
Which in turn allows to use all the four classes in js:
1// some-module.js 2import styles from "./styles.css"; 3 4console.log(styles.parent); // => "styles_parent__HASH" 5console.log(styles.child); // => "styles_child__HASH" 6console.log(styles.list); // => "styles_list__HASH" 7console.log(styles.list_item); // => "styles_list_item__HASH"
The rollup-plugin-styles provides an ability to use css modules by simply specifying modules: true | ModulesOptions
during configuration.
Under the hood it does not rely on the postcss-modules package directly, but introduces its own plugins pipeline instead:
// built-in plugins
styles-import - internal plugin, uses 'Once' hook, used only if the 'import' option is enabled;
styles-url - internal plugin, uses 'Once' hook, used only if the 'url' option is enabled;
// bunch of plugins from options.plugins
postcss-nested - could be listed here, if specified
plugin-from-options #1
plugin-from-options #2
...
// bunch of plugins from postcss.config.js
postcss-nested - or here, if specified
plugin-from-postcss-config #1
plugin-from-postcss-config #2
...
// css-modules-related plugins
postcss-modules-values - dependency plugin, uses 'Once' hook
postcss-modules-local-by-default - dependency plugin, uses 'Once' hook
postcss-modules-extract-imports - dependency plugin, uses 'Once' hook
postcss-modules-scope - dependency plugin, uses 'Once' hook
styles-icss - internal plugin involved in resulting exports generation, uses 'OnceExit' hook
By that far it seems like everything should work as expected due to proper plugin's order.
So to make the next guess it's good to know the responsibility of every plugin. To cut the long story short:
postcss-modules-values
extracts @value XX
and @value YY from
into corresponding internal :import {}
/ :export {}
selectors and gives local names;postcss-modules-local-by-default
wraps every suitable css selector in internal :local
directive;postcss-modules-extract-imports
is responsible for the compose
feature;postcss-modules-scope
among other actions generates :export {}
directives for every :local
selector;styles-icss
fills special object from the contents of every :export {}
directive.The object formed by styles-icss
is used further down the pipeline to write exports from the generated styles.js
file (which are consumed by import styles from './styles.css''
).
As a result, for the above input we'll get the following output:
1// styles.js (generated) 2var css = 3 ".styles_parent__HASH {" + 4 " color: red" + 5 "}" + 6 "" + 7 " .styles_parent__HASH .styles_child__HASH {" + 8 " color: green;" + 9 " }" + 10 "" + 11 ".styles_list__HASH {" + 12 " color: red" + 13 "}" + 14 "" + 15 ".styles_list__HASH_item {" + 16 " color: green;" + 17 " }" + 18 ""; 19var modules = { 20 parent: "styles_parent__HASH", 21 child: "styles_child__HASH", 22 list: "styles_list__HASH", 23}; 24injectCss["default"](css, {}); 25 26exports.css = css; 27exports.default = modules;
So we have an actual rule .styles_list__HASH_item
(which will be injected during the import), but do not have the corresponding export (making styles.list_item === undefined
at runtime).
The key hint is that _item
suffix is added after the __HASH
part, which means that postcss-nested
transformation runs after the postcss-modules-scope
transformation. This happens because postcss-nested
plugin uses Rule
hook while other ones (mostly) use Once
+ walk()
combination which comes first.
So the most simple solution is to move postcss-nested
's logic to the same Once
hook, which resulted in postcss-nested-once
plugin.
For the sake of simple maintenance this plugin lists postcss-nested
as dependency and reuses it by calling root.walkRules((rule) => { postcssNestedInstance.Rule(rule, postcssAPI); });
in Once
hook.
It accepts (and passes down) the same options as postcss-nested
.
Type definitions are copy-pasted from the original plugin.
No vulnerabilities found.
No security vulnerabilities found.