-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenfl-loader.js
108 lines (98 loc) · 2.68 KB
/
openfl-loader.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
const child_process = require("node:child_process");
const fs = require("node:fs");
const path = require("node:path");
module.exports = function (source) {
var cb = this.async();
const id = this.resourcePath;
const projectXml = fs.readFileSync(this.resourcePath, { encoding: "utf8" });
const haxelibs = getHaxelibs(projectXml);
const usesGenes = haxelibs.includes("genes");
const isProduction = this.mode === "production";
const projectDir = path.dirname(id);
const mode = isProduction ? "-release" : "-debug";
const templateDir = path.join("templates", usesGenes ? "genes" : "default");
const result = child_process.spawnSync("haxelib", [
"run",
"lime",
"build",
id,
"html5",
mode,
`--template=${path.resolve(__dirname, templateDir)}`,
]);
if (result.status !== 0) {
console.error(result.output.toString());
cb(
new Error(
"Command `lime build html5` failed with error code: " + result.status
)
);
return;
}
const hxml = getHXML(id);
if (!hxml) {
cb(new Error("Command `lime display html5` failed."));
return;
}
const jsOutputPath = getJSOutputPath(hxml);
if (!jsOutputPath) {
cb(new Error("Failed to detect OpenFL html5 output file path."));
return;
}
let code = fs.readFileSync(path.resolve(projectDir, jsOutputPath), {
encoding: "utf8",
});
if (usesGenes) {
const jsOutputDir = path.dirname(jsOutputPath);
code = code.replaceAll(
/import (.*?) from ('|")./g,
`import $1 from "./${jsOutputDir}/`
);
}
cb(null, code, null);
};
function isInXMLComment(projectXML, index) {
const startCommentIndex = projectXML.lastIndexOf("<!--", index);
if (startCommentIndex == -1) {
return false;
}
const endCommentIndex = projectXML.indexOf("-->", startCommentIndex + 4);
return endCommentIndex > index;
}
function getHaxelibs(projectXML) {
const haxelibRegExp = /<haxelib\s+name=\"(.*?)\".*?\/>/g;
let haxelibElement = haxelibRegExp.exec(projectXML);
if (!haxelibElement) {
return [];
}
const haxelibs = [];
while (haxelibElement) {
const haxelib = haxelibElement[1];
if (!isInXMLComment(projectXML, haxelibElement.index)) {
haxelibs.push(haxelib);
}
haxelibElement = haxelibRegExp.exec(projectXML);
}
return haxelibs;
}
function getHXML(id) {
const result = child_process.spawnSync("haxelib", [
"run",
"lime",
"display",
id,
"html5",
]);
if (result.status !== 0) {
console.error(result.output.toString());
return null;
}
return result.output.toString();
}
function getJSOutputPath(hxml, projectDir) {
const jsArg = /\-js (.+)/.exec(hxml);
if (!jsArg) {
return null;
}
return jsArg[1];
}