Mojolicious/Plugin/BarefootJS.pm

filter: Statementstmt 93.6% 59 / 63 Branchbran 66.6% 20 / 30 Conditioncond 51.8% 14 / 27 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 70.7% 104 / 147 CC 28 SCAR 24.4
CC
28
Cov
78%
__ANON__
17.7
__ANON__
11.8
register
5.4
CRAP 11.4
↑ Summary Mojolicious/Plugin/BarefootJS/DevReload.pm »
1 package Mojolicious::Plugin::BarefootJS;
2 2 our $VERSION = "0.41.4";
3 2 ▶use Mojo::Base 'Mojolicious::Plugin', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use BarefootJS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Plugin entry point. Wires up:
11 #
12 # 1. The `bf` controller helper. Lazily instantiates one
13 # BarefootJS object per request and stashes it under
14 # `bf.instance`.
15 #
16 # 2. A `before_render` hook that, when the rendered template name
17 # matches a top-level component in the build manifest, fills the
18 # heavy boilerplate the user previously hand-rolled in `app.pl`:
19 # generates the scope id, registers every UI-registry child
20 # renderer from the manifest, and seeds the stash with each
21 # template variable's static default (issue #1416).
22 #
23 # Configuration (all optional):
24 # - manifest_path: absolute path to the `bf build`-emitted
25 # `manifest.json`. Defaults to `<app->home>/dist/templates/manifest.json`.
26 # Pass `undef` to disable manifest-driven auto-init entirely; the
27 # bf helper is still installed and callers can drive everything
28 # manually as before.
29 9 ▶sub register ($self, $app, $config = {}) {
Subroutine register: called CC=5 SCAR=16.9
Pod documented
30 13 ▶ $app->helper(bf => sub ($c) {
Subroutine __ANON__: called CC=2 SCAR=7.2
31 13 ▶ $c->stash->{'bf.instance'} //= BarefootJS->new($c, $config);
Condition66%2/3
$c->stash->{"bf.instance"} //= "BarefootJS"->new($c, $config)
l!l&&r!l&&!r
490
MC/DC0%0/2
$c->stash->{"bf.instance"} //= "BarefootJS"->new($c, $config)
$c->stash->{"bf.instance"} "BarefootJS"->new($c, $config)
Truth table66%2/3
A //= B
A= $c->stash->{"bf.instance"}B= "BarefootJS"->new($c, $config)
ABresult
1X1
011
000
32 9 });
33  
34 9 ▶ return if exists $config->{manifest_path} && !defined $config->{manifest_path};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{"manifest_path"} and not defined $config->{"manifest_path"} 1 8 9
Condition66%2/3
exists $config->{"manifest_path"} and not defined $config->{"manifest_path"}
!ll&&!rl&&r
081
MC/DC50%1/2
exists $config->{"manifest_path"} and not defined $config->{"manifest_path"}
exists $config->{"manifest_path"} not defined $config->{"manifest_path"}
Truth table66%2/3
A and not(B)
A= exists $config->{"manifest_path"}B= not defined $config->{"manifest_path"}
ABresult
0X0
100
111
35 my $manifest_path = path(
36 8 ▶ $config->{manifest_path} // $app->home->child('dist/templates/manifest.json'));
Condition33%1/3
$config->{"manifest_path"} // $app->home->child("dist/templates/manifest.json")
l!l&&r!l&&!r
800
MC/DC0%0/2
$config->{"manifest_path"} // $app->home->child("dist/templates/manifest.json")
$config->{"manifest_path"} $app->home->child("dist/templates/manifest.json")
Truth table33%1/3
A // B
A= $config->{"manifest_path"}B= $app->home->child("dist/templates/manifest.json")
ABresult
1X1
011
000
37  
38 # Manifest resolution is lazy, re-checked per render (#2126). The
39 # scaffold's dev script starts `bf build --watch` and the web server
40 # concurrently, so the app routinely boots before the first build has
41 # written `manifest.json`. Loading once at register time turned that
42 # startup race into a permanent failure: auto-init stayed disabled for
43 # the server's lifetime and every top-level render died under strict
44 # (`Global symbol "$initial" requires explicit package name`). The
45 # cache is keyed on the file's (mtime, size), so the steady-state cost
46 # is one stat() per render — and `bf build --watch` rebuilds (new
47 # ssrDefaults, `bf add`ed components) are picked up without a restart.
48 8 my ($cached_sig, $manifest, $is_child_entry);
49 10 ▶ my $load_manifest = sub {
Subroutine __ANON__: called CC=10 SCAR=24.7
50 10 my ($size, $mtime) = (stat "$manifest_path")[7, 9];
51 10 ▶ unless (defined $mtime) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mtime) 1 9 10
52 # Missing (or unreadable) — drop the cache so the manifest is
53 # re-read as soon as the first build writes it.
54 1 ($cached_sig, $manifest, $is_child_entry) = (undef, undef, undef);
55 1 return undef;
56 }
57 9 my $sig = "$mtime/$size";
58 9 ▶ return $manifest if defined $cached_sig && $cached_sig eq $sig;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cached_sig and $cached_sig eq $sig 0 9 9
Condition66%2/3
defined $cached_sig and $cached_sig eq $sig
!ll&&!rl&&r
810
MC/DC0%0/2
defined $cached_sig and $cached_sig eq $sig
defined $cached_sig $cached_sig eq $sig
Truth table66%2/3
A and B
A= defined $cached_sigB= $cached_sig eq $sig
ABresult
0X0
100
111
59 # Cache parse failures under the same signature so a broken file
60 # warns once, not on every render, and retries when it changes.
61 9 $cached_sig = $sig;
62 9 ($manifest, $is_child_entry) = (undef, undef);
63 9 my $m = eval { decode_json($manifest_path->slurp) };
64 9 ▶ if ($@ || ref($m) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or ref $m ne "HASH") 0 9 9
Condition33%1/3
$@ or ref $m ne "HASH"
l!l&&r!l&&!r
009
MC/DC0%0/2
$@ or ref $m ne "HASH"
$@ ref $m ne "HASH"
Truth table33%1/3
A or B
A= $@B= ref $m ne "HASH"
ABresult
1X1
011
000
65 0 ▶ $app->log->warn("BarefootJS: cannot parse manifest at $manifest_path: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
66 0 return undef;
67 }
68 9 $manifest = $m;
69  
70 # Cache the set of UI-registry slot keys so we can answer
71 # "is this template name a child or a top-level page?" with a
72 # single hash lookup at render time. Top-level entries are
73 # everything that isn't `__barefoot__` and doesn't match
74 # `ui/<name>/index` — the same partition `register_components_from_manifest`
75 # applies internally.
76 9 $is_child_entry = {};
77 9 for my $entry_name (keys %$manifest) {
78 10 ▶ next if $entry_name eq '__barefoot__';
Branch: 1 branch
BranchTrueFalseTotal
if $entry_name eq "__barefoot__" 0 10 10
79 10 ▶ next unless $entry_name =~ m{^ui/[^/]+/index$};
Branch: 1 branch
BranchTrueFalseTotal
unless $entry_name =~ m[^ui/[^/]+/index$]u 8 2 10
80 2 $is_child_entry->{$entry_name} = 1;
81 }
82 9 return $manifest;
83 8 };
84  
85 10 ▶ $app->hook(before_render => sub ($c, $args) {
Subroutine __ANON__: called CC=14 SCAR=28.7
86 10 my $template = $args->{template};
87 10 ▶ return unless defined $template && length $template;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $template and length $template 0 10 10
Condition33%1/3
defined $template and length $template
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $template and length $template
defined $template length $template
Truth table33%1/3
A and B
A= defined $templateB= length $template
ABresult
0X0
100
111
88 10 ▶ my $m = $load_manifest->() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $m = &$load_manifest() or return 9 1 10
89 9 my $entry = $m->{$template};
90 9 ▶ return unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 0 9 9
91 9 ▶ return if $is_child_entry->{$template};
Branch: 1 branch
BranchTrueFalseTotal
if $is_child_entry->{$template} 1 8 9
92 # Idempotency guard for nested renders. A controller might
93 # call `render_to_string` inside an action and then `render`
94 # — without this we'd re-init `bf` on the second pass and
95 # wipe the script registrations the first pass collected.
96 8 ▶ return if $c->stash->{'bf.auto_init_done'};
Branch: 1 branch
BranchTrueFalseTotal
if $c->stash->{"bf.auto_init_done"} 0 8 8
97  
98 # Escape hatch for callers that wire `bf` up by hand (the
99 # existing `render_component` helper in the showcase app does
100 # this). If `_scope_id` is already set we treat the request as
101 # "manually managed" and leave it alone — same outcome as
102 # before the plugin gained auto-init.
103 8 my $bf = $c->bf;
104 8 ▶ if (defined $bf->_scope_id && length $bf->_scope_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bf->_scope_id and length $bf->_scope_id) 0 8 8
Condition33%1/3
defined $bf->_scope_id and length $bf->_scope_id
!ll&&!rl&&r
800
MC/DC0%0/2
defined $bf->_scope_id and length $bf->_scope_id
defined $bf->_scope_id length $bf->_scope_id
Truth table33%1/3
A and B
A= defined $bf->_scope_idB= length $bf->_scope_id
ABresult
0X0
100
111
105 0 $c->stash->{'bf.auto_init_done'} = 1;
106 0 return;
107 }
108 8 $c->stash->{'bf.auto_init_done'} = 1;
109  
110 8 $bf->_scope_id($template . '_' . BarefootJS::scope_id_suffix());
111 8 $bf->register_components_from_manifest($m);
112  
113 # Seed each ssrDefault into the stash unless the caller has already
114 # supplied a value for that key — callers always win. Resolved
115 # through the production `BarefootJS::_derive_stash_from_defaults`
116 # (the same call `register_components_from_manifest` above makes for
117 # child components) so an aliased destructured prop's CALLER-facing
118 # key (`propName`, e.g. `n` for `{ n: count }`) is honoured — the
119 # caller stash IS the props document for a top-level render (a route
120 # handler sets `$c->stash(n => 5)` before `$c->render(...)`).
121 # Resolving `$d->{value}` directly, ignoring `propName`, was the
122 # #2524 production bug: it could never see a caller-supplied `n`,
123 # only ever the static default.
124 #
125 # CONSTRAINT (documented, not fixed): because the caller stash IS
126 # the props document here, `propName` resolution also sees every
127 # key Mojolicious itself already populates on `$c->stash` before
128 # this hook runs — `action`, `controller`, `template`,
129 # `template_class`, and friends (Mojolicious::Controller's own
130 # reserved stash keys). A ROOT-level component whose destructured
131 # prop happens to alias FROM one of those names (e.g. `{ action:
132 # label }`) resolves `propName => 'action'` against Mojo's OWN
133 # internal value, not a genuinely absent prop — the component
134 # silently picks up the current route's action name instead of its
135 # static default. This can't collide for a CHILD render
136 # (`register_components_from_manifest`'s renderer closure passes a
137 # fresh, purpose-built props hash, never the live `$c->stash`), only
138 # for a root-level render where the stash doubles as both Mojo's
139 # own bookkeeping and the props document. Avoid destructuring a prop
140 # named `action`/`controller`/`template`/`template_class`/etc. on a
141 # ROOT component if this matters to you.
142 8 my $defaults = $entry->{ssrDefaults};
143 8 ▶ if (ref($defaults) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $defaults eq "HASH") 8 0 8
144 8 my %extra = BarefootJS::_derive_stash_from_defaults($defaults, $c->stash);
145 8 for my $name (keys %extra) {
146 # A stash value the caller set wins -- `undef` (JS `null`)
147 # included -- unless it is JS `undefined`, which takes the
148 # seed (its default, or `undefined`; decision 6 of
149 # spec/js-undefined-value.md).
150 next if exists $c->stash->{$name}
151 10 ▶ && !BarefootJS::Undefined::is_undefined($c->stash->{$name});
Branch: 1 branch
BranchTrueFalseTotal
if exists $c->stash->{$name} and not BarefootJS::Undefined::is_undefined($c->stash->{$name}) 2 8 10
Condition66%2/3
exists $c->stash->{$name} and not BarefootJS::Undefined::is_undefined($c->stash->{$name})
!ll&&!rl&&r
802
MC/DC50%1/2
exists $c->stash->{$name} and not BarefootJS::Undefined::is_undefined($c->stash->{$name})
exists $c->stash->{$name} not BarefootJS::Undefined::is_undefined($c->stash->{$name})
Truth table66%2/3
A and not(B)
A= exists $c->stash->{$name}B= not BarefootJS::Undefined::is_undefined($c->stash->{$name})
ABresult
0X0
100
111
152 8 $c->stash->{$name} = $extra{$name};
153 }
154 }
155  
156 # (#1922) Seed the request-scoped `searchParams()` reader as the
157 # `$searchParams` template var, built from the live request query —
158 # so `searchParams().get(k)` resolves the current query during SSR
159 # (the client re-reads window.location on hydration). A caller that
160 # set it by hand wins (`//=`). Harmless for components that never read
161 # it; the var simply goes unused. `$bf->search_params` lazy-loads the
162 # reader class, so the plugin needn't `use` it directly.
163 $c->stash->{searchParams} //=
164 8 ▶ $bf->search_params($c->req->query_params->to_string);
Condition66%2/3
$c->stash->{"searchParams"} //= $bf->search_params($c->req->query_params->to_string)
l!l&&r!l&&!r
170
MC/DC0%0/2
$c->stash->{"searchParams"} //= $bf->search_params($c->req->query_params->to_string)
$c->stash->{"searchParams"} $bf->search_params($c->req->query_params->to_string)
Truth table66%2/3
A //= B
A= $c->stash->{"searchParams"}B= $bf->search_params($c->req->query_params->to_string)
ABresult
1X1
011
000
165 8 });
166 }
167  
168 2 1;
169 __END__
↑ Summary Mojolicious/Plugin/BarefootJS/DevReload.pm »