Merge branch '5.4' into 5.5
[GitHub/WoltLab/WCF.git] / extra / _buildCore.js
CommitLineData
5480a636
AE
1const childProcess = require("child_process");
2const compiler = require("./compiler");
3const fs = require("fs");
4
238a4492 5async function compile(destination, files, overrides) {
3dddc464 6 let content = `// ${destination} -- DO NOT EDIT\n\n`;
5480a636 7
238a4492
TD
8 for (const filename of files) {
9 const output = await compiler.compile(fs.readFileSync(process.cwd() + `/${filename}`, "utf-8"), overrides);
10
11 content += `// ${filename}\n`;
12 content += `(function (window, undefined) { ${output.code} })(this);`;
3dddc464 13 content += "\n\n";
238a4492 14 };
5480a636 15
3dddc464
AE
16 fs.writeFileSync(destination, content);
17}
5480a636 18
238a4492
TD
19(async () => {
20 //
21 // step 1) build `WCF.Combined.min.js` and `WCF.Combined.tiny.min.js`
22 //
23 process.chdir("../wcfsetup/install/files/js/");
24 for (const COMPILER_TARGET_DEFAULT of [true, false]) {
25 let output = "WCF.Combined" + (COMPILER_TARGET_DEFAULT ? "" : ".tiny") + ".min.js";
26 console.time(output);
27 {
28 let data = fs.readFileSync(".buildOrder", "utf8");
29 let files = data
30 .trim()
31 .split(/\r?\n/)
32 .map((filename) => `${filename}.js`);
33
34 await compile(output, files, {
35 compress: {
36 global_defs: {
37 COMPILER_TARGET_DEFAULT: COMPILER_TARGET_DEFAULT,
38 },
3dddc464 39 },
238a4492 40 });
3dddc464 41 }
238a4492
TD
42 console.timeEnd(output);
43 }
3dddc464 44
238a4492
TD
45 //
46 // step 2) Redactor II + plugins
47 //
48 const redactorCombined = "redactor.combined.min.js";
49 process.chdir("3rdParty/redactor2/");
3dddc464 50
238a4492
TD
51 console.time(redactorCombined);
52 {
53 let files = ["redactor.js"];
54 fs.readdirSync("./plugins/").forEach((file) => {
55 file = `plugins/${file}`;
56 let stat = fs.statSync(file);
57 if (stat.isFile() && !stat.isSymbolicLink()) {
58 files.push(file);
59 }
3dddc464 60 });
5480a636 61
238a4492
TD
62 await compile(redactorCombined, files);
63 }
64 console.timeEnd(redactorCombined);
5480a636 65
238a4492
TD
66 //
67 // step3) build rjs
68 //
69 const rjsCmd = process.platform === "win32" ? "r.js.cmd" : "r.js";
70 process.chdir("../../");
3dddc464 71
238a4492 72 {
35cddc23 73 const configFile = "require.build.js";
238a4492
TD
74
75 for (const COMPILER_TARGET_DEFAULT of [true, false]) {
35cddc23 76 const name = `WoltLabSuite.Core${COMPILER_TARGET_DEFAULT ? '' : '.tiny'}.min`;
238a4492 77
35cddc23 78 console.time(name);
238a4492 79 {
35cddc23 80 childProcess.execSync(`${rjsCmd} -o ${configFile} name=${name} out=${name}.js`, {
238a4492
TD
81 cwd: process.cwd(),
82 stdio: [0, 1, 2],
83 });
35cddc23 84 const sourceMap = JSON.parse(fs.readFileSync(`${name}.js.map`, "utf-8"));
f64e09d4
TD
85
86 const output = await compiler.compile(
35cddc23 87 fs.readFileSync(`${name}.js`, "utf-8"),
f64e09d4
TD
88 {
89 sourceMap: {
90 content: JSON.stringify(sourceMap),
35cddc23 91 url: `${name}.js.map`,
f64e09d4 92 includeSources: true,
1da83c30
TD
93 },
94 compress: {
95 global_defs: {
96 COMPILER_TARGET_DEFAULT: COMPILER_TARGET_DEFAULT,
97 },
f64e09d4
TD
98 }
99 }
100 );
35cddc23
TD
101 fs.writeFileSync(`${name}.js`, output.code);
102 fs.writeFileSync(`${name}.js.map`, output.map);
238a4492 103 }
35cddc23 104 console.timeEnd(name);
3dddc464 105 }
238a4492
TD
106 }
107
108 //
109 // step 4) legacy ACP scripts
110 //
111 process.chdir("../acp/js/");
112
113 await Promise.all(fs.readdirSync("./")
114 .filter((filename) => {
115 const stat = fs.statSync(filename);
116 if (stat.isFile() && !stat.isSymbolicLink()) {
117 return filename.match(/\.js$/) && !filename.match(/\.min\.js$/);
118 }
119
120 return false;
121 })
122 .map(async (filename) => {
123 console.time(filename);
124 {
125 const output = await compiler.compile(fs.readFileSync(process.cwd() + `/${filename}`, "utf-8"));
126 fs.writeFileSync(filename.replace(/\.js$/, ".min.js"), output.code);
127 }
128 console.timeEnd(filename);
129 }));
130
131})();