| 1 | package Mojolicious::Plugin::AssetPack::Pipe::Vuejs; | ||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | sub process { | ||||||||
|
Subroutine
process: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 5 | 1 | my ($self, $assets) = @_; | |||||||||
| 6 | 1 | my $store = $self->assetpack->store; | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | return $assets->each(sub { | ||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.3
| |||||||||||
| 9 | 1 | my ($asset, $index) = @_; | |||||||||
| 10 | 1 | ▶ | return unless $asset->format eq 'vue'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | my $vue = sprintf 'Vue.component("%s", {', $asset->name; | |||||||||
| 13 | 1 | my ($script, $template); | |||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | if ($asset->content =~ m!<script[^>]*>(.+)</script>!s) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 1 | $script = $1; | |||||||||
| 17 | 1 | ▶ | $vue = "$1$vue" if $script =~ s!^(.*)\s?module\.exports\s*=\s*\{!!s; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 1 | $script =~ s!\s*\}\s*;?\s*$!!s; | |||||||||
| 19 | 1 | $vue .= $script; | |||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | 1 | ▶ | if ($asset->content =~ m!<template[^>]*>(.+)</template>!s) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 1 | $template = $1; | |||||||||
| 24 | 1 | $template =~ s!"!\\"!g; # escape all double quotes | |||||||||
| 25 | 1 | $template =~ s!^\s*!!s; | |||||||||
| 26 | 1 | $template =~ s!\r?\n!\\n!g; | |||||||||
| 27 | 1 | ▶ | $vue .= qq',\n' if $script; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 1 | $vue .= qq' template: "$template"'; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 1 | $vue = Mojo::Util::encode('UTF-8', "(function(){$vue})})();"); | |||||||||
| 32 | 1 | $asset->content($vue)->format('js'); | |||||||||
| 33 | 1 | }); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 1 | 1; | |||||||||
| 37 | |||||||||||
| 38 | =encoding utf8 | ||||||||||
| 39 | |||||||||||
| 40 | =head1 NAME | ||||||||||
| 41 | |||||||||||
| 42 | Mojolicious::Plugin::AssetPack::Pipe::Vuejs - Process .vue files | ||||||||||
| 43 | |||||||||||
| 44 | =head1 DESCRIPTION | ||||||||||
| 45 | |||||||||||
| 46 | This modules is EXPERIMENTAL and based on homebrewed regexes instead of running | ||||||||||
| 47 | the code through an external nodejs binary! | ||||||||||
| 48 | |||||||||||
| 49 | This pipe could get pulled completely from the | ||||||||||
| 50 | L<Mojolicious::Plugin::AssetPack> distribution if the problem is too hard to | ||||||||||
| 51 | solve. | ||||||||||
| 52 | |||||||||||
| 53 | =head1 SYNOPSIS | ||||||||||
| 54 | |||||||||||
| 55 | Currently only C<script> and C<template> is supported. C<style> is not yet | ||||||||||
| 56 | supported. | ||||||||||
| 57 | |||||||||||
| 58 | Here is an example C<.vue> file: | ||||||||||
| 59 | |||||||||||
| 60 | <template> | ||||||||||
| 61 | <h1 :id="id">Example</h1> | ||||||||||
| 62 | <button @click="toggle" :disabled="loading">{{loading ? "loading" : "click me!"}}</button> | ||||||||||
| 63 | </template> | ||||||||||
| 64 | |||||||||||
| 65 | <script> | ||||||||||
| 66 | var initial = false; | ||||||||||
| 67 | module.exports = { | ||||||||||
| 68 | data: function() { | ||||||||||
| 69 | return {id: id, loading: initial} | ||||||||||
| 70 | }, | ||||||||||
| 71 | methods: { | ||||||||||
| 72 | toggle: function() { | ||||||||||
| 73 | this.loading = !this.loading; | ||||||||||
| 74 | } | ||||||||||
| 75 | } | ||||||||||
| 76 | }; | ||||||||||
| 77 | </script> | ||||||||||
| 78 | |||||||||||
| 79 | The vuejs file above produces this output: | ||||||||||
| 80 | |||||||||||
| 81 | (function(){ | ||||||||||
| 82 | var initial = false; | ||||||||||
| 83 | Vue.component("example", { | ||||||||||
| 84 | data: function() { | ||||||||||
| 85 | return {id: id, loading: initial} | ||||||||||
| 86 | }, | ||||||||||
| 87 | methods: { | ||||||||||
| 88 | toggle: function() { | ||||||||||
| 89 | this.loading = !this.loading; | ||||||||||
| 90 | } | ||||||||||
| 91 | }, | ||||||||||
| 92 | template: " | ||||||||||
| 93 | Example | ||||||||||
| 94 | {{loading ? \"loading\" : \"click me!\"}} | ||||||||||
| 95 | "}))(); | ||||||||||
| 96 | |||||||||||
| 97 | =head1 METHODS | ||||||||||
| 98 | |||||||||||
| 99 | =head2 process | ||||||||||
| 100 | |||||||||||
| 101 | See L<Mojolicious::Plugin::AssetPack::Pipe/process>. | ||||||||||
| 102 | |||||||||||
| 103 | =head1 SEE ALSO | ||||||||||
| 104 | |||||||||||
| 105 | L<Mojolicious::Plugin::AssetPack>. | ||||||||||
| 106 | |||||||||||
| 107 | =cut | ||||||||||