KT-3008 Implement AMD, CommonJS and UMD wrappers. Compile stdlib and builtins modules into separate JS files and wrap them in UMD.
This commit is contained in:
@@ -746,6 +746,10 @@ public final class DebugJsProtoBuf {
|
|||||||
* <code>COMMON_JS = 3;</code>
|
* <code>COMMON_JS = 3;</code>
|
||||||
*/
|
*/
|
||||||
COMMON_JS(2, 3),
|
COMMON_JS(2, 3),
|
||||||
|
/**
|
||||||
|
* <code>UMD = 4;</code>
|
||||||
|
*/
|
||||||
|
UMD(3, 4),
|
||||||
;
|
;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -760,6 +764,10 @@ public final class DebugJsProtoBuf {
|
|||||||
* <code>COMMON_JS = 3;</code>
|
* <code>COMMON_JS = 3;</code>
|
||||||
*/
|
*/
|
||||||
public static final int COMMON_JS_VALUE = 3;
|
public static final int COMMON_JS_VALUE = 3;
|
||||||
|
/**
|
||||||
|
* <code>UMD = 4;</code>
|
||||||
|
*/
|
||||||
|
public static final int UMD_VALUE = 4;
|
||||||
|
|
||||||
|
|
||||||
public final int getNumber() { return value; }
|
public final int getNumber() { return value; }
|
||||||
@@ -769,6 +777,7 @@ public final class DebugJsProtoBuf {
|
|||||||
case 1: return PLAIN;
|
case 1: return PLAIN;
|
||||||
case 2: return AMD;
|
case 2: return AMD;
|
||||||
case 3: return COMMON_JS;
|
case 3: return COMMON_JS;
|
||||||
|
case 4: return UMD;
|
||||||
default: return null;
|
default: return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2301,42 +2310,43 @@ public final class DebugJsProtoBuf {
|
|||||||
"rg.jetbrains.kotlin.serialization.js\0320co" +
|
"rg.jetbrains.kotlin.serialization.js\0320co" +
|
||||||
"re/deserialization/src/descriptors.debug" +
|
"re/deserialization/src/descriptors.debug" +
|
||||||
".proto\"!\n\007Classes\022\026\n\nclass_name\030\001 \003(\005B\002\020" +
|
".proto\"!\n\007Classes\022\026\n\nclass_name\030\001 \003(\005B\002\020" +
|
||||||
"\001\"\214\002\n\007Library\022G\n\005entry\030\001 \003(\01328.org.jetbr" +
|
"\001\"\225\002\n\007Library\022G\n\005entry\030\001 \003(\01328.org.jetbr" +
|
||||||
"ains.kotlin.serialization.js.Library.Fil" +
|
"ains.kotlin.serialization.js.Library.Fil" +
|
||||||
"eEntry\022H\n\004kind\030\002 \001(\01623.org.jetbrains.kot" +
|
"eEntry\022H\n\004kind\030\002 \001(\01623.org.jetbrains.kot" +
|
||||||
"lin.serialization.js.Library.Kind:\005PLAIN" +
|
"lin.serialization.js.Library.Kind:\005PLAIN" +
|
||||||
"\022\027\n\017importedModules\030\003 \003(\t\032*\n\tFileEntry\022\014" +
|
"\022\027\n\017importedModules\030\003 \003(\t\032*\n\tFileEntry\022\014" +
|
||||||
"\n\004path\030\001 \002(\t\022\017\n\007content\030\002 \002(\014\")\n\004Kind\022\t\n",
|
"\n\004path\030\001 \002(\t\022\017\n\007content\030\002 \002(\014\"2\n\004Kind\022\t\n",
|
||||||
"\005PLAIN\020\001\022\007\n\003AMD\020\002\022\r\n\tCOMMON_JS\020\003:t\n\020clas" +
|
"\005PLAIN\020\001\022\007\n\003AMD\020\002\022\r\n\tCOMMON_JS\020\003\022\007\n\003UMD\020" +
|
||||||
"s_annotation\022).org.jetbrains.kotlin.seri" +
|
"\004:t\n\020class_annotation\022).org.jetbrains.ko" +
|
||||||
"alization.Class\030\202\001 \003(\0132..org.jetbrains.k" +
|
"tlin.serialization.Class\030\202\001 \003(\0132..org.je" +
|
||||||
"otlin.serialization.Annotation:\200\001\n\026const" +
|
"tbrains.kotlin.serialization.Annotation:" +
|
||||||
"ructor_annotation\022/.org.jetbrains.kotlin" +
|
"\200\001\n\026constructor_annotation\022/.org.jetbrai" +
|
||||||
".serialization.Constructor\030\202\001 \003(\0132..org." +
|
"ns.kotlin.serialization.Constructor\030\202\001 \003" +
|
||||||
"jetbrains.kotlin.serialization.Annotatio" +
|
"(\0132..org.jetbrains.kotlin.serialization." +
|
||||||
"n:z\n\023function_annotation\022,.org.jetbrains" +
|
"Annotation:z\n\023function_annotation\022,.org." +
|
||||||
".kotlin.serialization.Function\030\202\001 \003(\0132.." +
|
"jetbrains.kotlin.serialization.Function\030" +
|
||||||
"org.jetbrains.kotlin.serialization.Annot",
|
"\202\001 \003(\0132..org.jetbrains.kotlin.serializat",
|
||||||
"ation:z\n\023property_annotation\022,.org.jetbr" +
|
"ion.Annotation:z\n\023property_annotation\022,." +
|
||||||
"ains.kotlin.serialization.Property\030\202\001 \003(" +
|
"org.jetbrains.kotlin.serialization.Prope" +
|
||||||
"\0132..org.jetbrains.kotlin.serialization.A" +
|
"rty\030\202\001 \003(\0132..org.jetbrains.kotlin.serial" +
|
||||||
"nnotation:\210\001\n\022compile_time_value\022,.org.j" +
|
"ization.Annotation:\210\001\n\022compile_time_valu" +
|
||||||
"etbrains.kotlin.serialization.Property\030\203" +
|
"e\022,.org.jetbrains.kotlin.serialization.P" +
|
||||||
"\001 \001(\0132=.org.jetbrains.kotlin.serializati" +
|
"roperty\030\203\001 \001(\0132=.org.jetbrains.kotlin.se" +
|
||||||
"on.Annotation.Argument.Value:}\n\025enum_ent" +
|
"rialization.Annotation.Argument.Value:}\n" +
|
||||||
"ry_annotation\022-.org.jetbrains.kotlin.ser" +
|
"\025enum_entry_annotation\022-.org.jetbrains.k" +
|
||||||
"ialization.EnumEntry\030\202\001 \003(\0132..org.jetbra" +
|
"otlin.serialization.EnumEntry\030\202\001 \003(\0132..o" +
|
||||||
"ins.kotlin.serialization.Annotation:\201\001\n\024",
|
"rg.jetbrains.kotlin.serialization.Annota",
|
||||||
"parameter_annotation\0222.org.jetbrains.kot" +
|
"tion:\201\001\n\024parameter_annotation\0222.org.jetb" +
|
||||||
"lin.serialization.ValueParameter\030\202\001 \003(\0132" +
|
"rains.kotlin.serialization.ValueParamete" +
|
||||||
"..org.jetbrains.kotlin.serialization.Ann" +
|
"r\030\202\001 \003(\0132..org.jetbrains.kotlin.serializ" +
|
||||||
"otation:r\n\017type_annotation\022(.org.jetbrai" +
|
"ation.Annotation:r\n\017type_annotation\022(.or" +
|
||||||
"ns.kotlin.serialization.Type\030\202\001 \003(\0132..or" +
|
"g.jetbrains.kotlin.serialization.Type\030\202\001" +
|
||||||
"g.jetbrains.kotlin.serialization.Annotat" +
|
" \003(\0132..org.jetbrains.kotlin.serializatio" +
|
||||||
"ion:\205\001\n\031type_parameter_annotation\0221.org." +
|
"n.Annotation:\205\001\n\031type_parameter_annotati" +
|
||||||
"jetbrains.kotlin.serialization.TypeParam" +
|
"on\0221.org.jetbrains.kotlin.serialization." +
|
||||||
"eter\030\202\001 \003(\0132..org.jetbrains.kotlin.seria" +
|
"TypeParameter\030\202\001 \003(\0132..org.jetbrains.kot" +
|
||||||
"lization.AnnotationB\021B\017DebugJsProtoBuf"
|
"lin.serialization.AnnotationB\021B\017DebugJsP",
|
||||||
|
"rotoBuf"
|
||||||
};
|
};
|
||||||
org.jetbrains.kotlin.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
|
org.jetbrains.kotlin.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
|
||||||
new org.jetbrains.kotlin.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() {
|
new org.jetbrains.kotlin.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() {
|
||||||
|
|||||||
@@ -319,6 +319,7 @@
|
|||||||
<arg value="-meta-info"/>
|
<arg value="-meta-info"/>
|
||||||
<arg value="-kjsm"/>
|
<arg value="-kjsm"/>
|
||||||
<arg line="-main noCall"/>
|
<arg line="-main noCall"/>
|
||||||
|
<arg line="-module-kind umd"/>
|
||||||
</java>
|
</java>
|
||||||
</sequential>
|
</sequential>
|
||||||
</macrodef>
|
</macrodef>
|
||||||
@@ -358,7 +359,8 @@
|
|||||||
prettyprint="true"
|
prettyprint="true"
|
||||||
languagein="ECMASCRIPT5_STRICT"
|
languagein="ECMASCRIPT5_STRICT"
|
||||||
warning="${warningLevel}"
|
warning="${warningLevel}"
|
||||||
output="${js.stdlib.output.dir}/kotlin.js">
|
output="${js.stdlib.output.dir}/kotlin.js"
|
||||||
|
outputWrapperFile="${stdlib.js.dir}/closure-wrapper.txt">
|
||||||
|
|
||||||
<sources dir="${stdlib.js.dir}">
|
<sources dir="${stdlib.js.dir}">
|
||||||
<file name="kotlin_lib_ecma5.js"/>
|
<file name="kotlin_lib_ecma5.js"/>
|
||||||
|
|||||||
+1
-1
@@ -49,7 +49,7 @@ public class K2JSCompilerArguments extends CommonCompilerArguments {
|
|||||||
public String target;
|
public String target;
|
||||||
|
|
||||||
@Argument(value = "module-kind", description = "Kind of a module generated by compiler. Supported values are: plain (by default), " +
|
@Argument(value = "module-kind", description = "Kind of a module generated by compiler. Supported values are: plain (by default), " +
|
||||||
"amd, commonjs.")
|
"amd, commonjs, umd.")
|
||||||
public String moduleKind;
|
public String moduleKind;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|||||||
@@ -287,6 +287,9 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
|
|||||||
else if (moduleKindName.equals("commonjs")) {
|
else if (moduleKindName.equals("commonjs")) {
|
||||||
moduleKind = ModuleKind.COMMON_JS;
|
moduleKind = ModuleKind.COMMON_JS;
|
||||||
}
|
}
|
||||||
|
else if (moduleKindName.equals("umd")) {
|
||||||
|
moduleKind = ModuleKind.UMD;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
messageCollector.report(CompilerMessageSeverity.ERROR, "Unknown module kind: " + moduleKindName + ". " +
|
messageCollector.report(CompilerMessageSeverity.ERROR, "Unknown module kind: " + moduleKindName + ". " +
|
||||||
"valid values are: plain, amd, commonjs",
|
"valid values are: plain, amd, commonjs",
|
||||||
|
|||||||
@@ -66,9 +66,10 @@ message Library {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum Kind {
|
enum Kind {
|
||||||
PLAIN = 1;
|
PLAIN = 1;
|
||||||
AMD = 2;
|
AMD = 2;
|
||||||
COMMON_JS = 3;
|
COMMON_JS = 3;
|
||||||
|
UMD = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
repeated FileEntry entry = 1;
|
repeated FileEntry entry = 1;
|
||||||
|
|||||||
@@ -650,6 +650,10 @@ public final class JsProtoBuf {
|
|||||||
* <code>COMMON_JS = 3;</code>
|
* <code>COMMON_JS = 3;</code>
|
||||||
*/
|
*/
|
||||||
COMMON_JS(2, 3),
|
COMMON_JS(2, 3),
|
||||||
|
/**
|
||||||
|
* <code>UMD = 4;</code>
|
||||||
|
*/
|
||||||
|
UMD(3, 4),
|
||||||
;
|
;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -664,6 +668,10 @@ public final class JsProtoBuf {
|
|||||||
* <code>COMMON_JS = 3;</code>
|
* <code>COMMON_JS = 3;</code>
|
||||||
*/
|
*/
|
||||||
public static final int COMMON_JS_VALUE = 3;
|
public static final int COMMON_JS_VALUE = 3;
|
||||||
|
/**
|
||||||
|
* <code>UMD = 4;</code>
|
||||||
|
*/
|
||||||
|
public static final int UMD_VALUE = 4;
|
||||||
|
|
||||||
|
|
||||||
public final int getNumber() { return value; }
|
public final int getNumber() { return value; }
|
||||||
@@ -673,6 +681,7 @@ public final class JsProtoBuf {
|
|||||||
case 1: return PLAIN;
|
case 1: return PLAIN;
|
||||||
case 2: return AMD;
|
case 2: return AMD;
|
||||||
case 3: return COMMON_JS;
|
case 3: return COMMON_JS;
|
||||||
|
case 4: return UMD;
|
||||||
default: return null;
|
default: return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
@@ -94,6 +94,7 @@ object KotlinJavascriptSerializationUtil {
|
|||||||
ModuleKind.PLAIN -> JsProtoBuf.Library.Kind.PLAIN
|
ModuleKind.PLAIN -> JsProtoBuf.Library.Kind.PLAIN
|
||||||
ModuleKind.AMD -> JsProtoBuf.Library.Kind.AMD
|
ModuleKind.AMD -> JsProtoBuf.Library.Kind.AMD
|
||||||
ModuleKind.COMMON_JS -> JsProtoBuf.Library.Kind.COMMON_JS
|
ModuleKind.COMMON_JS -> JsProtoBuf.Library.Kind.COMMON_JS
|
||||||
|
ModuleKind.UMD -> JsProtoBuf.Library.Kind.UMD
|
||||||
}
|
}
|
||||||
|
|
||||||
importedModules.forEach { contentBuilder.addImportedModules(it) }
|
importedModules.forEach { contentBuilder.addImportedModules(it) }
|
||||||
@@ -249,6 +250,7 @@ private fun ByteArray.toContentMap(): JsModuleProto {
|
|||||||
null, JsProtoBuf.Library.Kind.PLAIN -> ModuleKind.PLAIN
|
null, JsProtoBuf.Library.Kind.PLAIN -> ModuleKind.PLAIN
|
||||||
JsProtoBuf.Library.Kind.AMD -> ModuleKind.AMD
|
JsProtoBuf.Library.Kind.AMD -> ModuleKind.AMD
|
||||||
JsProtoBuf.Library.Kind.COMMON_JS -> ModuleKind.COMMON_JS
|
JsProtoBuf.Library.Kind.COMMON_JS -> ModuleKind.COMMON_JS
|
||||||
|
JsProtoBuf.Library.Kind.UMD -> ModuleKind.UMD
|
||||||
},
|
},
|
||||||
imported = content.importedModulesList
|
imported = content.importedModulesList
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -21,5 +21,6 @@ package org.jetbrains.kotlin.serialization.js;
|
|||||||
public enum ModuleKind {
|
public enum ModuleKind {
|
||||||
PLAIN,
|
PLAIN,
|
||||||
AMD,
|
AMD,
|
||||||
COMMON_JS
|
COMMON_JS,
|
||||||
|
UMD
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,6 @@ import org.jetbrains.kotlin.js.translate.test.JSTestGenerator;
|
|||||||
import org.jetbrains.kotlin.js.translate.test.JSTester;
|
import org.jetbrains.kotlin.js.translate.test.JSTester;
|
||||||
import org.jetbrains.kotlin.js.translate.test.QUnitTester;
|
import org.jetbrains.kotlin.js.translate.test.QUnitTester;
|
||||||
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils;
|
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils;
|
||||||
import org.jetbrains.kotlin.js.translate.utils.JsDescriptorUtils;
|
|
||||||
import org.jetbrains.kotlin.js.translate.utils.mutator.AssignToExpressionMutator;
|
import org.jetbrains.kotlin.js.translate.utils.mutator.AssignToExpressionMutator;
|
||||||
import org.jetbrains.kotlin.psi.KtDeclarationWithBody;
|
import org.jetbrains.kotlin.psi.KtDeclarationWithBody;
|
||||||
import org.jetbrains.kotlin.psi.KtExpression;
|
import org.jetbrains.kotlin.psi.KtExpression;
|
||||||
@@ -50,6 +49,7 @@ import org.jetbrains.kotlin.psi.KtFile;
|
|||||||
import org.jetbrains.kotlin.psi.KtNamedFunction;
|
import org.jetbrains.kotlin.psi.KtNamedFunction;
|
||||||
import org.jetbrains.kotlin.resolve.BindingTrace;
|
import org.jetbrains.kotlin.resolve.BindingTrace;
|
||||||
import org.jetbrains.kotlin.resolve.bindingContextUtil.BindingContextUtilsKt;
|
import org.jetbrains.kotlin.resolve.bindingContextUtil.BindingContextUtilsKt;
|
||||||
|
import org.jetbrains.kotlin.serialization.js.ModuleKind;
|
||||||
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
|
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -188,6 +188,16 @@ public final class Translation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class ImportedModule {
|
||||||
|
final String id;
|
||||||
|
final JsName name;
|
||||||
|
|
||||||
|
public ImportedModule(String id, JsName name) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static TranslationContext doGenerateAst(
|
private static TranslationContext doGenerateAst(
|
||||||
@NotNull BindingTrace bindingTrace,
|
@NotNull BindingTrace bindingTrace,
|
||||||
@@ -211,27 +221,18 @@ public final class Translation {
|
|||||||
|
|
||||||
mayBeGenerateTests(files, config, rootBlock, context);
|
mayBeGenerateTests(files, config, rootBlock, context);
|
||||||
|
|
||||||
JsBlock block = program.getGlobalBlock();
|
|
||||||
|
|
||||||
// Invoke function passing modules as arguments
|
// Invoke function passing modules as arguments
|
||||||
// This should help minifier tool to recognize references to this modules as local variables and make them shorter.
|
// This should help minifier tool to recognize references to this modules as local variables and make them shorter.
|
||||||
List<JsExpression> invocationArgs = new ArrayList<JsExpression>(staticContext.getImportedModules().size() + 1);
|
List<ImportedModule> importedModuleList = new ArrayList<ImportedModule>();
|
||||||
JsName kotlinName = program.getScope().declareName(Namer.KOTLIN_NAME);
|
JsName kotlinName = program.getScope().declareName(Namer.KOTLIN_NAME);
|
||||||
rootFunction.getParameters().add(new JsParameter((kotlinName)));
|
rootFunction.getParameters().add(new JsParameter((kotlinName)));
|
||||||
invocationArgs.add(kotlinName.makeRef());
|
importedModuleList.add(new ImportedModule(Namer.KOTLIN_LOWER_NAME, kotlinName));
|
||||||
|
|
||||||
for (String importedModule : staticContext.getImportedModules().keySet()) {
|
for (String importedModule : staticContext.getImportedModules().keySet()) {
|
||||||
rootFunction.getParameters().add(new JsParameter(staticContext.getImportedModules().get(importedModule)));
|
rootFunction.getParameters().add(new JsParameter(staticContext.getImportedModules().get(importedModule)));
|
||||||
|
|
||||||
JsName globalId = program.getScope().declareName(Namer.suggestedModuleName(importedModule));
|
JsName globalId = program.getScope().declareName(Namer.suggestedModuleName(importedModule));
|
||||||
invocationArgs.add(JsAstUtils.pureFqn(globalId, null));
|
importedModuleList.add(new ImportedModule(importedModule, globalId));
|
||||||
}
|
}
|
||||||
JsInvocation invocation = new JsInvocation(rootFunction, invocationArgs);
|
|
||||||
|
|
||||||
String thisModuleName = JsDescriptorUtils.getModuleNameFromDescriptorName(moduleDescriptor);
|
|
||||||
JsName thisModuleId = program.getScope().declareName(Namer.suggestedModuleName(thisModuleName));
|
|
||||||
block.getStatements().add(JsAstUtils.newVar(thisModuleId,
|
|
||||||
new JsBinaryOperation(JsBinaryOperator.OR, thisModuleId.makeRef(), invocation)));
|
|
||||||
|
|
||||||
if (mainCallParameters.shouldBeGenerated()) {
|
if (mainCallParameters.shouldBeGenerated()) {
|
||||||
JsStatement statement = generateCallToMain(context, files, mainCallParameters.arguments());
|
JsStatement statement = generateCallToMain(context, files, mainCallParameters.arguments());
|
||||||
@@ -240,9 +241,144 @@ public final class Translation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsBlock block = program.getGlobalBlock();
|
||||||
|
block.getStatements().addAll(wrapIfNecessary(config.getModuleId(), rootFunction, importedModuleList, program,
|
||||||
|
config.getModuleKind()));
|
||||||
|
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static List<JsStatement> wrapIfNecessary(
|
||||||
|
@NotNull String moduleId,
|
||||||
|
@NotNull JsExpression function,
|
||||||
|
@NotNull List<ImportedModule> importedModules,
|
||||||
|
@NotNull JsProgram program,
|
||||||
|
@NotNull ModuleKind kind
|
||||||
|
) {
|
||||||
|
switch (kind) {
|
||||||
|
case AMD:
|
||||||
|
return wrapAmd(moduleId, function, importedModules, program);
|
||||||
|
case COMMON_JS:
|
||||||
|
return wrapCommonJs(function, importedModules, program);
|
||||||
|
case UMD:
|
||||||
|
return wrapUmd(moduleId, function, importedModules, program);
|
||||||
|
case PLAIN:
|
||||||
|
default:
|
||||||
|
return wrapPlain(moduleId, function, importedModules, program);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static List<JsStatement> wrapUmd(
|
||||||
|
@Nullable String moduleId,
|
||||||
|
@NotNull JsExpression function,
|
||||||
|
@NotNull List<ImportedModule> importedModules,
|
||||||
|
@NotNull JsProgram program
|
||||||
|
) {
|
||||||
|
JsScope scope = program.getScope();
|
||||||
|
JsName rootName = scope.declareName("root");
|
||||||
|
JsName factoryName = scope.declareName("factory");
|
||||||
|
JsName defineName = scope.declareName("define");
|
||||||
|
JsName exportsName = scope.declareName("exports");
|
||||||
|
|
||||||
|
JsExpression amdTest = JsAstUtils.and(JsAstUtils.typeOfIs(defineName.makeRef(), program.getStringLiteral("function")),
|
||||||
|
new JsNameRef("amd", defineName.makeRef()));
|
||||||
|
JsExpression commonJsTest = JsAstUtils.typeOfIs(exportsName.makeRef(), program.getStringLiteral("object"));
|
||||||
|
|
||||||
|
JsBlock amdBody = new JsBlock(wrapAmd(moduleId, factoryName.makeRef(), importedModules, program));
|
||||||
|
JsBlock commonJsBody = new JsBlock(wrapCommonJs(factoryName.makeRef(), importedModules, program));
|
||||||
|
JsInvocation plainInvocation = makePlainInvocation(factoryName.makeRef(), importedModules);
|
||||||
|
JsExpression plainExpr = moduleId != null ?
|
||||||
|
JsAstUtils.assignment(new JsNameRef(moduleId, rootName.makeRef()), plainInvocation) :
|
||||||
|
plainInvocation;
|
||||||
|
|
||||||
|
JsStatement selector = JsAstUtils.newJsIf(amdTest, amdBody, JsAstUtils.newJsIf(commonJsTest, commonJsBody, plainExpr.makeStmt()));
|
||||||
|
JsFunction adapter = new JsFunction(program.getScope(), new JsBlock(selector), "UMD adapter");
|
||||||
|
adapter.getParameters().add(new JsParameter(rootName));
|
||||||
|
adapter.getParameters().add(new JsParameter(factoryName));
|
||||||
|
|
||||||
|
return Collections.singletonList(new JsInvocation(adapter, JsLiteral.THIS, function).makeStmt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static List<JsStatement> wrapAmd(
|
||||||
|
@Nullable String moduleId,
|
||||||
|
@NotNull JsExpression function,
|
||||||
|
@NotNull List<ImportedModule> importedModules,
|
||||||
|
@NotNull JsProgram program
|
||||||
|
) {
|
||||||
|
JsScope scope = program.getScope();
|
||||||
|
JsName defineName = scope.declareName("define");
|
||||||
|
List<JsExpression> invocationArgs = new ArrayList<JsExpression>();
|
||||||
|
|
||||||
|
if (moduleId != null) {
|
||||||
|
invocationArgs.add(program.getStringLiteral(moduleId));
|
||||||
|
}
|
||||||
|
|
||||||
|
List<JsExpression> moduleNameList = new ArrayList<JsExpression>(importedModules.size());
|
||||||
|
for (ImportedModule importedModule : importedModules) {
|
||||||
|
moduleNameList.add(program.getStringLiteral(importedModule.id));
|
||||||
|
}
|
||||||
|
invocationArgs.add(new JsArrayLiteral(moduleNameList));
|
||||||
|
|
||||||
|
invocationArgs.add(function);
|
||||||
|
|
||||||
|
JsInvocation invocation = new JsInvocation(defineName.makeRef(), invocationArgs);
|
||||||
|
return Collections.singletonList(invocation.makeStmt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static List<JsStatement> wrapCommonJs(
|
||||||
|
@NotNull JsExpression function,
|
||||||
|
@NotNull List<ImportedModule> importedModules,
|
||||||
|
@NotNull JsProgram program
|
||||||
|
) {
|
||||||
|
JsScope scope = program.getScope();
|
||||||
|
JsName moduleName = scope.declareName("module");
|
||||||
|
JsName requireName = scope.declareName("require");
|
||||||
|
|
||||||
|
List<JsExpression> invocationArgs = new ArrayList<JsExpression>();
|
||||||
|
for (ImportedModule importedModule : importedModules) {
|
||||||
|
invocationArgs.add(new JsInvocation(requireName.makeRef(), program.getStringLiteral(importedModule.id)));
|
||||||
|
}
|
||||||
|
|
||||||
|
JsInvocation invocation = new JsInvocation(function, invocationArgs);
|
||||||
|
JsExpression assignment = JsAstUtils.assignment(new JsNameRef("exports", moduleName.makeRef()), invocation);
|
||||||
|
return Collections.singletonList(assignment.makeStmt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static List<JsStatement> wrapPlain(
|
||||||
|
@Nullable String moduleId,
|
||||||
|
@NotNull JsExpression function,
|
||||||
|
@NotNull List<ImportedModule> importedModules,
|
||||||
|
@NotNull JsProgram program
|
||||||
|
) {
|
||||||
|
JsInvocation invocation = makePlainInvocation(function, importedModules);
|
||||||
|
|
||||||
|
JsStatement statement;
|
||||||
|
if (moduleId == null) {
|
||||||
|
statement = invocation.makeStmt();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
statement = JsAstUtils.newVar(program.getScope().declareName(moduleId), function);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Collections.singletonList(statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static JsInvocation makePlainInvocation(@NotNull JsExpression function, @NotNull List<ImportedModule> importedModules) {
|
||||||
|
List<JsExpression> invocationArgs = new ArrayList<JsExpression>(importedModules.size());
|
||||||
|
|
||||||
|
for (ImportedModule importedModule : importedModules) {
|
||||||
|
invocationArgs.add(importedModule.name.makeRef());
|
||||||
|
}
|
||||||
|
|
||||||
|
return new JsInvocation(function, invocationArgs);
|
||||||
|
}
|
||||||
|
|
||||||
private static void defineModule(@NotNull TranslationContext context, @NotNull List<JsStatement> statements, @NotNull String moduleId) {
|
private static void defineModule(@NotNull TranslationContext context, @NotNull List<JsStatement> statements, @NotNull String moduleId) {
|
||||||
JsName rootPackageName = context.scope().findName(Namer.getRootPackageName());
|
JsName rootPackageName = context.scope().findName(Namer.getRootPackageName());
|
||||||
if (rootPackageName != null) {
|
if (rootPackageName != null) {
|
||||||
|
|||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
(function (root, factory) {
|
||||||
|
if (typeof define === 'function' && define.amd) {
|
||||||
|
define('Kotlin', [], factory);
|
||||||
|
} else if (typeof exports === 'object') {
|
||||||
|
module.exports = factory();
|
||||||
|
} else {
|
||||||
|
root.Kotlin = factory();
|
||||||
|
}
|
||||||
|
}(this, function () {
|
||||||
|
var Kotlin = {};
|
||||||
|
%output%
|
||||||
|
return Kotlin;
|
||||||
|
}));
|
||||||
-1
@@ -14,7 +14,6 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Kotlin = Kotlin || {};
|
|
||||||
(function (Kotlin) {
|
(function (Kotlin) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
|||||||
@@ -14,8 +14,6 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Kotlin = Kotlin || {};
|
|
||||||
|
|
||||||
(function (Kotlin) {
|
(function (Kotlin) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
|||||||
Vendored
-2
@@ -26,8 +26,6 @@
|
|||||||
// distributed under the License is distributed on an "AS-IS" BASIS,
|
// distributed under the License is distributed on an "AS-IS" BASIS,
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
|
||||||
var Kotlin = Kotlin || {};
|
|
||||||
|
|
||||||
(function (Kotlin) {
|
(function (Kotlin) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
|||||||
@@ -252,7 +252,7 @@
|
|||||||
|
|
||||||
<!-- Closure Compiler -->
|
<!-- Closure Compiler -->
|
||||||
<!-- A download url taken from http://code.google.com/p/closure-compiler/wiki/BinaryDownloads -->
|
<!-- A download url taken from http://code.google.com/p/closure-compiler/wiki/BinaryDownloads -->
|
||||||
<get src="http://dl.google.com/closure-compiler/compiler-20131014.zip"
|
<get src="http://dl.google.com/closure-compiler/compiler-20160315.zip"
|
||||||
dest="${dependencies}/download/closure-compiler.zip" usetimestamp="true"/>
|
dest="${dependencies}/download/closure-compiler.zip" usetimestamp="true"/>
|
||||||
|
|
||||||
<delete file="${dependencies}/closure-compiler.jar" failonerror="false"/>
|
<delete file="${dependencies}/closure-compiler.jar" failonerror="false"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user