diff --git a/tools/cli/src/main/java/org/teavm/cli/devserver/TeaVMDevServerRunner.java b/tools/cli/src/main/java/org/teavm/cli/devserver/TeaVMDevServerRunner.java index ff75518ea..0750ffdb5 100644 --- a/tools/cli/src/main/java/org/teavm/cli/devserver/TeaVMDevServerRunner.java +++ b/tools/cli/src/main/java/org/teavm/cli/devserver/TeaVMDevServerRunner.java @@ -30,6 +30,7 @@ import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.eclipse.jetty.util.log.Log; +import org.teavm.backend.javascript.JSModuleType; import org.teavm.common.json.JsonParser; import org.teavm.devserver.DevServer; import org.teavm.tooling.ConsoleTeaVMToolLog; @@ -80,6 +81,12 @@ public final class TeaVMDevServerRunner { .hasArgs() .longOpt("property") .build()); + options.addOption(Option.builder() + .argName("module_type") + .hasArg() + .longOpt("js-module-type") + .desc("JS module type (umd, common-js, es2015 or none)") + .build()); options.addOption(Option.builder() .argName("number") .hasArg() @@ -182,6 +189,18 @@ public final class TeaVMDevServerRunner { if (commandLine.hasOption("preserved-classes")) { devServer.getPreservedClasses().addAll(List.of(commandLine.getOptionValues("preserved-classes"))); } + if (commandLine.hasOption("js-module-type")) { + var moduleTypeValue = commandLine.getOptionValue("js-module-type"); + JSModuleType type; + try { + type = JSModuleType.valueOf(moduleTypeValue.toUpperCase().replace('-', '_')); + } catch (IllegalArgumentException e) { + System.err.println("Invalid value for --js-module-type: " + moduleTypeValue); + printUsage(); + type = null; + } + devServer.setJsModuleType(type); + } if (commandLine.hasOption("proxy-url")) { devServer.setProxyUrl(commandLine.getOptionValue("proxy-url")); diff --git a/tools/devserver/src/main/java/org/teavm/devserver/CodeServlet.java b/tools/devserver/src/main/java/org/teavm/devserver/CodeServlet.java index 88e8e0583..cd68e3bf3 100644 --- a/tools/devserver/src/main/java/org/teavm/devserver/CodeServlet.java +++ b/tools/devserver/src/main/java/org/teavm/devserver/CodeServlet.java @@ -67,6 +67,7 @@ import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.client.io.UpgradeListener; import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; +import org.teavm.backend.javascript.JSModuleType; import org.teavm.backend.javascript.JavaScriptTarget; import org.teavm.cache.InMemoryMethodNodeCache; import org.teavm.cache.InMemoryProgramCache; @@ -122,6 +123,7 @@ public class CodeServlet extends HttpServlet { private String proxyBaseUrl; private Map properties = new LinkedHashMap<>(); private List preservedClasses = new ArrayList<>(); + private JSModuleType jsModuleType; private Map> sourceFileCache = new HashMap<>(); @@ -223,6 +225,10 @@ public class CodeServlet extends HttpServlet { return properties; } + public void setJsModuleType(JSModuleType jsModuleType) { + this.jsModuleType = jsModuleType; + } + public void setLogBuildErrors(boolean logBuildErrors) { this.logBuildErrors = logBuildErrors; } @@ -882,6 +888,9 @@ public class CodeServlet extends HttpServlet { jsTarget.setObfuscated(false); jsTarget.setAstCache(astCache); jsTarget.setDebugEmitter(debugInformationBuilder); + if (jsModuleType != null) { + jsTarget.setModuleType(jsModuleType); + } jsTarget.setStrict(true); vm.setOptimizationLevel(TeaVMOptimizationLevel.SIMPLE); vm.setCacheStatus(classSource); diff --git a/tools/devserver/src/main/java/org/teavm/devserver/DevServer.java b/tools/devserver/src/main/java/org/teavm/devserver/DevServer.java index 5537252e8..66725be12 100644 --- a/tools/devserver/src/main/java/org/teavm/devserver/DevServer.java +++ b/tools/devserver/src/main/java/org/teavm/devserver/DevServer.java @@ -23,6 +23,7 @@ import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; +import org.teavm.backend.javascript.JSModuleType; import org.teavm.tooling.TeaVMToolLog; public class DevServer { @@ -40,6 +41,7 @@ public class DevServer { private List listeners = new ArrayList<>(); private Map properties = new LinkedHashMap<>(); private List preservedClasses = new ArrayList<>(); + private JSModuleType jsModuleType; private boolean compileOnStartup; private boolean logBuildErrors = true; @@ -123,6 +125,10 @@ public class DevServer { return properties; } + public void setJsModuleType(JSModuleType jsModuleType) { + this.jsModuleType = jsModuleType; + } + public void invalidateCache() { servlet.invalidateCache(); } @@ -170,6 +176,7 @@ public class DevServer { servlet.setLogBuildErrors(logBuildErrors); servlet.getProperties().putAll(properties); servlet.getPreservedClasses().addAll(preservedClasses); + servlet.setJsModuleType(jsModuleType); for (DevServerListener listener : listeners) { servlet.addListener(listener); } diff --git a/tools/gradle/src/main/java/org/teavm/gradle/TeaVMPlugin.java b/tools/gradle/src/main/java/org/teavm/gradle/TeaVMPlugin.java index 819ffbe02..fcb8afa06 100644 --- a/tools/gradle/src/main/java/org/teavm/gradle/TeaVMPlugin.java +++ b/tools/gradle/src/main/java/org/teavm/gradle/TeaVMPlugin.java @@ -147,6 +147,7 @@ public class TeaVMPlugin implements Plugin { task.getMainClass().convention(js.getMainClass()); task.getClasspath().from(task.getProject().getConfigurations().getByName(CLASSPATH_CONFIGURATION_NAME)); task.getPreservedClasses().addAll(js.getPreservedClasses()); + task.getJsModuleType().convention(js.getModuleType()); task.getProcessMemory().convention(js.getDevServer().getProcessMemory()); task.getProperties().putAll(js.getProperties()); task.getServerClasspath().from(configuration); diff --git a/tools/gradle/src/main/java/org/teavm/gradle/tasks/JavaScriptDevServerTask.java b/tools/gradle/src/main/java/org/teavm/gradle/tasks/JavaScriptDevServerTask.java index f72e7fb94..ed0b03ae6 100644 --- a/tools/gradle/src/main/java/org/teavm/gradle/tasks/JavaScriptDevServerTask.java +++ b/tools/gradle/src/main/java/org/teavm/gradle/tasks/JavaScriptDevServerTask.java @@ -29,6 +29,7 @@ import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.Optional; import org.gradle.api.tasks.TaskAction; import org.gradle.internal.logging.progress.ProgressLoggerFactory; +import org.teavm.gradle.api.JSModuleType; public abstract class JavaScriptDevServerTask extends DefaultTask { @Classpath @@ -50,6 +51,10 @@ public abstract class JavaScriptDevServerTask extends DefaultTask { @Optional public abstract ListProperty getPreservedClasses(); + @Input + @Optional + public abstract Property getJsModuleType(); + @Input public abstract Property getMainClass(); @@ -111,6 +116,9 @@ public abstract class JavaScriptDevServerTask extends DefaultTask { pm.setProperties(getProperties().get()); pm.setPreservedClasses(getPreservedClasses().get()); + if (getJsModuleType().isPresent()) { + pm.setJsModuleType(getJsModuleType().get()); + } pm.setServerClasspath(getServerClasspath().getFiles()); pm.setMainClass(getMainClass().get()); diff --git a/tools/gradle/src/main/java/org/teavm/gradle/tasks/ProjectDevServerManager.java b/tools/gradle/src/main/java/org/teavm/gradle/tasks/ProjectDevServerManager.java index f744af229..fca74b25d 100644 --- a/tools/gradle/src/main/java/org/teavm/gradle/tasks/ProjectDevServerManager.java +++ b/tools/gradle/src/main/java/org/teavm/gradle/tasks/ProjectDevServerManager.java @@ -43,6 +43,7 @@ import org.teavm.common.json.JsonArrayValue; import org.teavm.common.json.JsonObjectValue; import org.teavm.common.json.JsonParser; import org.teavm.common.json.JsonValue; +import org.teavm.gradle.api.JSModuleType; public class ProjectDevServerManager { private Set serverClasspath = new LinkedHashSet<>(); @@ -51,6 +52,7 @@ public class ProjectDevServerManager { private String targetFilePath; private Map properties = new LinkedHashMap<>(); private Set preservedClasses = new LinkedHashSet<>(); + private JSModuleType jsModuleType; private String mainClass; private boolean stackDeobfuscated; private boolean indicator; @@ -79,6 +81,7 @@ public class ProjectDevServerManager { private String runningTargetFilePath; private Map runningProperties = new HashMap<>(); private Set runningPreservedClasses = new HashSet<>(); + private JSModuleType runningJsModuleType; private String runningMainClass; private boolean runningStackDeobfuscated; private boolean runningIndicator; @@ -114,6 +117,10 @@ public class ProjectDevServerManager { this.preservedClasses.addAll(preservedClasses); } + public void setJsModuleType(JSModuleType jsModuleType) { + this.jsModuleType = jsModuleType; + } + public void setTargetFileName(String targetFileName) { this.targetFileName = targetFileName; } @@ -561,6 +568,12 @@ public class ProjectDevServerManager { runningPreservedClasses.clear(); runningPreservedClasses.addAll(preservedClasses); + if (jsModuleType != null) { + command.add("--js-module-type"); + command.add(jsModuleType.name().toLowerCase().replace('_', '-')); + } + runningJsModuleType = jsModuleType; + command.add("--"); command.add(mainClass); runningMainClass = mainClass; @@ -575,6 +588,7 @@ public class ProjectDevServerManager { && Objects.equals(targetFilePath, runningTargetFilePath) && Objects.equals(properties, runningProperties) && Objects.equals(preservedClasses, runningPreservedClasses) + && Objects.equals(jsModuleType, runningJsModuleType) && Objects.equals(mainClass, runningMainClass) && stackDeobfuscated == runningStackDeobfuscated && indicator == runningIndicator