blib/lib/Mojolicious/Plugin/AssetPack/Pipe/VueTemplateCompiler.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 128 CC 8 SCAR 38.8
CC
8
Cov
0%
process
72.0
_save_topic
2.0
BEGIN
2.0
CRAP 48.7
1 package Mojolicious::Plugin::AssetPack::Pipe::VueTemplateCompiler;
2 use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe';
3 use Mojolicious::Plugin::AssetPack::Util qw(diag $CWD DEBUG checksum);
4 use Mojo::Util qw(url_unescape decode);
5  
6 has config => sub { my $self = shift; my $config = $self->assetpack->config || $self->assetpack->config({}); $config->{VueTemplateCompiler} ||= {} };
7 has enabled => sub { shift->config->{enabled} || 0 };
8 has mt => sub { Mojo::Template->new->vars(1) };
9 has dist_template => sub { shift->config->{dist_template} || q|parcelRequire.register("<%= $asset_url %>", (function(){<%= $content %>; return {render,staticRenderFns};})());| };
10 #~ has parceljs => sub { [qw(parcel build --no-cache )] };#--out-file --out-dir
11 #~ has parceljs => "/tmp/node_modules/parcel-bundler/bin/cli.js"; #--out-file --out-dir
12 #~ has compiler => sub { Mojo::File->new(__FILE__)->sibling('vue-template-compiler.js') };
13 has revision => sub { shift->assetpack->revision // '' };
14 has _compiler => sub {
15 my $self = shift;
16 my $bin = Mojo::Loader::data_section(__PACKAGE__, 'vue-template-compiler.js');
17 my $tmp = Mojo::File::tempfile(SUFFIX => '.js');
18 $tmp->spurt($bin);
19 #~ return [$self->_find_app([qw(nodejs node)]), $tmp->realpath];
20 };
21  
22  
23 sub process {
24 my ($self, $assets) = @_;
25  
26 my $topicURL = Mojo::URL->new($self->topic);
27 my $topic = url_unescape($topicURL->path->to_string);
28 my $format = $topicURL->path->[-1] =~ /\.(\w+)$/ ? lc $1 : '';
29
30 return
31 unless $format eq 'js';
32
33 my $checksum = checksum $topic.$self->revision;#
34 my $store = $self->assetpack->store;
35 my $attrs = {key => "js-vue-template", url=>$topic, name=>decode('UTF-8', $topic), checksum=>$checksum, minified=>1, format=>$format,};# for store
36
37 if ($self->assetpack->minify || !$self->enabled) {# production loads development
38 #~ my $asset = $store->load($attrs)
39 my $file = Mojo::File->new($self->app->static->paths->[0], $topic);
40 splice @$assets, 0, scalar @$assets,
41 Mojolicious::Plugin::AssetPack::Asset->new(url => $topic)->checksum($checksum)->minified(1)
42 ->content($file->slurp)
43 if -f $file;
44 return;
45 }
46
47 my @content = ();
48 $assets->each(
49 sub {
50 my ($asset, $index) = @_;
51 return
52 if $asset->format ne 'html' || $asset->minified;
53
54 return
55 unless $asset->name =~ /\.vue$/;
56
57 $asset->format('js');
58 my $attrs = $asset->TO_JSON;
59 $attrs->{key} = 'js-vue-template';
60 $attrs->{minified} = 1;
61 #~ $attrs->{name} = $attrs->{checksum};
62 my $url = url_unescape(Mojo::URL->new($asset->url)->path->to_string);
63 #~ my $file = $asset->path; #Mojo::File
64 #~ my $tmp_file_vue = $file->copy_to($file->new("/tmp/".$asset->name));#
65 #~ DEBUG &&
66 diag "Compile Vue Template: [%s] to topic [%s] ", $url, $topic;#$self->assetpack->app->dumper($asset);#, $file->stat->size;join("", map(("$_=>"=>$attrs->{$_}), keys %$attrs))
67
68 local $CWD = "/tmp";#$self->app->home->to_string;
69
70 #~ local $ENV{NODE_ENV} = $self->app->mode;
71 local $ENV{NODE_PATH} = '/tmp/node_modules';#$self->app->home->rel_file('node_modules');
72
73
74 #~ my $tmp_vue = $asset->path->copy_to(Mojo::File->new("/tmp/".$asset->name));#
75 #~ $self->run(['node', $self->parceljs, 'build', ' --no-cache', '--out-file', $tmp_vue->path.'.js', '--out-dir', '.', $tmp_vue->path, ], undef, undef,);# \my $content
76 #~ my $js = Mojo::File->new($tmp_vue->path.'.js');
77 #~ diag sprintf qq|"%s":function(){%s}|, $asset->url, $self->_parse_render_function($js->slurp);
78 #~ my $content = sprintf qq|"%s":function(){%s}|, $url, $self->_parse_render_function($js->slurp);
79 #~ my $content = sprintf qq|parcelRequire.register("%s", {%s});|, $url, $self->_parse_render_function($js->slurp);
80 #~ $tmp_vue->remove;
81 #~ $js->remove;
82
83 $self->_install_node_modules('vue-template-compiler', '@vue/component-compiler-utils');
84 $self->run([$self->_find_app([qw(nodejs node)]), $self->_compiler->realpath], \$asset->content, \my $content);
85 #~ $content = sprintf qq|parcelRequire.register("%s", (function(){%s; return {render,staticRenderFns};})());|, $url, $content;
86 $content = $self->mt->render($self->dist_template, {asset => $asset, asset_url => $url, content => $content});
87 push @content, $content;
88 $asset->content($store->save(\$content, $attrs)->minified(1));
89 }
90 );
91
92 return
93 unless scalar @content;
94
95 my $content = join "\n", @content;
96 my $path = $self->_save_topic($topic, $content);
97
98 #~ my $asset = Mojolicious::Plugin::AssetPack::Asset->new(url => $topic)->checksum($checksum)->minified(1)
99 #~ ->content($content);
100  
101 Mojo::File::path($self->app->static->paths->[0], 'cache', $topic)->dirname->make_path;
102 #~ $self->assetpack->{by_checksum}{$checksum}
103 my $asset = $store->save(\$content, $attrs);#decode('UTF-8', $topic)
104 $self->assetpack->{by_topic}{$topic} = [$asset];
105 #~ $self->assetpack->process($topic=>$path);
106 }
107  
108 sub _save_topic {
109 my ($self, $topic, $content) = @_;
110 my $path = Mojo::File::path($self->app->static->paths->[0], $topic);
111 DEBUG && diag "Save Vue compiled topic [%s]", $path;
112 $path->dirname->make_path;
113 $path->spurt($content);
114 }
115  
116 #~ sub _compiler_file {
117 #~ my ($self, $topic) = @_;
118 #~ diag "_compiler [%s]", $tmp;
119 #~ my $mt = Mojo::Template->new;
120 #~ }
121
122 1;
123  
124 =pod
125  
126 =encoding utf8
127  
128 Доброго всем
129  
130 ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
131  
132 =head1 NAME
133  
134 Mojolicious::Plugin::AssetPack::Pipe::VueTemplateCompiler - if you like separate files vue template and vue scrtipt.
135  
136  
137 =head1 SYNOPSIS
138  
139 $app->plugin('AssetPack::Che' => {
140 pipes => [qw(VueTemplateCompiler CombineFile)],
141 VueTemplateCompiler=>{# pipe options
142 enabled=>$ENV{MOJO_ASSETPACK_VUE_TEMPLATE_COMPILER} || 0,
143 # dist_template pass to Mojo::Template->render, $content is compiled source template to JS render and staticRenderFns
144 dist_template=>q|parcelRequire.register("<%= $asset_url %>", (function(){<%= $content %>; return {render,staticRenderFns};})());|,
145 },
146 process => [
147 ['js/dist/templates/app★.js?bla'=>qw(components/foo.vue.html components/bar.vue.html)],
148 ['app.js'=>qw('js/dist/templates/app★.js components/foo.vue.js components/bar.vue.js)],
149 ...,
150 ],
151 });
152  
153  
154 =head1 Конфигурация CONFIG
155  
156 Обработка файлов-шаблонов B<< \<path|url>.vue.html >> пойдет только (ONLY) в режиме development.
157  
158 Обработанные топики шаблонов сохраняются в пути этого топика относительно static L<https://metacpan.org/pod/Mojolicious#static>.
159  
160 В режиме production эти топики используются как обычные ассеты.
161  
162  
163 =head1 SEE ALSO
164  
165 L<Mojolicious::Plugin::AssetPack::Che>
166  
167 L<Mojolicious::Plugin::AssetPack>
168  
169 =head1 AUTHOR
170  
171 Михаил Че (Mikhail Che), C<< <mche[-at-]cpan.org> >>
172  
173 =head1 BUGS / CONTRIBUTING
174  
175 Please report any bugs or feature requests at L<https://github.com/mche/Mojolicious-Plugin-AssetPack-Che/issues>. Pull requests also welcome.
176  
177 =head1 COPYRIGHT
178  
179 Copyright 2020-2021 Mikhail Che.
180  
181 This library is free software; you can redistribute it and/or modify
182 it under the same terms as Perl itself.
183  
184 =cut
185  
186 __DATA__