-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite-plugin-openfl.js
179 lines (170 loc) · 4.87 KB
/
vite-plugin-openfl.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
const child_process = require("node:child_process");
const fs = require("node:fs");
const path = require("node:path");
const vite = require("vite");
module.exports = function openflPlugin() {
/** @type vite.ResolvedConfig */ let config = null;
return {
name: "openfl",
handleHotUpdate(ctx) {
if (ctx.file.endsWith(".hx")) {
// if a Haxe source file changes, restart the server
ctx.server.restart();
}
},
config() {
const projectXmlPath = path.resolve(process.cwd(), "project.xml");
const hxml = getHXML(projectXmlPath);
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;
}
const projectPath = path.dirname(projectXmlPath);
const fullOutputFolder = path.resolve(
projectPath,
path.dirname(jsOutputPath)
);
const ignorePath = `${fullOutputFolder}/**`;
return {
server: {
watch: {
ignored: [ignorePath],
},
},
};
},
configResolved(resolvedConfig) {
config = resolvedConfig;
},
resolveId(source, importer) {
if (!/\/project\.xml$/.test(importer)) {
return;
}
const hxml = getHXML(importer);
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;
}
const projectDir = path.dirname(importer);
const outDir = path.dirname(path.resolve(projectDir, jsOutputPath));
if (!path.isAbsolute(source)) {
const absoluteSource = path.resolve(outDir, source);
if (fs.existsSync(absoluteSource)) {
return {
id: absoluteSource,
};
}
}
},
transform(code, id, options) {
if (!/\/project\.xml$/.test(id)) {
return;
}
const projectXml = fs.readFileSync(id, {
encoding: "utf8",
});
const haxelibs = getHaxelibs(projectXml);
const usesGenes = haxelibs.includes("genes");
// if using rollup, instead of vite, configResolved() won't be called,
// so the config variable will not get set. that's fine, though. we can
// assume that we're building and not serving.
const isServeCommand = !config || config.command === "serve";
const projectDir = path.dirname(id);
const mode = isServeCommand ? "-debug" : "-release";
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)}`,
]);
const logger = vite.createLogger("info");
result.output.forEach((line) => {
if (!line) {
return;
}
logger.info(line.toString());
});
if (result.status !== 0) {
this.error("OpenFL Build Failed with status 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;
}
return {
code: fs.readFileSync(path.resolve(projectDir, jsOutputPath), {
encoding: "utf8",
}),
};
},
};
};
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) {
const jsArg = /\-js (.+)/.exec(hxml);
if (!jsArg) {
return null;
}
return jsArg[1];
}