TAP5-2591: add support for TypeScript modules

Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/0f2c01c9
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/0f2c01c9
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/0f2c01c9

Branch: refs/heads/master
Commit: 0f2c01c9a6747721ceaebba114a15bf48fb577e9
Parents: 80f65eb
Author: Jochen Kemnade <[email protected]>
Authored: Mon Nov 27 13:00:35 2017 +0100
Committer: Jochen Kemnade <[email protected]>
Committed: Mon Nov 27 13:00:35 2017 +0100

----------------------------------------------------------------------
 tapestry-webresources/build.gradle              |     3 +-
 .../webresources/TypeScriptCompiler.java        |    76 +
 .../modules/WebResourcesModule.java             |     8 +-
 .../webresources/internal/invoke-typescript.js  |    24 +
 .../webresources/internal/typescript.js         | 97680 +++++++++++++++++
 .../tests/TypeScriptCompilerSpec.groovy         |    58 +
 .../t5/webresources/greeter-compiled.js         |     8 +
 .../test/resources/t5/webresources/greeter.ts   |     7 +
 .../t5/webresources/park-example-compiled.js    |    26 +
 .../resources/t5/webresources/park-example.ts   |     6 +
 10 files changed, 97894 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/0f2c01c9/tapestry-webresources/build.gradle
----------------------------------------------------------------------
diff --git a/tapestry-webresources/build.gradle 
b/tapestry-webresources/build.gradle
index 874cfef..775cdde 100644
--- a/tapestry-webresources/build.gradle
+++ b/tapestry-webresources/build.gradle
@@ -8,7 +8,8 @@ dependencies {
 
     testCompile project(":tapestry-runner")
     testCompile "org.gebish:geb-spock:${versions.geb}"
-
+    testCompile "org.spockframework:spock-tapestry:${versions.spock}"
+    
     testCompile "org.seleniumhq.selenium:selenium-java:${versions.selenium}", {
         exclude group: "org.eclipse.jetty"
     }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/0f2c01c9/tapestry-webresources/src/main/java/org/apache/tapestry5/internal/webresources/TypeScriptCompiler.java
----------------------------------------------------------------------
diff --git 
a/tapestry-webresources/src/main/java/org/apache/tapestry5/internal/webresources/TypeScriptCompiler.java
 
b/tapestry-webresources/src/main/java/org/apache/tapestry5/internal/webresources/TypeScriptCompiler.java
new file mode 100755
index 0000000..355aee1
--- /dev/null
+++ 
b/tapestry-webresources/src/main/java/org/apache/tapestry5/internal/webresources/TypeScriptCompiler.java
@@ -0,0 +1,76 @@
+package org.apache.tapestry5.internal.webresources;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.tapestry5.ContentType;
+import org.apache.tapestry5.annotations.Path;
+import org.apache.tapestry5.internal.InternalConstants;
+import org.apache.tapestry5.ioc.OperationTracker;
+import org.apache.tapestry5.ioc.Resource;
+import org.apache.tapestry5.ioc.internal.util.ClasspathResource;
+import org.apache.tapestry5.ioc.internal.util.InternalUtils;
+import org.apache.tapestry5.services.assets.ResourceDependencies;
+import org.apache.tapestry5.services.assets.ResourceTransformer;
+import org.mozilla.javascript.NativeObject;
+
+public class TypeScriptCompiler implements ResourceTransformer {
+  private final static Charset UTF8 = StandardCharsets.UTF_8;
+
+  private final RhinoExecutorPool executorPool;
+
+  @Override
+  public ContentType getTransformedContentType()
+  {
+      return InternalConstants.JAVASCRIPT_CONTENT_TYPE;
+  }
+
+  public TypeScriptCompiler(final OperationTracker tracker,
+      
@Path("classpath:org/apache/tapestry5/webresources/internal/typescript.js") 
final Resource typescript)
+  {
+      this.executorPool = new RhinoExecutorPool(tracker, Arrays.<Resource> 
asList(typescript,
+          new 
ClasspathResource("org/apache/tapestry5/webresources/internal/invoke-typescript.js")));
+
+  }
+
+  private static String getString(final NativeObject object, final String key)
+  {
+      return object.get(key).toString();
+  }
+
+  @Override
+  public InputStream transform(final Resource source, final 
ResourceDependencies dependencies) throws IOException
+  {
+      InputStream is = null;
+      String content;
+  
+      try
+      {
+          is = source.openStream();
+          content = IOUtils.toString(is, UTF8);
+      } finally {
+          InternalUtils.close(is);
+      }
+  
+      RhinoExecutor executor = executorPool.get();
+  
+      try {
+  
+          NativeObject result = (NativeObject) 
executor.invokeFunction("transpile", content, source.toString());
+  
+          if (result.containsKey("exception")) {
+              throw new RuntimeException(getString(result, "exception"));
+          }
+  
+          return IOUtils.toInputStream(getString(result, "output"), UTF8);
+  
+        } finally {
+          executor.discard();
+        }
+
+   }
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/0f2c01c9/tapestry-webresources/src/main/java/org/apache/tapestry5/webresources/modules/WebResourcesModule.java
----------------------------------------------------------------------
diff --git 
a/tapestry-webresources/src/main/java/org/apache/tapestry5/webresources/modules/WebResourcesModule.java
 
b/tapestry-webresources/src/main/java/org/apache/tapestry5/webresources/modules/WebResourcesModule.java
index 7a716e4..c2adcad 100644
--- 
a/tapestry-webresources/src/main/java/org/apache/tapestry5/webresources/modules/WebResourcesModule.java
+++ 
b/tapestry-webresources/src/main/java/org/apache/tapestry5/webresources/modules/WebResourcesModule.java
@@ -15,6 +15,7 @@ package org.apache.tapestry5.webresources.modules;
 import com.github.sommeri.less4j.LessCompiler;
 import com.github.sommeri.less4j.core.parser.AntlrException;
 import com.google.javascript.jscomp.CompilationLevel;
+
 import org.apache.tapestry5.MarkupWriter;
 import org.apache.tapestry5.internal.webresources.*;
 import org.apache.tapestry5.ioc.MappedConfiguration;
@@ -63,7 +64,8 @@ public class WebResourcesModule
 
     @Contribute(StreamableResourceSource.class)
     public static void provideCompilers(MappedConfiguration<String, 
ResourceTransformer> configuration, ResourceTransformerFactory factory,
-                                        @Autobuild CoffeeScriptCompiler 
coffeeScriptCompiler)
+                                        @Autobuild CoffeeScriptCompiler 
coffeeScriptCompiler,
+                                        @Autobuild final TypeScriptCompiler 
tsCompiler)
     {
         // contribution ids are file extensions:
 
@@ -75,6 +77,10 @@ public class WebResourcesModule
         configuration.add("less",
                 factory.createCompiler("text/css", "Less", "CSS", new 
LessResourceTransformer(),
                         CacheMode.MULTIPLE_FILE));
+
+        configuration.add("ts",
+                factory.createCompiler("text/javascript", "TS", "JavaScript", 
tsCompiler,
+                        CacheMode.SINGLE_FILE));
     }
 
     @Contribute(ResourceMinimizer.class)

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/0f2c01c9/tapestry-webresources/src/main/resources/org/apache/tapestry5/webresources/internal/invoke-typescript.js
----------------------------------------------------------------------
diff --git 
a/tapestry-webresources/src/main/resources/org/apache/tapestry5/webresources/internal/invoke-typescript.js
 
b/tapestry-webresources/src/main/resources/org/apache/tapestry5/webresources/internal/invoke-typescript.js
new file mode 100644
index 0000000..fa18d6d
--- /dev/null
+++ 
b/tapestry-webresources/src/main/resources/org/apache/tapestry5/webresources/internal/invoke-typescript.js
@@ -0,0 +1,24 @@
+//https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API
+function transpile(fileNames) {
+  var compilerOptions = {
+    noEmitOnError : true,
+    emitDecoratorMetadata: true,
+    experimentalDecorators: true,
+    newLine: 'LF',
+    noImplicitAny : true,
+    target : ts.ScriptTarget.ES5,
+    module : "AMD"
+  };
+  
+  var transpileOptions = {
+    compilerOptions : compilerOptions,
+    reportDiagnostics: false
+  };
+  
+  var result = ts.transpileModule(fileNames, transpileOptions);
+
+  return {
+    output : result.outputText
+  };
+
+}

Reply via email to