gradle: support JS module type in dev server task

This commit is contained in:
Alexey Andreev 2024-04-12 21:15:12 +02:00
parent 908047fb1f
commit 271545a3ef
6 changed files with 58 additions and 0 deletions

View File

@ -30,6 +30,7 @@ import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options; import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException; import org.apache.commons.cli.ParseException;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.teavm.backend.javascript.JSModuleType;
import org.teavm.common.json.JsonParser; import org.teavm.common.json.JsonParser;
import org.teavm.devserver.DevServer; import org.teavm.devserver.DevServer;
import org.teavm.tooling.ConsoleTeaVMToolLog; import org.teavm.tooling.ConsoleTeaVMToolLog;
@ -80,6 +81,12 @@ public final class TeaVMDevServerRunner {
.hasArgs() .hasArgs()
.longOpt("property") .longOpt("property")
.build()); .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() options.addOption(Option.builder()
.argName("number") .argName("number")
.hasArg() .hasArg()
@ -182,6 +189,18 @@ public final class TeaVMDevServerRunner {
if (commandLine.hasOption("preserved-classes")) { if (commandLine.hasOption("preserved-classes")) {
devServer.getPreservedClasses().addAll(List.of(commandLine.getOptionValues("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")) { if (commandLine.hasOption("proxy-url")) {
devServer.setProxyUrl(commandLine.getOptionValue("proxy-url")); devServer.setProxyUrl(commandLine.getOptionValue("proxy-url"));

View File

@ -67,6 +67,7 @@ import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.client.io.UpgradeListener; import org.eclipse.jetty.websocket.client.io.UpgradeListener;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
import org.teavm.backend.javascript.JSModuleType;
import org.teavm.backend.javascript.JavaScriptTarget; import org.teavm.backend.javascript.JavaScriptTarget;
import org.teavm.cache.InMemoryMethodNodeCache; import org.teavm.cache.InMemoryMethodNodeCache;
import org.teavm.cache.InMemoryProgramCache; import org.teavm.cache.InMemoryProgramCache;
@ -122,6 +123,7 @@ public class CodeServlet extends HttpServlet {
private String proxyBaseUrl; private String proxyBaseUrl;
private Map<String, String> properties = new LinkedHashMap<>(); private Map<String, String> properties = new LinkedHashMap<>();
private List<String> preservedClasses = new ArrayList<>(); private List<String> preservedClasses = new ArrayList<>();
private JSModuleType jsModuleType;
private Map<String, Supplier<InputStream>> sourceFileCache = new HashMap<>(); private Map<String, Supplier<InputStream>> sourceFileCache = new HashMap<>();
@ -223,6 +225,10 @@ public class CodeServlet extends HttpServlet {
return properties; return properties;
} }
public void setJsModuleType(JSModuleType jsModuleType) {
this.jsModuleType = jsModuleType;
}
public void setLogBuildErrors(boolean logBuildErrors) { public void setLogBuildErrors(boolean logBuildErrors) {
this.logBuildErrors = logBuildErrors; this.logBuildErrors = logBuildErrors;
} }
@ -882,6 +888,9 @@ public class CodeServlet extends HttpServlet {
jsTarget.setObfuscated(false); jsTarget.setObfuscated(false);
jsTarget.setAstCache(astCache); jsTarget.setAstCache(astCache);
jsTarget.setDebugEmitter(debugInformationBuilder); jsTarget.setDebugEmitter(debugInformationBuilder);
if (jsModuleType != null) {
jsTarget.setModuleType(jsModuleType);
}
jsTarget.setStrict(true); jsTarget.setStrict(true);
vm.setOptimizationLevel(TeaVMOptimizationLevel.SIMPLE); vm.setOptimizationLevel(TeaVMOptimizationLevel.SIMPLE);
vm.setCacheStatus(classSource); vm.setCacheStatus(classSource);

View File

@ -23,6 +23,7 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import org.teavm.backend.javascript.JSModuleType;
import org.teavm.tooling.TeaVMToolLog; import org.teavm.tooling.TeaVMToolLog;
public class DevServer { public class DevServer {
@ -40,6 +41,7 @@ public class DevServer {
private List<DevServerListener> listeners = new ArrayList<>(); private List<DevServerListener> listeners = new ArrayList<>();
private Map<String, String> properties = new LinkedHashMap<>(); private Map<String, String> properties = new LinkedHashMap<>();
private List<String> preservedClasses = new ArrayList<>(); private List<String> preservedClasses = new ArrayList<>();
private JSModuleType jsModuleType;
private boolean compileOnStartup; private boolean compileOnStartup;
private boolean logBuildErrors = true; private boolean logBuildErrors = true;
@ -123,6 +125,10 @@ public class DevServer {
return properties; return properties;
} }
public void setJsModuleType(JSModuleType jsModuleType) {
this.jsModuleType = jsModuleType;
}
public void invalidateCache() { public void invalidateCache() {
servlet.invalidateCache(); servlet.invalidateCache();
} }
@ -170,6 +176,7 @@ public class DevServer {
servlet.setLogBuildErrors(logBuildErrors); servlet.setLogBuildErrors(logBuildErrors);
servlet.getProperties().putAll(properties); servlet.getProperties().putAll(properties);
servlet.getPreservedClasses().addAll(preservedClasses); servlet.getPreservedClasses().addAll(preservedClasses);
servlet.setJsModuleType(jsModuleType);
for (DevServerListener listener : listeners) { for (DevServerListener listener : listeners) {
servlet.addListener(listener); servlet.addListener(listener);
} }

View File

@ -147,6 +147,7 @@ public class TeaVMPlugin implements Plugin<Project> {
task.getMainClass().convention(js.getMainClass()); task.getMainClass().convention(js.getMainClass());
task.getClasspath().from(task.getProject().getConfigurations().getByName(CLASSPATH_CONFIGURATION_NAME)); task.getClasspath().from(task.getProject().getConfigurations().getByName(CLASSPATH_CONFIGURATION_NAME));
task.getPreservedClasses().addAll(js.getPreservedClasses()); task.getPreservedClasses().addAll(js.getPreservedClasses());
task.getJsModuleType().convention(js.getModuleType());
task.getProcessMemory().convention(js.getDevServer().getProcessMemory()); task.getProcessMemory().convention(js.getDevServer().getProcessMemory());
task.getProperties().putAll(js.getProperties()); task.getProperties().putAll(js.getProperties());
task.getServerClasspath().from(configuration); task.getServerClasspath().from(configuration);

View File

@ -29,6 +29,7 @@ import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional; import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
import org.gradle.internal.logging.progress.ProgressLoggerFactory; import org.gradle.internal.logging.progress.ProgressLoggerFactory;
import org.teavm.gradle.api.JSModuleType;
public abstract class JavaScriptDevServerTask extends DefaultTask { public abstract class JavaScriptDevServerTask extends DefaultTask {
@Classpath @Classpath
@ -50,6 +51,10 @@ public abstract class JavaScriptDevServerTask extends DefaultTask {
@Optional @Optional
public abstract ListProperty<String> getPreservedClasses(); public abstract ListProperty<String> getPreservedClasses();
@Input
@Optional
public abstract Property<JSModuleType> getJsModuleType();
@Input @Input
public abstract Property<String> getMainClass(); public abstract Property<String> getMainClass();
@ -111,6 +116,9 @@ public abstract class JavaScriptDevServerTask extends DefaultTask {
pm.setProperties(getProperties().get()); pm.setProperties(getProperties().get());
pm.setPreservedClasses(getPreservedClasses().get()); pm.setPreservedClasses(getPreservedClasses().get());
if (getJsModuleType().isPresent()) {
pm.setJsModuleType(getJsModuleType().get());
}
pm.setServerClasspath(getServerClasspath().getFiles()); pm.setServerClasspath(getServerClasspath().getFiles());
pm.setMainClass(getMainClass().get()); pm.setMainClass(getMainClass().get());

View File

@ -43,6 +43,7 @@ import org.teavm.common.json.JsonArrayValue;
import org.teavm.common.json.JsonObjectValue; import org.teavm.common.json.JsonObjectValue;
import org.teavm.common.json.JsonParser; import org.teavm.common.json.JsonParser;
import org.teavm.common.json.JsonValue; import org.teavm.common.json.JsonValue;
import org.teavm.gradle.api.JSModuleType;
public class ProjectDevServerManager { public class ProjectDevServerManager {
private Set<File> serverClasspath = new LinkedHashSet<>(); private Set<File> serverClasspath = new LinkedHashSet<>();
@ -51,6 +52,7 @@ public class ProjectDevServerManager {
private String targetFilePath; private String targetFilePath;
private Map<String, String> properties = new LinkedHashMap<>(); private Map<String, String> properties = new LinkedHashMap<>();
private Set<String> preservedClasses = new LinkedHashSet<>(); private Set<String> preservedClasses = new LinkedHashSet<>();
private JSModuleType jsModuleType;
private String mainClass; private String mainClass;
private boolean stackDeobfuscated; private boolean stackDeobfuscated;
private boolean indicator; private boolean indicator;
@ -79,6 +81,7 @@ public class ProjectDevServerManager {
private String runningTargetFilePath; private String runningTargetFilePath;
private Map<String, String> runningProperties = new HashMap<>(); private Map<String, String> runningProperties = new HashMap<>();
private Set<String> runningPreservedClasses = new HashSet<>(); private Set<String> runningPreservedClasses = new HashSet<>();
private JSModuleType runningJsModuleType;
private String runningMainClass; private String runningMainClass;
private boolean runningStackDeobfuscated; private boolean runningStackDeobfuscated;
private boolean runningIndicator; private boolean runningIndicator;
@ -114,6 +117,10 @@ public class ProjectDevServerManager {
this.preservedClasses.addAll(preservedClasses); this.preservedClasses.addAll(preservedClasses);
} }
public void setJsModuleType(JSModuleType jsModuleType) {
this.jsModuleType = jsModuleType;
}
public void setTargetFileName(String targetFileName) { public void setTargetFileName(String targetFileName) {
this.targetFileName = targetFileName; this.targetFileName = targetFileName;
} }
@ -561,6 +568,12 @@ public class ProjectDevServerManager {
runningPreservedClasses.clear(); runningPreservedClasses.clear();
runningPreservedClasses.addAll(preservedClasses); runningPreservedClasses.addAll(preservedClasses);
if (jsModuleType != null) {
command.add("--js-module-type");
command.add(jsModuleType.name().toLowerCase().replace('_', '-'));
}
runningJsModuleType = jsModuleType;
command.add("--"); command.add("--");
command.add(mainClass); command.add(mainClass);
runningMainClass = mainClass; runningMainClass = mainClass;
@ -575,6 +588,7 @@ public class ProjectDevServerManager {
&& Objects.equals(targetFilePath, runningTargetFilePath) && Objects.equals(targetFilePath, runningTargetFilePath)
&& Objects.equals(properties, runningProperties) && Objects.equals(properties, runningProperties)
&& Objects.equals(preservedClasses, runningPreservedClasses) && Objects.equals(preservedClasses, runningPreservedClasses)
&& Objects.equals(jsModuleType, runningJsModuleType)
&& Objects.equals(mainClass, runningMainClass) && Objects.equals(mainClass, runningMainClass)
&& stackDeobfuscated == runningStackDeobfuscated && stackDeobfuscated == runningStackDeobfuscated
&& indicator == runningIndicator && indicator == runningIndicator