-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-shared-hooks.js
More file actions
253 lines (217 loc) · 7.74 KB
/
build-shared-hooks.js
File metadata and controls
253 lines (217 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env node
/**
* Shared Hooks Builder (with dead code elimination)
*
* - Reads shared hooks from shared-hooks/ and subfolders (alphabetical)
* - For each component hooks.source.js in packs/, concatenates shared + component
* - Uses esbuild DCE to remove unused code (anything not reachable from transformHook)
* - Output: plain static JS with const/let preserved
* - If a component has no hooks.source.js, it is skipped
* - Supports --watch to regenerate on changes
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { transform } from 'esbuild';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Constants
const SOURCE_FILENAME = 'hooks.source.js';
const OUTPUT_FILENAME = 'hooks.js';
/**
* Gets the shared hooks directory (always in the package)
*/
function getSharedDir() {
return path.join(__dirname, 'shared-hooks');
}
/**
* Recursively lists all shared hook files in shared-hooks/ and subfolders
* @returns {Promise<string[]>} Array of absolute paths to shared hook files (sorted alphabetically)
*/
async function listSharedFiles() {
const sharedDir = getSharedDir();
const results = [];
async function walk(dir) {
let entries;
try {
entries = await fs.promises.readdir(dir, { withFileTypes: true });
} catch (err) {
if (dir === sharedDir) {
console.error(`[hooks] Failed to read shared dir: ${err.message}`);
}
return;
}
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (entry.name.startsWith('.')) continue;
await walk(fullPath);
} else if (entry.isFile() && entry.name.endsWith('.js')) {
results.push(fullPath);
}
}
}
await walk(sharedDir);
return results.sort();
}
/**
* Recursively finds all hooks.source.js files in a directory
* @param {string} startDir - Directory to search
* @returns {Promise<string[]>} Array of absolute paths to source files
*/
async function findHookSources(startDir) {
const results = [];
async function walk(dir) {
let entries;
try {
entries = await fs.promises.readdir(dir, { withFileTypes: true });
} catch {
return; // Directory doesn't exist or can't be read
}
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (entry.name === 'node_modules' || entry.name.startsWith('.')) continue;
await walk(fullPath);
continue;
}
if (entry.isFile() && entry.name === SOURCE_FILENAME) {
results.push(fullPath);
}
}
}
await walk(startDir);
return results;
}
/**
* Builds a single hooks.js file from shared hooks + component source
* @param {string[]} sharedFiles - Array of shared hook file paths
* @param {string} sourcePath - Path to the hooks.source.js file
* @param {string} projectRoot - Project root for relative path display
* @param {boolean} [minify=true] - Whether to fully minify the output
*/
async function buildOne(sharedFiles, sourcePath, projectRoot, minify = true) {
const componentDir = path.dirname(sourcePath);
const outputPath = path.join(componentDir, OUTPUT_FILENAME);
// Read all shared hook files
const sharedPieces = [];
for (const file of sharedFiles) {
const content = await fs.promises.readFile(file, 'utf8');
sharedPieces.push(content.trim());
}
// Read the component source
let componentContent = await fs.promises.readFile(sourcePath, 'utf8');
const hasExportLine = /exports\.transformHook\s*=/.test(componentContent);
// Ensure the expected CommonJS export is present exactly once
if (!hasExportLine) {
componentContent = `${componentContent.trim()}\nexports.transformHook = transformHook;`;
}
// Concatenate everything
const combinedSource = `
${sharedPieces.join('\n\n')}
${componentContent}
`;
// Minify whitespace only (keep identifier names); enable syntax minification for small size
const result = await transform(combinedSource, {
loader: 'js',
target: 'es2018',
minifySyntax: minify,
minifyWhitespace: minify,
minifyIdentifiers: false,
format: 'cjs',
legalComments: 'none',
});
const banner = minify ? '' : `// AUTO-GENERATED: do not edit. Edit hooks.source.js instead.\n`;
await fs.promises.writeFile(outputPath, banner + result.code, 'utf8');
console.log(`[hooks] Wrote ${path.relative(projectRoot, outputPath)}`);
}
/**
* Builds all hooks.js files
* @param {Object} config - Configuration object
* @param {string} config.packsDir - Absolute path to the packs directory
* @param {string} [config.projectRoot] - Project root for display purposes
* @param {boolean} [config.minify=true] - Whether to fully minify the output
*/
export async function buildAll(config) {
const packsDir = config.packsDir;
const projectRoot = config.projectRoot || path.dirname(packsDir);
const minify = config.minify !== false; // Default to true
const sharedFiles = await listSharedFiles();
const sources = await findHookSources(packsDir);
console.log(`[hooks] Building ${sources.length} component hook(s); shared files: ${sharedFiles.length}`);
for (const sourcePath of sources) {
await buildOne(sharedFiles, sourcePath, projectRoot, minify);
}
console.log(`[hooks] Build complete`);
}
/**
* Starts watch mode for continuous building
* @param {Object} config - Configuration object
* @param {string} config.packsDir - Absolute path to the packs directory
* @param {string} [config.projectRoot] - Project root for display purposes
* @param {boolean} [config.minify=true] - Whether to fully minify the output
*/
export async function startWatch(config) {
const packsDir = config.packsDir;
const sharedDir = getSharedDir();
console.log('[hooks] Watch mode enabled. Listening for changes...');
let building = false;
const rebuild = async () => {
if (building) return;
building = true;
try {
await buildAll(config);
} catch (err) {
console.error('[hooks] Build error:', err.message || err);
} finally {
building = false;
}
};
// Watch both the shared hooks directory and the packs directory
const watchDirs = [sharedDir, packsDir];
for (const watchDir of watchDirs) {
try {
const watcher = fs.watch(watchDir, { recursive: true }, (eventType, filename) => {
if (!filename) return;
const lower = filename.toLowerCase();
if (lower.endsWith('hooks.source.js') || lower.endsWith('.js')) {
// Only rebuild for hooks.source.js changes in packs or any .js in shared-hooks
if (watchDir === sharedDir || lower.endsWith('hooks.source.js')) {
console.log(`[hooks] Change detected: ${filename} (${eventType})`);
rebuild();
}
}
});
watcher.on('error', (err) => {
console.error('[hooks] Watcher error:', err);
});
} catch (err) {
console.warn(`[hooks] Could not watch ${watchDir}: ${err.message}`);
}
}
await rebuild();
}
// Allow direct execution for backwards compatibility
if (process.argv[1] === __filename) {
// Direct execution: use ./packs relative to current working directory
const WATCH = process.argv.includes('--watch') || process.argv.includes('-w');
const NO_MINIFY = process.argv.includes('--no-minify');
const defaultPacksDir = path.resolve(process.cwd(), 'packs');
const config = {
packsDir: defaultPacksDir,
projectRoot: process.cwd(),
minify: !NO_MINIFY
};
(async () => {
try {
if (WATCH) {
await startWatch(config);
} else {
await buildAll(config);
}
} catch (err) {
console.error('[hooks] Build failed:', err.message || err);
process.exit(1);
}
})();
}