Skip to content

Instantly share code, notes, and snippets.

@nylen
Last active August 3, 2017 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nylen/e9bcda0ee6c2c21e9f0063d9cdebd7e9 to your computer and use it in GitHub Desktop.
Save nylen/e9bcda0ee6c2c21e9f0063d9cdebd7e9 to your computer and use it in GitHub Desktop.
List JS dependencies of the WP Gutenberg plugin and suggest a load order
#!/usr/bin/env perl
use warnings;
use strict;
use File::Basename;
chdir dirname $0;
if ( ! -f 'editor/index.js' ) {
chdir '..';
if ( ! -f 'editor/index.js' ) {
die "Put this script in the root or 'bin/' directory of your Gutenberg installation.";
}
}
my $files = qx( git ls-files );
my %deps = ();
my $entry_points = 'blocks|components|date|editor|element|i18n|utils';
foreach my $file ( split /[\r\n]+/, $files ) {
next if $file !~ /^($entry_points)\/.*\.js$/;
my $entry_point = $1;
open JSFILE, '<', $file or die;
while ( <JSFILE> ) {
if ( /from '(\@wordpress\/)?($entry_points)[\/']/ ) {
my $imported_entry = $2;
if ( $entry_point ne $imported_entry ) {
$deps{ $entry_point }{ $imported_entry } = 1;
}
}
}
close JSFILE or die;
}
foreach my $dest ( sort keys %deps ) {
foreach my $source ( sort keys %{ $deps{ $dest } } ) {
print "'$dest' depends on '$source'\n";
}
}
print "\n";
my @loaded = ();
sub load_deps {
my $entry_point = shift;
my $stack = shift;
my $loaded = shift;
for my $ancestor ( @$stack ) {
if ( $entry_point eq $ancestor ) {
push @$stack, $entry_point;
my $msg = join ' -> ', @$stack;
die "Circular dependency: $msg\n";
}
}
my @new_stack = @$stack;
push @new_stack, $entry_point;
unshift @loaded, \@new_stack;
foreach my $dep ( reverse sort keys %{ $deps{ $entry_point } } ) {
load_deps( $dep, \@new_stack );
}
}
load_deps 'editor', [];
sub uniq {
my %seen;
return grep {
my $seen_this = $seen{ $_ };
$seen{ $_ } = 1;
! $seen_this;
} @_;
}
print "Suggested load order:\n\n";
foreach my $load ( uniq map { @$_[-1] } @loaded ) {
print "$load\n";
}
@nylen
Copy link
Author

nylen commented Jun 5, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment