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);
$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);
}
}
}
+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} // {};
$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!");
}