@gustcss/vite 0.3.2 → 0.5.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/dist/index.cjs ADDED
@@ -0,0 +1,185 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ //#region rolldown:runtime
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
+ key = keys[i];
13
+ if (!__hasOwnProp.call(to, key) && key !== except) {
14
+ __defProp(to, key, {
15
+ get: ((k) => from[k]).bind(null, key),
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ }
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
24
+ value: mod,
25
+ enumerable: true
26
+ }) : target, mod));
27
+
28
+ //#endregion
29
+ let child_process = require("child_process");
30
+ let path = require("path");
31
+ path = __toESM(path);
32
+ let fs = require("fs");
33
+ fs = __toESM(fs);
34
+
35
+ //#region src/index.js
36
+ /**
37
+ * @gustcss/vite - Vite plugin for CSS Utility Generator
38
+ *
39
+ * Usage in vite.config.ts:
40
+ * import gustcss from '@gustcss/vite'
41
+ * export default defineConfig({
42
+ * plugins: [gustcss({ output: 'src/styles/utility.css' })],
43
+ * })
44
+ */
45
+ function cssUtility(opts = {}) {
46
+ const output = opts.output || "src/styles/utility.css";
47
+ const content = opts.content || ["./src/**/*.{js,ts,jsx,tsx,astro,vue}"];
48
+ const configPath = opts.config;
49
+ const outputToCssLayers = opts.outputToCssLayers;
50
+ let watchProcess = null;
51
+ function findBinary(cwd) {
52
+ const possiblePaths = [
53
+ path.default.join(cwd, "node_modules", ".bin", "gustcss"),
54
+ path.default.join(cwd, "..", "..", "bin", "gustcss"),
55
+ "gustcss"
56
+ ];
57
+ for (const p of possiblePaths) if (p === "gustcss" || fs.default.existsSync(p)) return p;
58
+ throw new Error("gustcss binary not found");
59
+ }
60
+ function findConfigFile(cwd) {
61
+ if (configPath) return configPath;
62
+ for (const configName of ["gustcss.config.json", "gustcss.config.js"]) {
63
+ const fullPath = path.default.join(cwd, configName);
64
+ if (fs.default.existsSync(fullPath)) return fullPath;
65
+ }
66
+ return null;
67
+ }
68
+ function buildCSS(cwd, binaryPath) {
69
+ const resolvedConfigPath = findConfigFile(cwd);
70
+ const args = [
71
+ "build",
72
+ "-o",
73
+ output
74
+ ];
75
+ if (outputToCssLayers) args.push("--css-layers");
76
+ let tempConfigPath = null;
77
+ if (resolvedConfigPath) args.push("--config", resolvedConfigPath);
78
+ else {
79
+ tempConfigPath = path.default.join(cwd, ".gustcss.vite.tmp.json");
80
+ const tempConfig = { content };
81
+ fs.default.writeFileSync(tempConfigPath, JSON.stringify(tempConfig));
82
+ args.push("--config", tempConfigPath);
83
+ }
84
+ try {
85
+ const result = (0, child_process.spawnSync)(binaryPath, args, {
86
+ cwd,
87
+ encoding: "utf-8",
88
+ stdio: [
89
+ "pipe",
90
+ "pipe",
91
+ "pipe"
92
+ ]
93
+ });
94
+ if (result.status !== 0) throw new Error(`gustcss build failed: ${result.stderr}`);
95
+ if (result.stderr) process.stderr.write(result.stderr);
96
+ } catch (error) {
97
+ throw new Error(`gustcss build failed: ${error.message}`);
98
+ } finally {
99
+ if (tempConfigPath && fs.default.existsSync(tempConfigPath)) fs.default.unlinkSync(tempConfigPath);
100
+ }
101
+ }
102
+ return {
103
+ name: "gustcss",
104
+ configResolved(config) {
105
+ const cwd = config.root || process.cwd();
106
+ const binaryPath = findBinary(cwd);
107
+ try {
108
+ buildCSS(cwd, binaryPath);
109
+ } catch (error) {
110
+ console.error(`[gustcss] Failed to build CSS: ${error.message}`);
111
+ throw error;
112
+ }
113
+ },
114
+ configureServer(server) {
115
+ const cwd = server.config.root || process.cwd();
116
+ const binaryPath = findBinary(cwd);
117
+ const resolvedConfigPath = findConfigFile(cwd);
118
+ const outputPath = path.default.resolve(cwd, output);
119
+ try {
120
+ buildCSS(cwd, binaryPath);
121
+ } catch (error) {
122
+ console.error(`[gustcss] Failed to build CSS: ${error.message}`);
123
+ }
124
+ const watchArgs = [
125
+ "build",
126
+ "--watch",
127
+ "-o",
128
+ output
129
+ ];
130
+ if (outputToCssLayers) watchArgs.push("--css-layers");
131
+ let tempConfigPath = null;
132
+ if (resolvedConfigPath) watchArgs.push("--config", resolvedConfigPath);
133
+ else {
134
+ tempConfigPath = path.default.join(cwd, ".gustcss.vite.tmp.json");
135
+ const tempConfig = { content };
136
+ fs.default.writeFileSync(tempConfigPath, JSON.stringify(tempConfig));
137
+ watchArgs.push("--config", tempConfigPath);
138
+ }
139
+ watchProcess = (0, child_process.spawn)(binaryPath, watchArgs, {
140
+ cwd,
141
+ stdio: [
142
+ "pipe",
143
+ "pipe",
144
+ "pipe"
145
+ ]
146
+ });
147
+ watchProcess.stderr.on("data", (data) => {
148
+ process.stderr.write(data);
149
+ });
150
+ server.watcher.add(outputPath);
151
+ server.watcher.on("change", (changedPath) => {
152
+ if (path.default.resolve(changedPath) === outputPath) server.ws.send({
153
+ type: "update",
154
+ updates: [{
155
+ type: "css-update",
156
+ path: "/" + path.default.relative(cwd, outputPath),
157
+ acceptedPath: "/" + path.default.relative(cwd, outputPath),
158
+ timestamp: Date.now()
159
+ }]
160
+ });
161
+ });
162
+ const cleanup = () => {
163
+ if (watchProcess) {
164
+ watchProcess.kill();
165
+ watchProcess = null;
166
+ }
167
+ if (tempConfigPath && fs.default.existsSync(tempConfigPath)) fs.default.unlinkSync(tempConfigPath);
168
+ };
169
+ server.httpServer?.on("close", cleanup);
170
+ process.once("SIGINT", cleanup);
171
+ process.once("SIGTERM", cleanup);
172
+ },
173
+ buildEnd() {
174
+ if (watchProcess) {
175
+ watchProcess.kill();
176
+ watchProcess = null;
177
+ }
178
+ }
179
+ };
180
+ }
181
+ var src_default = cssUtility;
182
+
183
+ //#endregion
184
+ exports.cssUtility = cssUtility;
185
+ exports.default = src_default;
package/dist/index.mjs ADDED
@@ -0,0 +1,154 @@
1
+ import { spawn, spawnSync } from "child_process";
2
+ import path from "path";
3
+ import fs from "fs";
4
+
5
+ //#region src/index.js
6
+ /**
7
+ * @gustcss/vite - Vite plugin for CSS Utility Generator
8
+ *
9
+ * Usage in vite.config.ts:
10
+ * import gustcss from '@gustcss/vite'
11
+ * export default defineConfig({
12
+ * plugins: [gustcss({ output: 'src/styles/utility.css' })],
13
+ * })
14
+ */
15
+ function cssUtility(opts = {}) {
16
+ const output = opts.output || "src/styles/utility.css";
17
+ const content = opts.content || ["./src/**/*.{js,ts,jsx,tsx,astro,vue}"];
18
+ const configPath = opts.config;
19
+ const outputToCssLayers = opts.outputToCssLayers;
20
+ let watchProcess = null;
21
+ function findBinary(cwd) {
22
+ const possiblePaths = [
23
+ path.join(cwd, "node_modules", ".bin", "gustcss"),
24
+ path.join(cwd, "..", "..", "bin", "gustcss"),
25
+ "gustcss"
26
+ ];
27
+ for (const p of possiblePaths) if (p === "gustcss" || fs.existsSync(p)) return p;
28
+ throw new Error("gustcss binary not found");
29
+ }
30
+ function findConfigFile(cwd) {
31
+ if (configPath) return configPath;
32
+ for (const configName of ["gustcss.config.json", "gustcss.config.js"]) {
33
+ const fullPath = path.join(cwd, configName);
34
+ if (fs.existsSync(fullPath)) return fullPath;
35
+ }
36
+ return null;
37
+ }
38
+ function buildCSS(cwd, binaryPath) {
39
+ const resolvedConfigPath = findConfigFile(cwd);
40
+ const args = [
41
+ "build",
42
+ "-o",
43
+ output
44
+ ];
45
+ if (outputToCssLayers) args.push("--css-layers");
46
+ let tempConfigPath = null;
47
+ if (resolvedConfigPath) args.push("--config", resolvedConfigPath);
48
+ else {
49
+ tempConfigPath = path.join(cwd, ".gustcss.vite.tmp.json");
50
+ const tempConfig = { content };
51
+ fs.writeFileSync(tempConfigPath, JSON.stringify(tempConfig));
52
+ args.push("--config", tempConfigPath);
53
+ }
54
+ try {
55
+ const result = spawnSync(binaryPath, args, {
56
+ cwd,
57
+ encoding: "utf-8",
58
+ stdio: [
59
+ "pipe",
60
+ "pipe",
61
+ "pipe"
62
+ ]
63
+ });
64
+ if (result.status !== 0) throw new Error(`gustcss build failed: ${result.stderr}`);
65
+ if (result.stderr) process.stderr.write(result.stderr);
66
+ } catch (error) {
67
+ throw new Error(`gustcss build failed: ${error.message}`);
68
+ } finally {
69
+ if (tempConfigPath && fs.existsSync(tempConfigPath)) fs.unlinkSync(tempConfigPath);
70
+ }
71
+ }
72
+ return {
73
+ name: "gustcss",
74
+ configResolved(config) {
75
+ const cwd = config.root || process.cwd();
76
+ const binaryPath = findBinary(cwd);
77
+ try {
78
+ buildCSS(cwd, binaryPath);
79
+ } catch (error) {
80
+ console.error(`[gustcss] Failed to build CSS: ${error.message}`);
81
+ throw error;
82
+ }
83
+ },
84
+ configureServer(server) {
85
+ const cwd = server.config.root || process.cwd();
86
+ const binaryPath = findBinary(cwd);
87
+ const resolvedConfigPath = findConfigFile(cwd);
88
+ const outputPath = path.resolve(cwd, output);
89
+ try {
90
+ buildCSS(cwd, binaryPath);
91
+ } catch (error) {
92
+ console.error(`[gustcss] Failed to build CSS: ${error.message}`);
93
+ }
94
+ const watchArgs = [
95
+ "build",
96
+ "--watch",
97
+ "-o",
98
+ output
99
+ ];
100
+ if (outputToCssLayers) watchArgs.push("--css-layers");
101
+ let tempConfigPath = null;
102
+ if (resolvedConfigPath) watchArgs.push("--config", resolvedConfigPath);
103
+ else {
104
+ tempConfigPath = path.join(cwd, ".gustcss.vite.tmp.json");
105
+ const tempConfig = { content };
106
+ fs.writeFileSync(tempConfigPath, JSON.stringify(tempConfig));
107
+ watchArgs.push("--config", tempConfigPath);
108
+ }
109
+ watchProcess = spawn(binaryPath, watchArgs, {
110
+ cwd,
111
+ stdio: [
112
+ "pipe",
113
+ "pipe",
114
+ "pipe"
115
+ ]
116
+ });
117
+ watchProcess.stderr.on("data", (data) => {
118
+ process.stderr.write(data);
119
+ });
120
+ server.watcher.add(outputPath);
121
+ server.watcher.on("change", (changedPath) => {
122
+ if (path.resolve(changedPath) === outputPath) server.ws.send({
123
+ type: "update",
124
+ updates: [{
125
+ type: "css-update",
126
+ path: "/" + path.relative(cwd, outputPath),
127
+ acceptedPath: "/" + path.relative(cwd, outputPath),
128
+ timestamp: Date.now()
129
+ }]
130
+ });
131
+ });
132
+ const cleanup = () => {
133
+ if (watchProcess) {
134
+ watchProcess.kill();
135
+ watchProcess = null;
136
+ }
137
+ if (tempConfigPath && fs.existsSync(tempConfigPath)) fs.unlinkSync(tempConfigPath);
138
+ };
139
+ server.httpServer?.on("close", cleanup);
140
+ process.once("SIGINT", cleanup);
141
+ process.once("SIGTERM", cleanup);
142
+ },
143
+ buildEnd() {
144
+ if (watchProcess) {
145
+ watchProcess.kill();
146
+ watchProcess = null;
147
+ }
148
+ }
149
+ };
150
+ }
151
+ var src_default = cssUtility;
152
+
153
+ //#endregion
154
+ export { cssUtility, src_default as default };
package/package.json CHANGED
@@ -1,28 +1,42 @@
1
1
  {
2
2
  "name": "@gustcss/vite",
3
- "version": "0.3.2",
3
+ "version": "0.5.0",
4
4
  "description": "Vite plugin for GustCSS",
5
- "main": "index.mjs",
6
- "module": "index.mjs",
5
+ "main": "dist/index.mjs",
6
+ "module": "dist/index.mjs",
7
7
  "type": "module",
8
8
  "files": [
9
- "index.cjs",
10
- "index.mjs",
9
+ "dist/index.cjs",
10
+ "dist/index.mjs",
11
11
  "README.md",
12
12
  "LICENSE"
13
13
  ],
14
14
  "exports": {
15
15
  ".": {
16
- "import": "./index.mjs",
17
- "require": "./index.cjs"
16
+ "import": "./dist/index.mjs",
17
+ "require": "./dist/index.cjs"
18
18
  }
19
19
  },
20
- "keywords": ["vite", "vite-plugin", "gustcss", "gust", "css", "utility"],
20
+ "scripts": {
21
+ "build": "node build.js",
22
+ "prepublishOnly": "npm run build"
23
+ },
24
+ "keywords": [
25
+ "vite",
26
+ "vite-plugin",
27
+ "gustcss",
28
+ "gust",
29
+ "css",
30
+ "utility"
31
+ ],
21
32
  "publishConfig": {
22
33
  "access": "public"
23
34
  },
24
35
  "dependencies": {
25
- "gustcss": "^0.3.2"
36
+ "gustcss": "^0.5.0"
37
+ },
38
+ "devDependencies": {
39
+ "rolldown": "^1.0.0-beta.5"
26
40
  },
27
41
  "peerDependencies": {
28
42
  "vite": "^4.0.0 || ^5.0.0 || ^6.0.0"
package/index.cjs DELETED
@@ -1,186 +0,0 @@
1
- const { execSync, spawn } = require('child_process')
2
- const path = require('path')
3
- const fs = require('fs')
4
-
5
- /**
6
- * @gustcss/vite - Vite plugin for CSS Utility Generator
7
- *
8
- * Usage in vite.config.ts:
9
- * import gustcss from '@gustcss/vite'
10
- * export default defineConfig({
11
- * plugins: [gustcss({ output: 'src/styles/utility.css' })],
12
- * })
13
- */
14
- function cssUtility(opts = {}) {
15
- const output = opts.output || 'src/styles/utility.css'
16
- const content = opts.content || ['./src/**/*.{js,ts,jsx,tsx,astro,vue}']
17
- const configPath = opts.config
18
- let watchProcess = null
19
-
20
- function findBinary(cwd) {
21
- const possiblePaths = [
22
- path.join(cwd, 'node_modules', '.bin', 'gustcss'),
23
- path.join(cwd, '..', '..', 'bin', 'gustcss'), // For sample projects
24
- 'gustcss', // Global installation
25
- ]
26
-
27
- for (const p of possiblePaths) {
28
- if (p === 'gustcss' || fs.existsSync(p)) {
29
- return p
30
- }
31
- }
32
-
33
- throw new Error('gustcss binary not found')
34
- }
35
-
36
- function findConfigFile(cwd) {
37
- if (configPath) {
38
- return configPath
39
- }
40
- // Auto-detect config file in current directory
41
- const defaultConfigPaths = [
42
- 'gustcss.config.json',
43
- 'gustcss.config.js',
44
- ]
45
- for (const configName of defaultConfigPaths) {
46
- const fullPath = path.join(cwd, configName)
47
- if (fs.existsSync(fullPath)) {
48
- return fullPath
49
- }
50
- }
51
- return null
52
- }
53
-
54
- function buildArgs(cwd, extraArgs = []) {
55
- const args = ['build', ...extraArgs]
56
- const resolvedConfigPath = findConfigFile(cwd)
57
-
58
- if (resolvedConfigPath) {
59
- // Use existing config file
60
- args.push('--config', resolvedConfigPath)
61
- } else {
62
- // Create temporary config file to avoid glob pattern issues with CLI
63
- const tempConfigPath = path.join(cwd, '.gustcss.vite.tmp.json')
64
- const tempConfig = { content, output }
65
- fs.writeFileSync(tempConfigPath, JSON.stringify(tempConfig))
66
- args.push('--config', tempConfigPath)
67
- // Note: temp file cleanup happens after build in buildCSS
68
- }
69
-
70
- return args
71
- }
72
-
73
- function buildCSS(cwd, binaryPath) {
74
- const resolvedConfigPath = findConfigFile(cwd)
75
- const args = ['build', '-o', output]
76
-
77
- let tempConfigPath = null
78
- if (resolvedConfigPath) {
79
- args.push('--config', resolvedConfigPath)
80
- } else {
81
- tempConfigPath = path.join(cwd, '.gustcss.vite.tmp.json')
82
- const tempConfig = { content }
83
- fs.writeFileSync(tempConfigPath, JSON.stringify(tempConfig))
84
- args.push('--config', tempConfigPath)
85
- }
86
-
87
- try {
88
- execSync(`"${binaryPath}" ${args.join(' ')}`, {
89
- cwd,
90
- encoding: 'utf-8',
91
- stdio: ['pipe', 'pipe', 'pipe'],
92
- })
93
- } catch (error) {
94
- const stderr = error.stderr ? error.stderr.toString() : error.message
95
- console.error(`gustcss build failed: ${stderr}`)
96
- } finally {
97
- // Clean up temporary config file
98
- if (tempConfigPath && fs.existsSync(tempConfigPath)) {
99
- fs.unlinkSync(tempConfigPath)
100
- }
101
- }
102
- }
103
-
104
- return {
105
- name: 'gustcss',
106
-
107
- configResolved(config) {
108
- const cwd = config.root || process.cwd()
109
- const binaryPath = findBinary(cwd)
110
-
111
- // Always run initial build
112
- buildCSS(cwd, binaryPath)
113
- },
114
-
115
- configureServer(server) {
116
- const cwd = server.config.root || process.cwd()
117
- const binaryPath = findBinary(cwd)
118
- const resolvedConfigPath = findConfigFile(cwd)
119
- const outputPath = path.resolve(cwd, output)
120
-
121
- // First, run initial build synchronously before starting watch mode
122
- buildCSS(cwd, binaryPath)
123
-
124
- // Start watch mode (without initial build since we just did it)
125
- const args = ['build', '--watch', '-o', output]
126
-
127
- let tempConfigPath = null
128
- if (resolvedConfigPath) {
129
- args.push('--config', resolvedConfigPath)
130
- } else {
131
- tempConfigPath = path.join(cwd, '.gustcss.vite.tmp.json')
132
- const tempConfig = { content }
133
- fs.writeFileSync(tempConfigPath, JSON.stringify(tempConfig))
134
- args.push('--config', tempConfigPath)
135
- }
136
-
137
- watchProcess = spawn(binaryPath, args, {
138
- cwd,
139
- stdio: ['pipe', 'pipe', 'pipe'],
140
- })
141
-
142
- watchProcess.stderr.on('data', (data) => {
143
- console.error(`gustcss: ${data}`)
144
- })
145
-
146
- // Watch generated CSS file for HMR
147
- server.watcher.add(outputPath)
148
- server.watcher.on('change', (changedPath) => {
149
- if (path.resolve(changedPath) === outputPath) {
150
- server.ws.send({
151
- type: 'update',
152
- updates: [
153
- {
154
- type: 'css-update',
155
- path: '/' + path.relative(cwd, outputPath),
156
- acceptedPath: '/' + path.relative(cwd, outputPath),
157
- timestamp: Date.now(),
158
- },
159
- ],
160
- })
161
- }
162
- })
163
-
164
- server.httpServer?.on('close', () => {
165
- if (watchProcess) {
166
- watchProcess.kill()
167
- watchProcess = null
168
- }
169
- // Clean up temporary config file
170
- if (tempConfigPath && fs.existsSync(tempConfigPath)) {
171
- fs.unlinkSync(tempConfigPath)
172
- }
173
- })
174
- },
175
-
176
- buildEnd() {
177
- if (watchProcess) {
178
- watchProcess.kill()
179
- watchProcess = null
180
- }
181
- },
182
- }
183
- }
184
-
185
- module.exports = { cssUtility }
186
- module.exports.default = cssUtility
package/index.mjs DELETED
@@ -1,186 +0,0 @@
1
- const { execSync, spawn } = require('child_process')
2
- const path = require('path')
3
- const fs = require('fs')
4
-
5
- /**
6
- * @gustcss/vite - Vite plugin for CSS Utility Generator
7
- *
8
- * Usage in vite.config.ts:
9
- * import gustcss from '@gustcss/vite'
10
- * export default defineConfig({
11
- * plugins: [gustcss({ output: 'src/styles/utility.css' })],
12
- * })
13
- */
14
- function cssUtility(opts = {}) {
15
- const output = opts.output || 'src/styles/utility.css'
16
- const content = opts.content || ['./src/**/*.{js,ts,jsx,tsx,astro,vue}']
17
- const configPath = opts.config
18
- let watchProcess = null
19
-
20
- function findBinary(cwd) {
21
- const possiblePaths = [
22
- path.join(cwd, 'node_modules', '.bin', 'gustcss'),
23
- path.join(cwd, '..', '..', 'bin', 'gustcss'), // For sample projects
24
- 'gustcss', // Global installation
25
- ]
26
-
27
- for (const p of possiblePaths) {
28
- if (p === 'gustcss' || fs.existsSync(p)) {
29
- return p
30
- }
31
- }
32
-
33
- throw new Error('gustcss binary not found')
34
- }
35
-
36
- function findConfigFile(cwd) {
37
- if (configPath) {
38
- return configPath
39
- }
40
- // Auto-detect config file in current directory
41
- const defaultConfigPaths = [
42
- 'gustcss.config.json',
43
- 'gustcss.config.js',
44
- ]
45
- for (const configName of defaultConfigPaths) {
46
- const fullPath = path.join(cwd, configName)
47
- if (fs.existsSync(fullPath)) {
48
- return fullPath
49
- }
50
- }
51
- return null
52
- }
53
-
54
- function buildArgs(cwd, extraArgs = []) {
55
- const args = ['build', ...extraArgs]
56
- const resolvedConfigPath = findConfigFile(cwd)
57
-
58
- if (resolvedConfigPath) {
59
- // Use existing config file
60
- args.push('--config', resolvedConfigPath)
61
- } else {
62
- // Create temporary config file to avoid glob pattern issues with CLI
63
- const tempConfigPath = path.join(cwd, '.gustcss.vite.tmp.json')
64
- const tempConfig = { content, output }
65
- fs.writeFileSync(tempConfigPath, JSON.stringify(tempConfig))
66
- args.push('--config', tempConfigPath)
67
- // Note: temp file cleanup happens after build in buildCSS
68
- }
69
-
70
- return args
71
- }
72
-
73
- function buildCSS(cwd, binaryPath) {
74
- const resolvedConfigPath = findConfigFile(cwd)
75
- const args = ['build', '-o', output]
76
-
77
- let tempConfigPath = null
78
- if (resolvedConfigPath) {
79
- args.push('--config', resolvedConfigPath)
80
- } else {
81
- tempConfigPath = path.join(cwd, '.gustcss.vite.tmp.json')
82
- const tempConfig = { content }
83
- fs.writeFileSync(tempConfigPath, JSON.stringify(tempConfig))
84
- args.push('--config', tempConfigPath)
85
- }
86
-
87
- try {
88
- execSync(`"${binaryPath}" ${args.join(' ')}`, {
89
- cwd,
90
- encoding: 'utf-8',
91
- stdio: ['pipe', 'pipe', 'pipe'],
92
- })
93
- } catch (error) {
94
- const stderr = error.stderr ? error.stderr.toString() : error.message
95
- console.error(`gustcss build failed: ${stderr}`)
96
- } finally {
97
- // Clean up temporary config file
98
- if (tempConfigPath && fs.existsSync(tempConfigPath)) {
99
- fs.unlinkSync(tempConfigPath)
100
- }
101
- }
102
- }
103
-
104
- return {
105
- name: 'gustcss',
106
-
107
- configResolved(config) {
108
- const cwd = config.root || process.cwd()
109
- const binaryPath = findBinary(cwd)
110
-
111
- // Always run initial build
112
- buildCSS(cwd, binaryPath)
113
- },
114
-
115
- configureServer(server) {
116
- const cwd = server.config.root || process.cwd()
117
- const binaryPath = findBinary(cwd)
118
- const resolvedConfigPath = findConfigFile(cwd)
119
- const outputPath = path.resolve(cwd, output)
120
-
121
- // First, run initial build synchronously before starting watch mode
122
- buildCSS(cwd, binaryPath)
123
-
124
- // Start watch mode (without initial build since we just did it)
125
- const args = ['build', '--watch', '-o', output]
126
-
127
- let tempConfigPath = null
128
- if (resolvedConfigPath) {
129
- args.push('--config', resolvedConfigPath)
130
- } else {
131
- tempConfigPath = path.join(cwd, '.gustcss.vite.tmp.json')
132
- const tempConfig = { content }
133
- fs.writeFileSync(tempConfigPath, JSON.stringify(tempConfig))
134
- args.push('--config', tempConfigPath)
135
- }
136
-
137
- watchProcess = spawn(binaryPath, args, {
138
- cwd,
139
- stdio: ['pipe', 'pipe', 'pipe'],
140
- })
141
-
142
- watchProcess.stderr.on('data', (data) => {
143
- console.error(`gustcss: ${data}`)
144
- })
145
-
146
- // Watch generated CSS file for HMR
147
- server.watcher.add(outputPath)
148
- server.watcher.on('change', (changedPath) => {
149
- if (path.resolve(changedPath) === outputPath) {
150
- server.ws.send({
151
- type: 'update',
152
- updates: [
153
- {
154
- type: 'css-update',
155
- path: '/' + path.relative(cwd, outputPath),
156
- acceptedPath: '/' + path.relative(cwd, outputPath),
157
- timestamp: Date.now(),
158
- },
159
- ],
160
- })
161
- }
162
- })
163
-
164
- server.httpServer?.on('close', () => {
165
- if (watchProcess) {
166
- watchProcess.kill()
167
- watchProcess = null
168
- }
169
- // Clean up temporary config file
170
- if (tempConfigPath && fs.existsSync(tempConfigPath)) {
171
- fs.unlinkSync(tempConfigPath)
172
- }
173
- })
174
- },
175
-
176
- buildEnd() {
177
- if (watchProcess) {
178
- watchProcess.kill()
179
- watchProcess = null
180
- }
181
- },
182
- }
183
- }
184
-
185
- module.exports = { cssUtility }
186
- module.exports.default = cssUtility