mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 07:19:22 +00:00
es2015 modules
This commit is contained in:
parent
3b106e86e2
commit
828ee21072
@ -401,6 +401,14 @@ public class JSweetContext extends Context {
|
||||
* A flag to tell if the transpiler is in module mode or not.
|
||||
*/
|
||||
public boolean useModules = false;
|
||||
|
||||
/**
|
||||
* A flag to tell if the transpiler should transpiler modules to old fashioned require instructions rather than the ES2015 flavored syntax.
|
||||
* import foo = require("foo");
|
||||
* instead of
|
||||
* import * as foo from 'foo';
|
||||
*/
|
||||
public boolean useRequireForModules = true;
|
||||
|
||||
/**
|
||||
* The source files that are being transpiled.
|
||||
|
||||
@ -59,6 +59,10 @@ public enum JSweetProblem {
|
||||
* Raised when Node is not found.
|
||||
*/
|
||||
NODE_CANNOT_START(Severity.ERROR),
|
||||
/**
|
||||
* Raised when Node version is obsolete and should be updated
|
||||
*/
|
||||
NODE_OBSOLETE_VERSION(Severity.ERROR),
|
||||
/**
|
||||
* Raised when the program tries to access a forbidden Java type.
|
||||
*
|
||||
@ -327,6 +331,8 @@ public enum JSweetProblem {
|
||||
return String.format("%s", params);
|
||||
case NODE_CANNOT_START:
|
||||
return String.format("cannot find Node.js: install first and make sure that the 'node' command is in your execution path", params);
|
||||
case NODE_OBSOLETE_VERSION:
|
||||
return String.format("Node.js is obsolete: %s < %s (minimum)", params);
|
||||
case TSC_CANNOT_START:
|
||||
return String.format("cannot find TypeScript compiler: install first and make sure that the 'tsc' command is in your execution path", params);
|
||||
case JDK_TYPE:
|
||||
|
||||
@ -320,7 +320,16 @@ public class JSweetTranspiler<C extends JSweetContext> implements JSweetOptions
|
||||
File initFile = new File(workingDir, ".node-init");
|
||||
boolean initialized = initFile.exists();
|
||||
if (!initialized) {
|
||||
ProcessUtil.runCommand(ProcessUtil.NODE_COMMAND, null, () -> {
|
||||
ProcessUtil.runCommand(ProcessUtil.NODE_COMMAND, line -> {
|
||||
logger.info("node version: " + line);
|
||||
|
||||
if (line.compareTo(ProcessUtil.NODE_MINIMUM_VERSION) < 0) {
|
||||
transpilationHandler.report(JSweetProblem.NODE_OBSOLETE_VERSION, null,
|
||||
JSweetProblem.NODE_OBSOLETE_VERSION.getMessage(line, ProcessUtil.NODE_MINIMUM_VERSION));
|
||||
throw new RuntimeException("node.js version is obsolete, minimum version: " + ProcessUtil.NODE_MINIMUM_VERSION);
|
||||
}
|
||||
|
||||
}, () -> {
|
||||
transpilationHandler.report(JSweetProblem.NODE_CANNOT_START, null,
|
||||
JSweetProblem.NODE_CANNOT_START.getMessage());
|
||||
throw new RuntimeException("cannot find node.js");
|
||||
@ -524,7 +533,7 @@ public class JSweetTranspiler<C extends JSweetContext> implements JSweetOptions
|
||||
log.dumpOnError = false;
|
||||
log.emitWarnings = false;
|
||||
|
||||
logger.info("parsingPOUET: " + fileObjects);
|
||||
logger.info("parsing: " + fileObjects);
|
||||
List<JCCompilationUnit> compilationUnits = compiler.enterTrees(compiler.parseFiles(fileObjects));
|
||||
MainMethodFinder mainMethodFinder = new MainMethodFinder();
|
||||
try {
|
||||
@ -694,7 +703,8 @@ public class JSweetTranspiler<C extends JSweetContext> implements JSweetOptions
|
||||
return null;
|
||||
}
|
||||
context.useModules = isUsingModules();
|
||||
|
||||
context.useRequireForModules = moduleKind != ModuleKind.es2015;
|
||||
|
||||
if (context.useModules && bundle) {
|
||||
transpilationHandler.report(JSweetProblem.BUNDLE_WITH_MODULE, null,
|
||||
JSweetProblem.BUNDLE_WITH_MODULE.getMessage());
|
||||
@ -1143,7 +1153,7 @@ public class JSweetTranspiler<C extends JSweetContext> implements JSweetOptions
|
||||
}
|
||||
|
||||
if (isUsingModules()) {
|
||||
if (ecmaTargetVersion.higherThan(EcmaScriptComplianceLevel.ES5)) {
|
||||
if (ecmaTargetVersion.higherThan(EcmaScriptComplianceLevel.ES5) && moduleKind != ModuleKind.es2015) {
|
||||
logger.warn("cannot use old fashionned modules with ES>5 target");
|
||||
} else {
|
||||
args.add("--module");
|
||||
|
||||
@ -49,5 +49,10 @@ public enum ModuleKind {
|
||||
* The generated code uses the <code>umd</code> (EcmaScript 6 Universal
|
||||
* Module Definition) module kind.
|
||||
*/
|
||||
umd
|
||||
umd,
|
||||
|
||||
/**
|
||||
* The generated code uses the <code>ES6</code> module import flavour.
|
||||
*/
|
||||
es2015;
|
||||
}
|
||||
|
||||
@ -286,8 +286,14 @@ public class Java2TypeScriptTranslator<C extends JSweetContext> extends Abstract
|
||||
// import, which can be named after something like
|
||||
// qualName.replace(".", "_")... this would work in the general
|
||||
// case...
|
||||
if (require || GLOBALS_CLASS_NAME.equals(targetName)) {
|
||||
print("import " + targetName + " = require(\"" + moduleName + "\"); ").println();
|
||||
|
||||
boolean fullImport = require || GLOBALS_CLASS_NAME.equals(targetName);
|
||||
if (fullImport) {
|
||||
if (context.useRequireForModules) {
|
||||
print("import " + targetName + " = require(\"" + moduleName + "\"); ").println();
|
||||
} else {
|
||||
print("import * as " + targetName + " from '" + moduleName + "'; ").println();
|
||||
}
|
||||
} else {
|
||||
print("import { " + targetName + " } from '" + moduleName + "'; ").println();
|
||||
}
|
||||
@ -2325,7 +2331,7 @@ public class Java2TypeScriptTranslator<C extends JSweetContext> extends Abstract
|
||||
String adaptedQualId = getAdapter().needsImport(importDecl, qualId);
|
||||
if (adaptedQualId != null && adaptedQualId.contains(".")) {
|
||||
if (importDecl.isStatic() && !qualId.contains("." + JSweetConfig.GLOBALS_CLASS_NAME + ".")
|
||||
&& !qualId.contains("." + JSweetConfig.STRING_TYPES_INTERFACE_NAME + ".")) {
|
||||
&& !qualId.contains("." + JSweetConfig.STRING_TYPES_INTERFACE_NAME + ".")) {
|
||||
if (!context.bundleMode) {
|
||||
print(VAR_DECL_KEYWORD + " ").print(qualId.substring(qualId.lastIndexOf('.') + 1)).print(": any = ")
|
||||
.print(qualId).print(";");
|
||||
|
||||
@ -37,6 +37,8 @@ import org.apache.log4j.Logger;
|
||||
public class ProcessUtil {
|
||||
private final static Logger logger = Logger.getLogger(ProcessUtil.class);
|
||||
|
||||
public static final String NODE_MINIMUM_VERSION = "v7.5.0";
|
||||
|
||||
private static boolean initialized = false;
|
||||
|
||||
/**
|
||||
|
||||
@ -33,7 +33,6 @@ import org.jsweet.transpiler.JSweetFactory;
|
||||
import org.jsweet.transpiler.JSweetTranspiler;
|
||||
import org.jsweet.transpiler.ModuleKind;
|
||||
import org.jsweet.transpiler.SourceFile;
|
||||
import org.jsweet.transpiler.extensions.RemoveJavaDependenciesFactory;
|
||||
import org.jsweet.transpiler.util.EvaluationResult;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
@ -51,7 +51,7 @@ import source.require.b.ClassImport;
|
||||
import source.require.b.ClassImportImplicitRequire;
|
||||
import source.require.b.GlobalsImport;
|
||||
|
||||
public class RequireTests extends AbstractTest {
|
||||
public class ModuleTests extends AbstractTest {
|
||||
|
||||
// TODO: reactivate when candies are back
|
||||
@Ignore
|
||||
Loading…
x
Reference in New Issue
Block a user