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.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"));

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.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<String, String> properties = new LinkedHashMap<>();
private List<String> preservedClasses = new ArrayList<>();
private JSModuleType jsModuleType;
private Map<String, Supplier<InputStream>> 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);

View File

@ -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<DevServerListener> listeners = new ArrayList<>();
private Map<String, String> properties = new LinkedHashMap<>();
private List<String> 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);
}

View File

@ -147,6 +147,7 @@ public class TeaVMPlugin implements Plugin<Project> {
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);

View File

@ -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<String> getPreservedClasses();
@Input
@Optional
public abstract Property<JSModuleType> getJsModuleType();
@Input
public abstract Property<String> 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());

View File

@ -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<File> serverClasspath = new LinkedHashSet<>();
@ -51,6 +52,7 @@ public class ProjectDevServerManager {
private String targetFilePath;
private Map<String, String> properties = new LinkedHashMap<>();
private Set<String> 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<String, String> runningProperties = new HashMap<>();
private Set<String> 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