| 1 | package Mojolicious::Plugin::AssetPack::Pipe::TypeScript; | ||
| 2 | use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe'; | ||
| 3 | |||
| 4 | use Mojo::File 'path'; | ||
| 5 | use Mojolicious::Plugin::AssetPack::Util qw(diag $CWD DEBUG); | ||
| 6 | |||
| 7 | has _typescript => sub { | ||
| 8 | my $self = shift; | ||
| 9 | |||
| 10 | return [$self->_find_app([qw(nodejs node)]), path(__FILE__)->dirname->child('typescript.js')->to_abs,]; | ||
| 11 | }; | ||
| 12 | |||
| 13 | sub process { | ||
| 14 | my ($self, $assets) = @_; | ||
| 15 | my $store = $self->assetpack->store; | ||
| 16 | my $file; | ||
| 17 | |||
| 18 | $assets->each(sub { | ||
| 19 | my ($asset, $index) = @_; | ||
| 20 | my $attrs = $asset->TO_JSON; | ||
| 21 | $attrs->{key} = 'ts'; | ||
| 22 | $attrs->{format} = 'js'; | ||
| 23 | return unless $asset->format eq 'ts'; | ||
| 24 | return $asset->content($file)->FROM_JSON($attrs) if $file = $store->load($attrs); | ||
| 25 | |||
| 26 | $self->_install_typescript unless $self->{installed}++; | ||
| 27 | local $CWD = $self->app->home->to_string; | ||
| 28 | local $ENV{NODE_PATH} = $self->app->home->rel_file('node_modules'); | ||
| 29 | |||
| 30 | $self->run($self->_typescript, \$asset->content, \my $js); | ||
| 31 | $asset->content($store->save(\$js, $attrs))->FROM_JSON($attrs); | ||
| 32 | }); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub _install_typescript { | ||
| 36 | my $self = shift; | ||
| 37 | |||
| 38 | # TODO: This is a bit fragile, since tsc is not part of typescript-simple | ||
| 39 | my $path = $self->app->home->rel_file('node_modules/.bin/tsc'); | ||
| 40 | return 1 if -e $path; | ||
| 41 | |||
| 42 | local $CWD = $self->app->home->to_string; | ||
| 43 | $self->app->log->warn('Installing typescript-simple... Please wait. (npm install typescript-simple)'); | ||
| 44 | $self->run([qw(npm install typescript-simple)]); | ||
| 45 | } | ||
| 46 | |||
| 47 | 1; | ||
| 48 | |||
| 49 | =encoding utf8 | ||
| 50 | |||
| 51 | =head1 NAME | ||
| 52 | |||
| 53 | Mojolicious::Plugin::AssetPack::Pipe::TypeScript - Process TypeScript .ts files | ||
| 54 | |||
| 55 | =head1 SYNOPSIS | ||
| 56 | |||
| 57 | $app->plugin(pipes => [qw(TypeScript JavaScript Combine)]); | ||
| 58 | $app->asset->process("app.js" => qw(foo.ts)); | ||
| 59 | |||
| 60 | =head1 DESCRIPTION | ||
| 61 | |||
| 62 | L<Mojolicious::Plugin::AssetPack::Pipe::TypeScript> will process | ||
| 63 | L<https://www.typescriptlang.org/> files into JavaScript. | ||
| 64 | |||
| 65 | This module require the C<typescript-simple> nodejs library to be installed. | ||
| 66 | C<typescript-simple> will be automatically installed using | ||
| 67 | L<https://www.npmjs.com/> unless already installed. | ||
| 68 | |||
| 69 | =head1 METHODS | ||
| 70 | |||
| 71 | =head2 process | ||
| 72 | |||
| 73 | See L<Mojolicious::Plugin::AssetPack::Pipe/process>. | ||
| 74 | |||
| 75 | =head1 SEE ALSO | ||
| 76 | |||
| 77 | L<Mojolicious::Plugin::AssetPack>. | ||
| 78 | |||
| 79 | =cut |