Skip to content

Instantly share code, notes, and snippets.

@chamberlainpi
Last active August 29, 2015 14:12
Show Gist options
  • Save chamberlainpi/24a1fac7327cc63bcfc4 to your computer and use it in GitHub Desktop.
Save chamberlainpi/24a1fac7327cc63bcfc4 to your computer and use it in GitHub Desktop.
Macro for removing (comment-out) specific modules that are automatically created by Haxe compiler in JS
package macros;
import sys.FileSystem;
import sys.io.File;
/**
* @author Pierre Chamberlain
* @usage In a compile.hxml file, you can call this command like so:
*
* --macro macros.MacroModuleFixer.run("yourFile.js", "MyModule")
*
*/
class MacroModuleFixer
{
public static function run(outputFile:String, moduleNames:String):Void {
if (!FileSystem.exists(outputFile)) {
throw "The file does not exists: " + outputFile;
}
var outputContent = File.getContent(outputFile);
var modules = moduleNames.split(",");
for (mod in modules) {
var keyMod = "var " + mod + " = {}";
outputContent = StringTools.replace(outputContent, keyMod, "//" + keyMod);
}
File.saveContent(outputFile, outputContent);
}
}
@chamberlainpi
Copy link
Author

The purpose of this Macro is to remove module-initialization objects from a Haxe-generated JS file when it is already defined elsewhere in the larger scope of a project (from another JS file, or directly on an HTML / PHP page).

It does not simply remove ALL modules though - you have to specify them in a comma-delimited list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment