OSDN Git Service

Plugin::JavaScriptLoader: add tsc filter
authorhylom <hylom@users.sourceforge.jp>
Thu, 4 Apr 2019 14:47:41 +0000 (23:47 +0900)
committerhylom <hylom@users.sourceforge.jp>
Thu, 4 Apr 2019 14:47:41 +0000 (23:47 +0900)
src/newslash_web/lib/Newslash/Plugin/JavaScriptLoader.pm
src/newslash_web/lib/Newslash/Plugin/Preprocessor.pm

index e470a07..dbd8c2e 100644 (file)
@@ -11,6 +11,8 @@ use Mojo::File qw(path);
 use constant CACHE_KEY => "JavaScriptLoader";
 use constant JS_CONTENT_TYPE => "application/javascript";
 use Data::Dumper;
+use File::Temp;
+use File::Basename;
 
 use IO::Uncompress::Gunzip qw(gunzip);
 
@@ -32,6 +34,16 @@ sub _generate_content {
         $self->{app}->log->error("JavaScriptLoader: generate content failed: $pathname");
         return;
     }
+
+    if ($self->{use_tsc}) {
+        $js_content = $self->_tsc_filter($js_content);
+        if (!$js_content) {
+        $self->{app}->log->error("JavaScriptLoader: tsc error for $pathname");
+        return;
+        }
+    }
+
+
     return $self->_build_content_object($js_content, $pathname, JS_CONTENT_TYPE);
 }
 
@@ -132,6 +144,47 @@ sub load_all_bundles {
     }
 }
 
+sub _tsc_filter {
+    my ($self, $content) = @_;
+    my $args;
+    my $command = "tsc";
+
+    my $fh_tmp = File::Temp->new(TEMPLATE => "NSJSXXXX",
+                                 SUFFIX => ".js");
+    $fh_tmp->print(Encode::encode_utf8($content));
+    $fh_tmp->flush;
+
+    my $filename = $fh_tmp->filename;
+    my ($name, $path, $suffix) = fileparse($filename, qw(.js));
+    my $outfile = $path . $name . ".tsc.js";
+
+    $args = [ "--allowJS", "--outFile", $outfile, $filename, ];
+
+    $self->{app}->log->debug("JavaScriptLoader: run tsc...");
+    my ($rs, $success) = $self->_execute_cmd($command, undef, $args);
+    $fh_tmp->close;
+    if (!$success) {
+        $self->{app}->log->error("JavaScriptLoader: tsc failed.");
+        $self->{app}->log->error($rs);
+        return;
+    }
+
+    my $fh;
+    if (!open($fh, "<:utf8", $outfile)) {
+        $self->{app}->log->error("JavaScriptLoader: cannot read tsc generated file: $outfile.");
+        return;
+    }
+    my $js_body = do { local($/); <$fh> } ;
+    close($fh);
+    unlink $outfile;
+    if ($!) {
+        $self->{app}->log->error("JavaScriptLoader: cannot unlink tsc generated file: $outfile.");
+    }
+
+    return $js_body;
+
+}
+
 sub _is_bundled {
     my ($self, $target) = @_;
     my $pool = $self->{bundled} // {};
@@ -165,6 +218,17 @@ sub register {
     $self->{compress} = $cnf->{use_compression};
     $self->{bundled} = {};
 
+    if ($self->{conf}->{tsc_mode}) {
+        if ($self->{conf}->{tsc_mode} eq "always") {
+            $self->{use_tsc} = 1;
+        }
+        else {
+            $self->{use_tsc} = $self->{mode} eq ($self->{conf}->{tsc_mode} || "");
+        }
+    }
+
+    $self->{tsc_command} = "tsc";
+
     if (!$self->{conf}->{source_directory}) {
         $app->log->warn("JavaScriptLoader: no source_directory given!");
     }
index cafe92a..0536dd8 100644 (file)
@@ -406,7 +406,7 @@ sub _execute_cmd {
     # decode result to Perl's internal format
     $result = Encode::decode('utf8', $result);
 
-    return $result;
+    return wantarray ? ($result, 1) : $result;
 }