@moluoxixi/css-module-global-root-plugin 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/es/index.d.ts CHANGED
@@ -1,7 +1,73 @@
1
1
  import { Plugin } from 'postcss';
2
+ /**
3
+ * PostCSS 插件配置选项
4
+ */
2
5
  export interface CssModuleGlobalRootPluginOptions {
6
+ /**
7
+ * 是否移除 :root
8
+ * - true: 移除 :root,查找选择器中的 `:global :root`,替换为 `:global`(默认)
9
+ * 例如:.root :global :root -> .root :global
10
+ * - false: 将 :root 替换为 *
11
+ * 例如:.root :root -> .root *
12
+ */
3
13
  removeRoot?: boolean;
4
14
  }
15
+ /**
16
+ * PostCSS 插件:处理 CSS Module 文件中的 :root 选择器
17
+ *
18
+ * 功能说明:
19
+ * - 仅处理后缀为 .module.css/.module.scss/.module.less/.module.sass/.module.styl 的 CSS Module 文件
20
+ * - 如果选择器是 `:global :root` 开头,则不处理(保持原样)
21
+ * - 根据配置进行替换:
22
+ * - removeRoot = true: 查找选择器中的 `:global :root`,替换为 `:global`(移除 `:root`)(默认)
23
+ * - removeRoot = false: 直接替换所有 `:root` 为 `*`
24
+ * - 不修改其他任何选择器、CSS 变量、样式属性等内容
25
+ *
26
+ * 使用场景:
27
+ * 在 CSS Module 中,当需要在 :global() 作用域内定义全局 CSS 变量时,
28
+ * 使用 :root 会导致选择器过于具体,可以通过替换为 * 或直接移除 :root 来调整作用域。
29
+ *
30
+ * 示例(removeRoot = true,默认):
31
+ * 输入(CSS Module 处理后):
32
+ * .root :global :root {
33
+ * --primary: #007bff;
34
+ * }
35
+ * .root :root {
36
+ * --secondary: #6c757d;
37
+ * }
38
+ *
39
+ * 输出:
40
+ * .root :global {
41
+ * --primary: #007bff;
42
+ * }
43
+ * .root {
44
+ * --secondary: #6c757d;
45
+ * }
46
+ *
47
+ * 示例(removeRoot = false):
48
+ * 输入(CSS Module 处理后):
49
+ * .root :global :root {
50
+ * --primary: #007bff;
51
+ * }
52
+ * .root :root {
53
+ * --secondary: #6c757d;
54
+ * }
55
+ *
56
+ * 输出:
57
+ * .root :global * {
58
+ * --primary: #007bff;
59
+ * }
60
+ * .root * {
61
+ * --secondary: #6c757d;
62
+ * }
63
+ *
64
+ * 注意:
65
+ * - `:global :root` 开头的选择器不会被处理,保持原样
66
+ * - 只处理 CSS Module 文件,不影响其他文件中的 :global 使用
67
+ *
68
+ * @param options 插件配置选项
69
+ * @returns {Plugin} PostCSS 插件实例
70
+ */
5
71
  declare function cssModuleGlobalRootPlugin(options?: CssModuleGlobalRootPluginOptions): Plugin;
6
72
  declare namespace cssModuleGlobalRootPlugin {
7
73
  var postcss: boolean;
package/es/index.mjs CHANGED
@@ -2,6 +2,10 @@ function cssModuleGlobalRootPlugin(options = {}) {
2
2
  const { removeRoot = true } = options;
3
3
  return {
4
4
  postcssPlugin: "css-module-global-root",
5
+ /**
6
+ * 使用 Once 钩子,在 PostCSS 处理完所有规则后执行
7
+ * 只处理 CSS Module 处理完毕后的代码,如 .root :root
8
+ */
5
9
  Once(root, { result }) {
6
10
  var _a;
7
11
  const filePath = ((_a = result.root.source) == null ? void 0 : _a.input.from) || "";
@@ -11,6 +15,8 @@ function cssModuleGlobalRootPlugin(options = {}) {
11
15
  }
12
16
  const specialRules = [
13
17
  /^\s*:global\s+:root/
18
+ // 以 :global :root 开头
19
+ // 未来可以在这里添加更多特殊规则
14
20
  ];
15
21
  function isSpecialRule(selector) {
16
22
  const trimmed = selector.trim();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moluoxixi/css-module-global-root-plugin",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "cssModuleGlobalRootPlugin 组件",
5
5
  "sideEffects": [
6
6
  "*.css",