@gustcss/postcss 0.1.0
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/index.js +114 -0
- package/package.json +20 -0
package/index.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
const { execSync } = require('child_process')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const fs = require('fs')
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @gustcss/postcss - PostCSS plugin for CSS Utility Generator
|
|
7
|
+
*
|
|
8
|
+
* Usage in postcss.config.js:
|
|
9
|
+
* module.exports = {
|
|
10
|
+
* plugins: [
|
|
11
|
+
* require('@gustcss/postcss')({
|
|
12
|
+
* content: ['./src/**\/*.{js,ts,jsx,tsx}'],
|
|
13
|
+
* }),
|
|
14
|
+
* ],
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* In your CSS file:
|
|
18
|
+
* @gustcss;
|
|
19
|
+
*/
|
|
20
|
+
module.exports = (opts = {}) => {
|
|
21
|
+
const content = opts.content || ['./src/**/*.{js,ts,jsx,tsx}']
|
|
22
|
+
const configPath = opts.config
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
postcssPlugin: 'gustcss',
|
|
26
|
+
Once(root, { result }) {
|
|
27
|
+
let hasDirective = false
|
|
28
|
+
root.walkAtRules('gustcss', (atRule) => {
|
|
29
|
+
hasDirective = true
|
|
30
|
+
atRule.remove()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
if (!hasDirective) {
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Find the CLI binary
|
|
38
|
+
const cwd = process.cwd()
|
|
39
|
+
const possiblePaths = [
|
|
40
|
+
path.join(cwd, 'node_modules', '.bin', 'gustcss'),
|
|
41
|
+
path.join(cwd, '..', '..', 'bin', 'gustcss'), // For sample projects
|
|
42
|
+
'gustcss', // Global installation
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
let binaryPath = null
|
|
46
|
+
for (const p of possiblePaths) {
|
|
47
|
+
if (p === 'gustcss' || fs.existsSync(p)) {
|
|
48
|
+
binaryPath = p
|
|
49
|
+
break
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!binaryPath) {
|
|
54
|
+
throw new Error('gustcss binary not found')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Build CLI arguments
|
|
58
|
+
const args = ['build', '--stdout']
|
|
59
|
+
|
|
60
|
+
// Determine config file path
|
|
61
|
+
let resolvedConfigPath = configPath
|
|
62
|
+
if (!resolvedConfigPath) {
|
|
63
|
+
// Auto-detect config file in current directory
|
|
64
|
+
const defaultConfigPaths = [
|
|
65
|
+
'gustcss.config.json',
|
|
66
|
+
'gustcss.config.js',
|
|
67
|
+
]
|
|
68
|
+
for (const configName of defaultConfigPaths) {
|
|
69
|
+
const fullPath = path.join(cwd, configName)
|
|
70
|
+
if (fs.existsSync(fullPath)) {
|
|
71
|
+
resolvedConfigPath = fullPath
|
|
72
|
+
break
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let tempConfigPath = null
|
|
78
|
+
|
|
79
|
+
if (resolvedConfigPath) {
|
|
80
|
+
// Use existing config file
|
|
81
|
+
args.push('--config', resolvedConfigPath)
|
|
82
|
+
} else {
|
|
83
|
+
// Create temporary config file to avoid glob pattern issues with CLI
|
|
84
|
+
tempConfigPath = path.join(cwd, '.gustcss.postcss.tmp.json')
|
|
85
|
+
const tempConfig = { content }
|
|
86
|
+
fs.writeFileSync(tempConfigPath, JSON.stringify(tempConfig))
|
|
87
|
+
args.push('--config', tempConfigPath)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
const css = execSync(`"${binaryPath}" ${args.join(' ')}`, {
|
|
92
|
+
cwd,
|
|
93
|
+
encoding: 'utf-8',
|
|
94
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
// Parse the generated CSS and append to the root
|
|
98
|
+
const postcss = require('postcss')
|
|
99
|
+
const parsed = postcss.parse(css)
|
|
100
|
+
root.append(parsed)
|
|
101
|
+
} catch (error) {
|
|
102
|
+
const stderr = error.stderr ? error.stderr.toString() : error.message
|
|
103
|
+
throw new Error(`gustcss build failed: ${stderr}`)
|
|
104
|
+
} finally {
|
|
105
|
+
// Clean up temporary config file
|
|
106
|
+
if (tempConfigPath && fs.existsSync(tempConfigPath)) {
|
|
107
|
+
fs.unlinkSync(tempConfigPath)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
module.exports.postcss = true
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gustcss/postcss",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "PostCSS plugin for Gust CSS Utility Generator",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "commonjs",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js"
|
|
9
|
+
],
|
|
10
|
+
"keywords": ["postcss", "postcss-plugin", "gustcss", "gust", "css", "utility"],
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"gustcss": "^0.1.0"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"postcss": "^8.0.0"
|
|
19
|
+
}
|
|
20
|
+
}
|