@unizap/unicss 2.0.2 → 2.0.3

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.
Files changed (2) hide show
  1. package/package.json +3 -2
  2. package/postcss-plugin.js +141 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unizap/unicss",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "Unicss: Build sleek interfaces straight from your markup. Fast, modern, utility-first CSS framework for rapid UI development.",
5
5
  "main": "dist/index.js",
6
6
  "unpkg": "dist/unicss.min.js",
@@ -28,7 +28,8 @@
28
28
  "dist/unicss.min.js",
29
29
  "bin",
30
30
  "vite-plugin-unicss.js",
31
- "postcss.config.js"
31
+ "postcss.config.js",
32
+ "postcss-plugin.js"
32
33
  ],
33
34
  "scripts": {
34
35
  "build": "node build.js",
@@ -0,0 +1,141 @@
1
+ const generateCSS = require('./dist/index.js');
2
+ const glob = require('glob');
3
+ const path = require('path');
4
+
5
+ /**
6
+ * PostCSS plugin for UniCSS
7
+ * Processes @unicss directives and generates utility classes
8
+ *
9
+ * Usage in postcss.config.js:
10
+ * module.exports = {
11
+ * plugins: {
12
+ * '@unizap/unicss': { skipBase: false },
13
+ * autoprefixer: {}
14
+ * }
15
+ * }
16
+ *
17
+ * Usage in CSS:
18
+ * @unicss base;
19
+ * @unicss utilities;
20
+ *
21
+ * Or simply:
22
+ * @unicss;
23
+ */
24
+ module.exports = (opts = {}) => {
25
+ const skipBase = opts.skipBase || false;
26
+
27
+ return {
28
+ postcssPlugin: 'unicss',
29
+
30
+ Once(root, { result }) {
31
+ // Add all source files to dependencies so PostCSS/Webpack/Vite watches them
32
+ const sourceFiles = getAllSourceFiles();
33
+ sourceFiles.forEach(file => {
34
+ result.messages.push({
35
+ type: 'dependency',
36
+ plugin: 'unicss',
37
+ file: file,
38
+ parent: result.opts.from
39
+ });
40
+ });
41
+ let hasUnicssDirective = false;
42
+ let baseDirective = null;
43
+ let utilitiesDirective = null;
44
+ let fullDirective = null;
45
+
46
+ // Find @unicss directives
47
+ root.walkAtRules('unicss', (rule) => {
48
+ hasUnicssDirective = true;
49
+ const param = rule.params.trim();
50
+
51
+ if (param === 'base') {
52
+ baseDirective = rule;
53
+ } else if (param === 'utilities') {
54
+ utilitiesDirective = rule;
55
+ } else if (param === '' || param === 'all') {
56
+ fullDirective = rule;
57
+ } else {
58
+ rule.warn(result, `Unknown @unicss directive: "${param}". Use "base", "utilities", or leave empty.`);
59
+ }
60
+ });
61
+
62
+ // If no directives found, do nothing
63
+ if (!hasUnicssDirective) {
64
+ return;
65
+ }
66
+
67
+ try {
68
+ // Generate CSS
69
+ const generatedCSS = generateCSS({ skipBase });
70
+
71
+ // Parse the generated CSS into AST
72
+ const postcss = require('postcss');
73
+ const generatedRoot = postcss.parse(generatedCSS);
74
+
75
+ // If there's a full directive (@unicss or @unicss all), replace it with everything
76
+ if (fullDirective) {
77
+ fullDirective.replaceWith(generatedRoot.nodes);
78
+ } else {
79
+ // Handle separate base and utilities directives
80
+ if (baseDirective) {
81
+ // Extract base styles (everything before utility classes)
82
+ const baseCSS = extractBaseStyles(generatedCSS);
83
+ const baseRoot = postcss.parse(baseCSS);
84
+ baseDirective.replaceWith(baseRoot.nodes);
85
+ }
86
+
87
+ if (utilitiesDirective) {
88
+ // Extract utility styles
89
+ const utilitiesCSS = extractUtilities(generatedCSS);
90
+ const utilitiesRoot = postcss.parse(utilitiesCSS);
91
+ utilitiesDirective.replaceWith(utilitiesRoot.nodes);
92
+ }
93
+ }
94
+ } catch (error) {
95
+ throw root.error(`UniCSS generation failed: ${error.message}`);
96
+ }
97
+ }
98
+ };
99
+ };
100
+
101
+ module.exports.postcss = true;
102
+
103
+ /**
104
+ * Get all source files that should trigger CSS regeneration
105
+ */
106
+ function getAllSourceFiles() {
107
+ const cwd = process.cwd();
108
+ const extensions = ['html', 'js', 'jsx', 'ts', 'tsx', 'vue', 'svelte'];
109
+ const files = [];
110
+
111
+ extensions.forEach(ext => {
112
+ const matches = glob.sync(`**/*.${ext}`, {
113
+ cwd: cwd,
114
+ absolute: true,
115
+ ignore: ['**/node_modules/**', '**/dist/**', '**/build/**', '**/.next/**']
116
+ });
117
+ files.push(...matches);
118
+ });
119
+
120
+ return files;
121
+ }
122
+
123
+ /**
124
+ * Extract base styles from generated CSS
125
+ * For now, we just return the full CSS when base is requested
126
+ * TODO: Implement intelligent splitting between base and utilities
127
+ */
128
+ function extractBaseStyles(css) {
129
+ // Return full CSS for now - users can use @unicss without params for everything
130
+ return css;
131
+ }
132
+
133
+ /**
134
+ * Extract utility classes from generated CSS
135
+ * For now, we just return the full CSS when utilities is requested
136
+ * TODO: Implement intelligent splitting between base and utilities
137
+ */
138
+ function extractUtilities(css) {
139
+ // Return full CSS for now - users can use @unicss without params for everything
140
+ return css;
141
+ }