Support JS dependency embedding on Emscripten output

This commit is contained in:
Joshua Granick
2023-05-20 11:19:33 -07:00
parent b410a90121
commit f285df6f76
3 changed files with 41 additions and 1 deletions

View File

@@ -0,0 +1,3 @@
::SOURCE_FILE::
::if embeddedLibraries::::foreach (embeddedLibraries)::
::__current__::::end::::end::

View File

@@ -65,7 +65,6 @@
}, true);::end::::end::
</script>
::if DEBUG::<script type="text/javascript" src="./webgl-debug.js"></script>::end::
<script type="text/javascript" src="./::APP_FILE::.js"></script>
</body>

View File

@@ -23,6 +23,7 @@ import sys.FileSystem;
class EmscriptenPlatform extends PlatformTarget
{
private var dependencyPath:String;
private var outputFile:String;
public function new(command:String, _project:HXProject, targetFlags:Map<String, String>)
@@ -94,6 +95,7 @@ class EmscriptenPlatform extends PlatformTarget
project = defaults;
targetDirectory = Path.combine(project.app.path, project.config.getString("emscripten.output-directory", "emscripten"));
dependencyPath = project.config.getString("emscripten.dependency-path", "lib");
outputFile = targetDirectory + "/bin/" + project.app.file + ".js";
}
@@ -262,6 +264,24 @@ class EmscriptenPlatform extends PlatformTarget
System.runCommand(targetDirectory + "/obj", "emcc", args, true, false, true);
if (FileSystem.exists(outputFile))
{
var context = project.templateContext;
context.SOURCE_FILE = File.getContent(outputFile);
context.embeddedLibraries = [];
for (dependency in project.dependencies)
{
if (dependency.embed && StringTools.endsWith(dependency.path, ".js") && FileSystem.exists(dependency.path))
{
var script = File.getContent(dependency.path);
context.embeddedLibraries.push(script);
}
}
System.copyFileTemplate(project.templatePaths, "emscripten/output.js", outputFile, context);
}
if (project.targetFlags.exists("minify"))
{
HTML5Helper.minify(project, targetDirectory + "/bin/" + project.app.file + ".js");
@@ -409,6 +429,24 @@ class EmscriptenPlatform extends PlatformTarget
context.favicons.push({rel: "shortcut icon", type: "image/png", href: "./favicon.png"});
}
for (dependency in project.dependencies)
{
if (!dependency.embed)
{
if (StringTools.endsWith(dependency.name, ".js"))
{
context.linkedLibraries.push(dependency.name);
}
else if (StringTools.endsWith(dependency.path, ".js") && FileSystem.exists(dependency.path))
{
var name = Path.withoutDirectory(dependency.path);
context.linkedLibraries.push("./" + dependencyPath + "/" + name);
System.copyIfNewer(dependency.path, Path.combine(destination, Path.combine(dependencyPath, name)));
}
}
}
for (asset in project.assets)
{
var path = Path.combine(targetDirectory + "/obj/assets", asset.targetPath);