forked from openfl/lime
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHTML5Helper.hx
More file actions
238 lines (206 loc) · 5.99 KB
/
HTML5Helper.hx
File metadata and controls
238 lines (206 loc) · 5.99 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
package lime.tools;
import hxp.*;
import lime.tools.Architecture;
import lime.tools.Asset;
import lime.tools.HXProject;
import lime.tools.Platform;
import sys.FileSystem;
import sys.io.File;
#if cpp
import cpp.vm.Thread;
#end
class HTML5Helper
{
public static function encodeSourceMappingURL(sourceFile:String)
{
// This is only required for projects with url-unsafe characters built with a Haxe version prior to 4.0.0
var filename = Path.withoutDirectory(sourceFile);
if (filename != StringTools.urlEncode(filename))
{
var output = System.runProcess("", "haxe", ["-version"], true, true, true, false, true);
var haxeVer:Version = StringTools.trim(output);
if (haxeVer < ("4.0.0":Version))
{
var replaceString = "//# sourceMappingURL=" + filename + ".map";
var replacement = "//# sourceMappingURL=" + StringTools.urlEncode(filename) + ".map";
System.replaceText(sourceFile, replaceString, replacement);
}
}
}
public static function generateWebfonts(project:HXProject, font:Asset):Void
{
var suffix = switch (System.hostPlatform)
{
case WINDOWS: "-windows.exe";
case MAC: "-mac";
case LINUX: "-linux";
default: return;
}
if (suffix == "-linux")
{
if (System.hostArchitecture == X86)
{
suffix += "32";
}
else
{
suffix += "64";
}
}
var templatePaths = [
Path.combine(Haxelib.getPath(new Haxelib(#if lime "lime" #else "hxp" #end)), #if lime "templates" #else "" #end)
].concat(project.templatePaths);
var webify = System.findTemplate(templatePaths, "bin/webify" + suffix);
if (System.hostPlatform != WINDOWS)
{
Sys.command("chmod", ["+x", webify]);
}
if (Log.verbose)
{
System.runCommand("", webify, [FileSystem.fullPath(font.sourcePath)]);
}
else
{
System.runProcess("", webify, [FileSystem.fullPath(font.sourcePath)], true, true, true);
}
}
public static function launch(project:HXProject, path:String, port:Int = 0):Void
{
if (project.app.url != null && project.app.url != "")
{
System.openURL(project.app.url);
}
else
{
var templatePaths = [
Path.combine(Haxelib.getPath(new Haxelib(#if lime "lime" #else "hxp" #end)), #if lime "templates" #else "" #end)
].concat(project.templatePaths);
var server = System.findTemplate(templatePaths, "bin/node/http-server/bin/http-server");
var args = [server, path, "-c-1", "--cors"];
if (project.targetFlags.exists("port"))
{
port = Std.parseInt(project.targetFlags.get("port"));
}
if (port != 0)
{
args.push("-p");
args.push(Std.string(port));
Log.info("", "\x1b[1mStarting local web server:\x1b[0m http://localhost:" + port);
}
else
{
Log.info("", "\x1b[1mStarting local web server:\x1b[0m http://localhost:[3000*]");
}
if (!project.targetFlags.exists("nolaunch"))
{
args.push("-o");
}
if (!Log.verbose)
{
args.push("--silent");
}
System.runCommand("", "node", args);
}
}
public static function minify(project:HXProject, sourceFile:String):Bool
{
if (FileSystem.exists(sourceFile))
{
var tempFile = System.getTemporaryFile(".js");
if (project.targetFlags.exists("terser"))
{
var executable = "npx";
var terser = "terser";
if (!project.targetFlags.exists("npx"))
{
var templatePaths = [
Path.combine(Haxelib.getPath(new Haxelib(#if lime "lime" #else "hxp" #end)), #if lime "templates" #else "" #end)
].concat(project.templatePaths);
terser = System.findTemplate(templatePaths, "bin/node/terser/bin/terser");
}
var args = [
terser,
sourceFile,
"-c",
"-m",
"-o",
tempFile
];
if (FileSystem.exists(sourceFile + ".map"))
{
args.push("--source-map");
args.push('content=\'${sourceFile}.map\'');
}
System.runCommand("", "node", args);
}
else if (project.targetFlags.exists("yui"))
{
Log.error("YUI Compressor is no longer supported by Lime for JavaScript minification.");
}
else
{
var executable:String;
var args:Array<String>;
if (project.targetFlags.exists("npx"))
{
executable = "npx";
args = [
"google-closure-compiler"
];
}
else
{
executable = "java";
var templatePaths = [
Path.combine(Haxelib.getPath(new Haxelib(#if lime "lime" #else "hxp" #end)), #if lime "templates" #else "" #end)
].concat(project.templatePaths);
args = [
"-Dapple.awt.UIElement=true",
"-jar",
System.findTemplate(templatePaths, "bin/compiler.jar"),
];
}
args.push("--strict_mode_input");
args.push("false");
args.push("--js");
args.push(sourceFile);
args.push("--js_output_file");
args.push(tempFile);
if (project.targetFlags.exists("advanced"))
{
args.push("--compilation_level");
args.push("ADVANCED_OPTIMIZATIONS");
}
if (FileSystem.exists(sourceFile + ".map") || project.targetFlags.exists("source-map"))
{
// if an input .js.map exists closure automatically detects it (from sourceMappingURL)
// --source_map_location_mapping adds file:// to paths (similarly to haxe's .js.map)
args.push("--create_source_map");
args.push(tempFile + ".map");
args.push("--source_map_location_mapping");
args.push("/|file:///");
}
if (!Log.verbose)
{
args.push("--jscomp_off=uselessCode");
args.push("--jscomp_off=suspiciousCode"); // avoid warnings caused by the embedded minified libraries
}
System.runCommand("", executable, args);
if (FileSystem.exists(tempFile + ".map"))
{
// closure does not include a sourceMappingURL in the created .js, we do it here
var f = File.append(tempFile);
f.writeString("//# sourceMappingURL=" + StringTools.urlEncode(Path.withoutDirectory(sourceFile)) + ".map");
f.close();
File.copy(tempFile + ".map", sourceFile + ".map");
FileSystem.deleteFile(tempFile + ".map");
}
}
FileSystem.deleteFile(sourceFile);
File.copy(tempFile, sourceFile);
FileSystem.deleteFile(tempFile);
return true;
}
return false;
}
}