Mojo/Alien/rollup.pm

filter: Statementstmt 29.2% 19 / 65 Branchbran 0.0% 0 / 22 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 33.3% 5 / 15 Podpod 100% 2 / 2 totaltotal 24.1% 27 / 112 CC 14 SCAR 24.7
CC
14
Cov
22%
watch
30.0
_run
20.0
__ANON__
12.0
CRAP 11.8
« Mojo/Alien/npm.pm ↑ Summary Mojolicious/Command/Author/webpack.pm »
1 package Mojo::Alien::rollup;
2 2 ▶use Mojo::Base 'Mojo::Alien::webpack';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::File qw(path tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use constant DEBUG => $ENV{MOJO_ROLLUP_DEBUG} && 1;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_ROLLUP_DEBUG'} && 1
!ll
20
MC/DC0%0/1
$ENV{'MOJO_ROLLUP_DEBUG'} && 1
$ENV{'MOJO_ROLLUP_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_ROLLUP_DEBUG'}
Aresult
00
11
9  
10 0 ▶has binary => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
11 0 my $self = shift;
12 0 ▶ return $ENV{MOJO_ROLLUP_BINARY} if $ENV{MOJO_ROLLUP_BINARY};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOJO_ROLLUP_BINARY"} 0 0 0
13 0 my $bin = $self->config->to_abs->dirname->child(qw(node_modules .bin rollup));
14 0 $self->_d('%s %s', -e $bin ? 'Found' : 'Not installed', $bin) if DEBUG;
15 0 ▶ return -e $bin ? $bin->to_string : 'rollup';
Branch: 1 branch
BranchTrueFalseTotal
-e $bin ? : 0 0 0
16 2 };
17  
18 0 ▶has config => sub { path->to_abs->child('rollup.config.js') };
Subroutine __ANON__: not called CC=1 SCAR=1.2
19  
20 0 ▶has dependencies => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 return {
22 0 core => [qw(rollup @rollup/plugin-commonjs @rollup/plugin-node-resolve)],
23 css => [qw(cssnano postcss-preset-env rollup-plugin-postcss)],
24 eslint => [qw(@rollup/plugin-eslint)],
25 js => [qw(@babel/core @babel/preset-env @babel/plugin-transform-runtime @rollup/plugin-babel rollup-plugin-terser)],
26 sass => [qw(cssnano @csstools/postcss-sass postcss-preset-env rollup-plugin-postcss sass)],
27 svelte => [qw(rollup-plugin-svelte)],
28 };
29 2 };
30  
31 0 ▶sub exec {
Subroutine exec: not called CC=2 SCAR=17.9
Pod documented
32 0 my $self = shift;
33 0 my @cmd = ($self->_cmd_build, '--watch');
34 0 my $home = $self->config->dirname->to_string;
35  
36 0 ▶ chdir $home or die "Can't chdir to $home: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $home or die "Can't chdir to ${home}: $!" 0 0 0
37 0 $ENV{NODE_ENV} = $self->mode;
38 0 $ENV{ROLLUP_ASSETS_DIR} = $self->assets_dir->to_string;
39 0 $ENV{ROLLUP_OUT_DIR} = $self->out_dir->to_string;
40 0 $self->_d('(%s) cd %s && %s', $$, $home, join ' ', @_) if DEBUG;
41 0 { exec @cmd }
42 0 die "Can't exec @cmd: $!";
43 }
44  
45 0 ▶sub watch {
Subroutine watch: not called CC=5 SCAR=34.0
Pod documented
46 0 my $self = shift;
47 0 ▶ return $self if $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pid 0 0 0
48  
49 0 my $home = $self->config->dirname->to_string;
50 0 ▶ croak "Can't chdir $home: No such file or directory" unless -d $home;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $home 0 0 0
51  
52 0 my @cmd = ($self->_cmd_build, '--watch');
53 0 ▶ croak "Can't fork: $!" unless defined(my $pid = fork);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $pid = fork) 0 0 0
54 0 ▶ return $self if $self->{pid} = $pid; # Parent
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pid"} = $pid 0 0 0
55 0 return $self->exec; # Child
56 }
57  
58 0 ▶sub _cmd_build {
Subroutine _cmd_build: not called CC=3 SCAR=24.8
59 0 my $self = shift;
60 0 $self->init;
61  
62 0 my @cmd = ($self->binary);
63 0 ▶ croak "Can't run $cmd[0]" unless -x $cmd[0];
Branch: 1 branch
BranchTrueFalseTotal
unless -x $cmd[0] 0 0 0
64  
65 0 ▶ $self->{basename} ||= path($cmd[0])->basename;
Condition0%0/3
$self->{"basename"} ||= path($cmd[0])->basename
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"basename"} ||= path($cmd[0])->basename
$self->{"basename"} path($cmd[0])->basename
Truth table0%0/3
A ||= B
A= $self->{"basename"}B= path($cmd[0])->basename
ABresult
1X1
011
000
66 0 push @cmd, '--config' => $self->config->to_string;
67 0 return @cmd;
68 }
69  
70 0 ▶sub _config_include_dir { shift->assets_dir->child('rollup.config.d') }
Subroutine _config_include_dir: not called CC=1 SCAR=6.9
71 0 ▶sub _config_template_name {'rollup.config.js'}
Subroutine _config_template_name: not called CC=1 SCAR=6.9
72 0 ▶sub _d { my ($class, $format) = (shift, shift); warn sprintf "[Rollup] $format\n", @_ }
Subroutine _d: not called CC=1 SCAR=6.9
73  
74 0 ▶sub _run {
Subroutine _run: not called CC=4 SCAR=30.0
75 0 my ($self, @cmd) = @_;
76 0 local $CWD = $self->config->dirname->to_string;
77 0 local $ENV{NODE_ENV} = $self->mode;
78 0 local $ENV{ROLLUP_ASSETS_DIR} = $self->assets_dir->to_string;
79 0 local $ENV{ROLLUP_OUT_DIR} = $self->out_dir->to_string;
80 0 $self->_d('cd %s && %s', $CWD, join ' ', @cmd) if DEBUG;
81 0 ▶ open my $ROLLUP, '-|', @cmd or die "Can't run @cmd: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $ROLLUP, "-|", @cmd or die "Can't run @{cmd}: $!" 0 0 0
82 0 ▶ return $ROLLUP if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 0 0
83 0 ▶ DEBUG && print while <$ROLLUP>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $ROLLUP) and do { undef } 0 0 0
84 }
85  
86 2 1;
87  
88 =encoding utf8
89  
90 =head1 NAME
91  
92 Mojo::Alien::rollup - Runs the external nodejs program rollup
93  
94 =head1 SYNOPSIS
95  
96 use Mojo::Alien::rollup;
97 my $rollup = Mojo::Alien::rollup->new;
98  
99 # Run once
100 $rollup->build;
101  
102 # Build when rollup see files change
103 $rollup->watch
104  
105 =head1 DESCRIPTION
106  
107 L<Mojo::Alien::rollup> is a class for runnig the external nodejs program
108 L<rollup|https://rollupjs.org/>.
109  
110 =head1 ATTRIBUTES
111  
112 =head2 assets_dir
113  
114 See L<Mojo::Alien::webpack/assets_dir>.
115  
116 =head2 binary
117  
118 $array_ref = $rollup->binary;
119 $rollup = $rollup->binary('rollup');
120  
121 The path to the rollup executable. Defaults to C<MOJO_ROLLUP_BINARY>
122 environment variable, or "rollup" inside "./node_modules". Fallback to just
123 "rollup".
124  
125 =head2 config
126  
127 $path = $rollup->config;
128 $rollup = $rollup->config(path->to_abs->child('rollup.config.js'));
129  
130 Holds an I</absolute> path to
131 L<rollup.config.js|https://rollup.js.org/concepts/configuration/>.
132  
133 =head2 dependencies
134  
135 $hash_ref = $rollup->dependencies;
136  
137 A hash where the keys can match the items in L</include> and the values are
138 lists of packages to install. Keys that does I</not> match items in L</include>
139 will be ignored. This attribute will be used by L</init>.
140  
141 These dependencies are predefined:
142  
143 core | rollup @rollup/plugin-commonjs @rollup/plugin-node-resolve
144 css | cssnano postcss-preset-env rollup-plugin-postcss
145 eslint | @rollup-plugin-eslint
146 js | @babel/core @babel/preset-env @rollup/plugin-babel rollup-plugin-terser
147 sass | cssnano @csstools/postcss-sass postcss-preset-env rollup-plugin-postcss sass
148 svelte | rollup-plugin-svelte
149  
150 =head2 include
151  
152 See L<Mojo::Alien::webpack/include>.
153  
154 =head2 mode
155  
156 See L<Mojo::Alien::webpack/mode>.
157  
158 =head2 npm
159  
160 See L<Mojo::Alien::webpack/npm>.
161  
162 =head2 out_dir
163  
164 See L<Mojo::Alien::webpack/out_dir>.
165  
166 =head1 METHODS
167  
168 =head2 asset_map
169  
170 $hash_ref = $rollup->asset_map;
171  
172 Parses the filenames in L</out_dir> and returns a hash ref with information
173 about the generated assets. Example return value:
174  
175 {
176 'entry-name.js' => '/path/to/entry-name.development.js',
177 'cool-beans.png' => /path/to/f47352684211060f3e34.png',
178 }
179  
180 Note that this method is currently EXPERIMENTAL.
181  
182 =head2 build
183  
184 See L<Mojo::Alien::webpack/build>.
185  
186 =head2 exec
187  
188 See L<Mojo::Alien::webpack/exec>.
189  
190 =head2 init
191  
192 See L<Mojo::Alien::webpack/init>.
193  
194 =head2 pid
195  
196 See L<Mojo::Alien::webpack/pid>.
197  
198 =head2 stop
199  
200 See L<Mojo::Alien::webpack/stop>.
201  
202 =head2 watch
203  
204 See L<Mojo::Alien::webpack/watch>.
205  
206 =head1 SEE ALSO
207  
208 L<Mojolicious::Plugin::Webpack> and L<Mojo::Alien::webpack>.
209  
210 =cut
211  
212 __DATA__
« Mojo/Alien/npm.pm ↑ Summary Mojolicious/Command/Author/webpack.pm »