-
Notifications
You must be signed in to change notification settings - Fork 39
/
fixup-dist.js
191 lines (170 loc) · 4.22 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
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
/* 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);
}
}
/**
* Fixup main bundle.
*
* @param {string} file - file name
*/
function fixupMainBundle(file) {
editFile(file, (data) => {
const regex = /import.*\r?\n/g;
return ["'use client';", ...data.match(regex).map((line) => line.trim()), data.replaceAll(regex, "").trim()].join(
os.EOL,
);
});
}
/**
* Remove side effect imports.
*
* @param {string} file - file name
*/
function cleanupSideEffectImports(file) {
editFile(file, (data) => {
const regex = /import\s*['"]+[^'"]+['"]+;*\r?\n/g;
return data.replaceAll(regex, "");
});
}
/**
* 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));
}
/**
* Fixup plugin's imports.
*
* @param {string} file - file name
*/
function fixupPluginsImports(file) {
const parts = file.split(path.sep);
if (parts.length === 4) {
const plugin = parts[2];
const parseImports = (data) => {
const importsMatch = [...data.matchAll(/import\s*\{(.*)}\s*from\s*['"]\.\.\/\.\.\/(?:types|index).js['"]/g)];
return importsMatch.length > 0 ? importsMatch[0][1].split(/[ ,]+/).filter(Boolean) : [];
};
editFile(file, (data) => {
const imports = new Set();
parseImports(data).forEach(imports.add, imports);
parseImports(readFile(`src/plugins/${plugin}/index.ts`)).forEach(imports.add, imports);
return data.replaceAll(
/import\s*\{.*}\s*from\s*['"]\.\.\/\.\.\/types.js['"]/g,
`import { ${Array.from(imports).join(", ")} } from '../../types.js'`,
);
});
}
}
/**
* Fixup plugin's module augmentation.
*
* @param {string} file - file name
*/
function fixupPluginsModuleAugmentation(file) {
editFile(file, (data) => {
const regex = /declare module "\.\.\/\.\.\/types.js"/g;
return data.replaceAll(regex, 'declare module "yet-another-react-lightbox"');
});
}
/**
* Run all fix-ups.
*
* @param {boolean} [watchMode] - watch mode flag
*/
function fixup(watchMode) {
try {
fixupMainBundle(`${ROOT}/index.js`);
globSync(`${ROOT}/**/*.{js,d\\.ts}`).forEach((file) => {
cleanupSideEffectImports(file);
});
globSync(`${ROOT}/**/*.css`).forEach((file) => {
fixupCssDefinitions(file);
});
globSync(`${ROOT}/plugins/**/index.d.ts`).forEach((file) => {
fixupPluginsModuleAugmentation(file);
fixupPluginsImports(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();