-
Notifications
You must be signed in to change notification settings - Fork 35
/
fixup-dist.js
133 lines (119 loc) · 2.67 KB
/
fixup-dist.js
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
/* eslint-disable import/no-extraneous-dependencies */
import os from "os";
import fs from "fs";
import path from "path";
import { globSync } from "glob";
import chokidar from "chokidar";
const ROOT = "dist";
/**
* Read file contents.
*
* @param {string} file - file name
* @returns {string} - file contents
*/
function readFile(file) {
return fs.readFileSync(path.resolve(file), { encoding: "utf8", flag: "r" });
}
/**
* Write file contents.
*
* @param {string} file - file name
* @param {string} data - file contents
*/
function writeFile(file, data) {
fs.writeFileSync(path.resolve(file), data, { encoding: "utf8" });
}
/**
* Edit file contents.
*
* @param {string} file - file name
* @param {function} callback - callback function
*/
function editFile(file, callback) {
if (fs.existsSync(file)) {
let data = readFile(file);
data = callback(data);
writeFile(file, data);
}
}
/**
* Add type definitions for CSS files.
*
* @param {string} file - file name
*/
function fixupCssDefinitions(file) {
writeFile(`${file}.d.ts`, ["declare const styles: unknown;", "export default styles;"].join(os.EOL));
}
/**
* Add client boundary.
*
* @param {string} file - file name
*/
function fixupClientBoundary(file) {
editFile(file, (data) => {
return /import.*(?:useMemo|useState|useRef|useCallback|useReducer).*from\s*['"]react['"]/.test(data) ||
/import.*from.*\.\/(?:rows|columns|masonry)\.js/.test(data)
? ['"use client";', data].join(os.EOL)
: data;
});
}
/**
* Run all fix-ups.
*
* @param {boolean} [watchMode] - watch mode flag
*/
function fixup(watchMode) {
try {
globSync(`${ROOT}/**/*.js`).forEach((file) => {
fixupClientBoundary(file);
});
globSync(`${ROOT}/**/*.css`).forEach((file) => {
fixupCssDefinitions(file);
});
globSync(`${ROOT}/**/*-*.{js,d\\.ts}`).forEach((file) => {
// eslint-disable-next-line no-console
console.error(`Unexpected chunk: ${file}${os.EOL}`);
if (!watchMode) {
process.exit(1);
}
});
} catch (error) {
if (watchMode) {
// eslint-disable-next-line no-console
console.error(error);
} else {
throw error;
}
}
}
/**
* Run all fix-ups in watch mode.
*/
function watch() {
let timeout;
let running = false;
chokidar.watch(ROOT).on("all", () => {
clearTimeout(timeout);
timeout = setTimeout(() => {
if (!running) {
running = true;
try {
fixup(true);
} finally {
running = false;
}
}
}, 3_000);
});
}
/**
* Main entrypoint.
*/
function main() {
if ([...process.argv].includes("-w")) {
watch();
} else {
fixup();
}
}
main();