This repository was archived by the owner on Jul 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathbuild-wc.js
More file actions
76 lines (65 loc) · 2.45 KB
/
Copy pathbuild-wc.js
File metadata and controls
76 lines (65 loc) · 2.45 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
const fs = require("fs-extra");
const concat = require("concat");
const path = require("path");
const glob = require("glob")
const DIST_DIR = path.resolve(__dirname, "dist/collection-editor-library-wc");
function findFirstMatchingFile(dir, patterns) {
for (const pat of patterns) {
const matches = glob.sync(pat, { cwd: dir });
if (matches && matches.length > 0) {
return path.join(dir, matches[0]);
}
}
return null;
}
async function build() {
await fs.ensureDir(DIST_DIR);
const main = findFirstMatchingFile(DIST_DIR, ["main.*.js", "main.js"]);
if (!main) {
throw new Error(
`Could not find main bundle in ${DIST_DIR}. Expected one of: main.*.js or main.js.`
);
}
const runtime = findFirstMatchingFile(DIST_DIR, ["runtime.*.js", "runtime.js"]);
const polyfills = findFirstMatchingFile(DIST_DIR, ["polyfills.*.js", "polyfills.js"]);
const scripts = findFirstMatchingFile(DIST_DIR, ["scripts.*.js", "scripts.js"]);
// Concatenation order: runtime -> polyfills -> scripts -> main
const files = [runtime, polyfills, scripts, main].filter(Boolean);
const filesToExclude = new Set(
[
path.join(DIST_DIR, "index.html"),
runtime,
polyfills,
scripts,
main,
].filter(Boolean)
);
const filter = (file) => !filesToExclude.has(file);
const subDir = "assets/collection-editor";
async function cleanDir(dir, preserve = []) {
if (!fs.existsSync(dir)) return;
const files = await fs.readdir(dir);
for (const file of files) {
if (!preserve.includes(file)) {
await fs.remove(path.join(dir, file));
}
}
}
// Build outputs for web-component
const wcDir = path.resolve(__dirname, "web-component");
await cleanDir(wcDir, ["package.json"]);
await fs.ensureDir(path.join(wcDir, subDir));
await concat(files, path.join(wcDir, subDir, "sunbird-collection-editor.js"));
await fs.copy(DIST_DIR + "/", path.join(wcDir, subDir), { filter });
// Build outputs for web-component-demo
const wcDemoDir = path.resolve(__dirname, "web-component-demo");
await cleanDir(wcDemoDir, ["index.html"]);
await fs.ensureDir(path.join(wcDemoDir, subDir));
await concat(files, path.join(wcDemoDir, subDir, "sunbird-collection-editor.js"));
await fs.copy(DIST_DIR + "/", path.join(wcDemoDir, subDir), { filter });
console.log("Web component bundles prepared successfully.");
}
build().catch((err) => {
console.error("Failed to build web component:", err);
process.exit(1);
});