-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathwpbf.mjs
More file actions
364 lines (306 loc) · 9.21 KB
/
wpbf.mjs
File metadata and controls
364 lines (306 loc) · 9.21 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env zx
import "zx/globals";
import { fileURLToPath } from "url";
import {
intro,
outro,
isCancel,
cancel,
text,
select,
spinner,
} from "@clack/prompts";
import { resolve, dirname } from "path";
import { build } from "vite";
import {
deleteIfDirExists,
getErrorMessage,
getViteConfig,
toPascalCase,
} from "./build-scripts/build-utils.mjs";
process.env.NODE_ENV = "production";
process.env.context = "browser";
process.env.sourceType = "script";
const DELETE_DIST_DIR_BEFORE_BUILD = false;
const ALLOWED_FILE_EXTENSIONS = ["scss", "js", "ts", "jsx", "tsx"];
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const themeBuildDir = "./build";
const rootFilesAndDirsToSkip = [
".parcel-cache",
"node_modules",
"build",
"build-scripts",
".babelrc",
".editorconfig",
".git",
".gitattributes",
".gitignore",
".npmrc",
".parcelrc",
".prettierrc",
".eslintrc.json",
"manifest.json",
"package.json",
"package-lock.json",
"phpcs.xml",
"pnpm-lock.yaml",
"pnpm-workspace.yaml",
"readme.md",
"tsconfig.json",
"types.js",
"wpbf.mjs",
];
const loadingSpinner = spinner();
async function main() {
intro(`Page Builder Framework`);
const selectedTask = await select({
message: "Please select a task.",
options: [
{ value: "build-wp-theme", label: "Build WP theme" },
{ value: "build-customizer-control", label: "Build customizer control" },
{ value: "build-asset", label: "Build asset" },
],
});
if (isCancel(selectedTask)) {
cancel("Task cancelled.");
process.exit(0);
}
if (selectedTask === "build-wp-theme") {
loadingSpinner.start("Building WP theme...");
buildWpTheme();
loadingSpinner.stop();
} else if (selectedTask === "build-customizer-control") {
const controlName = await text({
message: "Insert the customizer control namespace (e.g: slider):",
});
if (isCancel(controlName)) {
cancel("Build customizer control cancelled.");
process.exit(0);
}
loadingSpinner.start(`Building customizer control: ${controlName}...`);
if (DELETE_DIST_DIR_BEFORE_BUILD) {
deleteIfDirExists(
resolve(
__dirname,
`Customizer/Controls/${toPascalCase(controlName)}/dist`,
),
);
}
const response = await bundleCustomizerControl(controlName);
if (response.success) {
loadingSpinner.stop(response.message);
} else {
loadingSpinner.stop(response.message, 500);
}
} else if (selectedTask === "build-asset") {
const filePath = await text({
message: "Insert the file path to build (e.g: assets/js/site.js):",
});
if (isCancel(filePath)) {
cancel("Build asset cancelled.");
process.exit(0);
}
if (!ALLOWED_FILE_EXTENSIONS.some((ext) => filePath.endsWith(`.${ext}`))) {
cancel(
`Invalid file extension. Allowed extensions: ${ALLOWED_FILE_EXTENSIONS.join(", ")}`,
);
process.exit(0);
}
const fileType = filePath.split(".").pop();
const outputDirFromUser = await text({
message:
"Insert the output directory path. If empty, it will use default location (js: js/min, scss: css/min).",
});
const outputDir = outputDirFromUser
? resolve(__dirname, String(outputDirFromUser))
: fileType === "scss"
? resolve(__dirname, "css/min")
: resolve(__dirname, "js/min");
loadingSpinner.start(`Building asset file: ${filePath}...`);
const response = await buildAsset(filePath, outputDir);
if (response.success) {
loadingSpinner.stop(response.message);
} else {
loadingSpinner.stop(response.message, 500);
}
}
outro(`Done`);
}
/**
* Build an asset file.
*
* @param {string} filePath The file path to build.
* @param {string} outputDir The output directory path.
*
* @returns {Promise<{success: boolean, message: string}>} The result of the bundling process.}
*/
async function buildAsset(filePath, outputDir) {
try {
return await bundleViaViteAPI(filePath, outputDir);
} catch (err) {
return {
success: false,
message: getErrorMessage(err),
};
}
}
/**
* Bundle the customizer control.
*
* @param {string} controlName The customizer control name. E.g: checkbox.
* @returns {Promise<{success: boolean, message: string}>} The result of the bundling process.}
*/
async function bundleCustomizerControl(controlName) {
const pascalCaseControlName = toPascalCase(controlName);
const srcDir = `Customizer/Controls/${pascalCaseControlName}/src`;
const distDir = `Customizer/Controls/${pascalCaseControlName}/dist`;
const absDistDir = resolve(__dirname, distDir);
// Find files inside `srcDir` directory that suffixed with `-control.ts` or `-control.tsx`.
const controlFiles = fs.readdirSync(srcDir).filter((file) => {
return file.endsWith("-control.ts") || file.endsWith("-control.tsx");
});
if (controlFiles.length === 0) {
return {
success: false,
message: `No control files found in ${srcDir} directory.`,
};
}
/**
* The entries to pass to Parcel.
*
* @type {import('vite').Rollup.InputOption}
*/
const entries = {};
for (const controlFile of controlFiles) {
// Get the control file name without the "-control.ts" and "-control.tsx" suffixes.
const controlFileName = controlFile.replace(/-control\.tsx?$/, "");
// Add the control file to the entries.
entries[
`${controlFileName}-control${controlFile.endsWith("-control.scss") ? "-style" : ""}`
] = resolve(srcDir, controlFile);
// Check if ${controlFileName}-preview.ts exists in src directory.
const previewPath = resolve(srcDir, `${controlFileName}-preview.ts`);
if (fs.existsSync(previewPath)) {
entries[`${controlFileName}-preview`] = previewPath;
}
// Check if ${controlFileName}-preview.tsx exists in src directory.
const previewPathTsx = resolve(srcDir, `${controlFileName}-preview.tsx`);
if (fs.existsSync(previewPathTsx)) {
entries[`${controlFileName}-preview`] = previewPathTsx;
}
}
try {
return await bundleViaViteAPI(entries, absDistDir, true);
} catch (err) {
return {
success: false,
message: getErrorMessage(err),
};
}
}
/**
* Bundle the customizer control using the Vite API.
*
* @param {import('vite').Rollup.InputOption} entries The entries to pass to Vite.
* @param {string} distDir The dist directory path.
* @param {boolean} clearDistDir Whether to clear the dist directory before building.
*
* @returns {Promise<{success: boolean, message: string}>} The result of the bundling process.
*/
async function bundleViaViteAPI(entries, distDir, clearDistDir = false) {
try {
if (typeof entries === "string") {
await build(getViteConfig(entries, distDir, clearDistDir));
return {
success: true,
message: `✨`,
};
}
/**
* Unfortunately, multiple entries are not supported when "output.inlineDynamicImports" is true.
*
* Vite build failed:
* Invalid value for option "output.inlineDynamicImports" - multiple inputs are not supported when "output.inlineDynamicImports" is true.
*
* So instead of executing `await build(getViteConfig(entries, distDir));`,
* We need to loop the entries and build them individually.
*
* @see https://github.com/rollup/rollup/issues/5601
*/
await Promise.all(
Object.entries(entries).map(([entryKey, filePath]) => {
return build(getViteConfig(filePath, distDir));
}),
);
return {
success: true,
message: `✨`,
};
} catch (error) {
throw new Error(`Vite build failed: ${getErrorMessage(error)}`);
}
}
/**
* Copy the files and directories from the source directory to the destination directory.
*
* @param {string} srcPath The source directory path.
* @param {string} destPath The destination directory path.
*/
function copyFilesAndDir(srcPath, destPath) {
const filesAndDirsToCopy = fs.readdirSync(srcPath);
filesAndDirsToCopy.forEach((fileOrDir) => {
if (rootFilesAndDirsToSkip.includes(fileOrDir)) {
return;
}
fs.copySync(`${srcPath}/${fileOrDir}`, `${destPath}/${fileOrDir}`);
});
}
/**
* Delete the map files inside a directory.
*
* @param {string} dir The directory path.
*/
function deleteMapFiles(dir) {
const filesAndDirs = fs.readdirSync(dir);
filesAndDirs.forEach((fileOrDir) => {
if (fs.lstatSync(`${dir}/${fileOrDir}`).isDirectory()) {
deleteMapFiles(`${dir}/${fileOrDir}`);
return;
}
if (fileOrDir.endsWith(".map")) {
fs.unlinkSync(`${dir}/${fileOrDir}`);
}
if (fileOrDir.endsWith("-min.js") || fileOrDir.endsWith("-min.css")) {
const fileContent = fs.readFileSync(`${dir}/${fileOrDir}`, "utf8");
let newFileContent = "";
/**
* Remove the whole line that begins with "//# sourceMappingURL=" or "/*# sourceMappingURL="
*/
fileContent.split("\n").forEach((line) => {
if (
line.startsWith("//# sourceMappingURL=") ||
line.startsWith("/*# sourceMappingURL=")
) {
return;
}
newFileContent += line + "\n";
});
// If the file content contains multiple lines, then remove the second line.
if (newFileContent.includes("\n\n")) {
newFileContent = newFileContent.replace(/\n\n/g, "\n");
}
fs.writeFileSync(`${dir}/${fileOrDir}`, newFileContent, "utf8");
}
});
}
function buildWpTheme() {
deleteIfDirExists(themeBuildDir);
fs.mkdirSync(themeBuildDir);
const wpbfBuildDir = `${themeBuildDir}/page-builder-framework`;
deleteIfDirExists(wpbfBuildDir);
fs.mkdirSync(wpbfBuildDir);
copyFilesAndDir(".", wpbfBuildDir);
deleteMapFiles(wpbfBuildDir);
}
main();