From 7113d55fd043bc878e2934f799d78598bf62e074 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Sat, 6 Jan 2024 19:46:03 -0300 Subject: [PATCH] Always download vendored dependencies --- README.md | 24 ++++++------------------ src/Commands/PinCommand.php | 29 +++++++++-------------------- src/Commands/UnpinCommand.php | 9 ++------- 3 files changed, 17 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 0e50894..1b46475 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ In a nutshell, importmap works by giving the browser a map of where to look for use Tonysm\ImportmapLaravel\Facades\Importmap; // Other pins... -Importmap::pin("alpinejs", to: "https://ga.jspm.io/npm:alpinejs@3.8.1/dist/module.esm.js"); +Importmap::pin("alpinejs", to: "/js/vendor/alpinejs.js"); //@3.8.1 ``` Then, in your JavaScript files you can safely do: @@ -137,33 +137,21 @@ If you depend on any external library you can use the `importmap:pin` command to php artisan importmap:pin alpinejs ``` -That will add the following line to your `routes/importmap.php` file: +It will download the `alpinejs` lib, then it will add the following line to your `routes/importmap.php` file: ```php -Importmap::pin("alpinejs", to: "https://ga.jspm.io/npm:alpinejs@3.8.1/dist/module.esm.js"); +Importmap::pin("alpinejs", to: "/js/vendor/alpinejs.js"); // @3.8.1 ``` -The `pin` command makes use of the jspm.io API to resolve the dependencies (and the dependencies of our dependencies), looking for ESM modules that we can pin, and resolving it to a CDN URL. We can control the CDN we want to use by specifying the `--from` flag like so: +The `pin` command makes use of the jspm.io API to resolve the dependencies (and the dependencies of our dependencies), looking for ESM modules that we can pin, and resolving it to a CDN URL and downloading as a dependency. We can control the CDN we want to use by specifying the `--from` flag like so: ```bash php artisan importmap:pin alpinejs --from=unpkg ``` -Which should generate a pin like so: - -```php -Importmap::pin("alpinejs", to: "https://unpkg.com/alpinejs@3.8.1/dist/module.esm.js"); -``` - -It's preferred that you always pin from the same CDN, because then your browser will reuse the same SSL handshake when downloading the files (which means they will be downloaded faster). - -Alternatively to using CDNs, you may prefer to vendor the libraries yourself, which you can do by using the `--download` flag, like so: - -```bash -php artisan importmap:pin alpinejs --download -``` +This will download the lib from unpkg instead of the default JSPM CDN. -This will resolve the dependencies (and the dependencies of our dependencies) and download all the files to your `resources/js/vendor` folder, which you should add to your version control and maintain yourself. The pin will look like this: +It's important to note that the `pin` command will always resolve the dependencies (and the dependencies of our dependencies) and download all the files to your `resources/js/vendor` folder, which you should add to your version control and vendor it yourself. The pin will look like this: ```php Importmap::pin("alpinejs", to: "/js/vendor/alpinejs.js"); // @3.8.1 diff --git a/src/Commands/PinCommand.php b/src/Commands/PinCommand.php index bb1d1a0..624be23 100644 --- a/src/Commands/PinCommand.php +++ b/src/Commands/PinCommand.php @@ -18,7 +18,6 @@ class PinCommand extends Command protected $signature = ' importmap:pin {--from-env=production : The CDN environment} - {--download : If used, the dependency will be downloaded to be checked in under version control instead of relying on CDNs.} {--from=jspm : The CDN.} {packages*} '; @@ -55,27 +54,17 @@ public function handle(Packager $packager) private function importPackages(Packager $packager, Collection $imports): void { $imports->each(function (string $url, string $package) use ($packager) { - if ($this->option('download')) { - $this->info(sprintf( - 'Pinning "%s" to %s/%s.js via download from %s', - $package, - $packager->vendorPath, - $package, - $url, - )); + $this->info(sprintf( + 'Pinning "%s" to %s/%s.js via download from %s', + $package, + $packager->vendorPath, + $package, + $url, + )); - $packager->download($package, $url); + $packager->download($package, $url); - $pin = $packager->vendoredPinFor($package, $url); - } else { - $this->info(sprintf( - 'Pinning "%s" to %s', - $package, - $url, - )); - - $pin = $packager->pinFor($package, $url); - } + $pin = $packager->vendoredPinFor($package, $url); if ($packager->packaged($package)) { // Replace existing pin... diff --git a/src/Commands/UnpinCommand.php b/src/Commands/UnpinCommand.php index 39971a6..bad6883 100644 --- a/src/Commands/UnpinCommand.php +++ b/src/Commands/UnpinCommand.php @@ -16,7 +16,6 @@ class UnpinCommand extends Command protected $signature = ' importmap:unpin {--from-env=production : The CDN environment} - {--download : If used, the dependency will be downloaded to be checked in under version control instead of relying on CDNs.} {--from=jspm : The CDN.} {packages*} '; @@ -38,13 +37,9 @@ public function handle(Packager $packager) $packages = Arr::wrap($this->argument('packages')); if ($imports = $packager->import($packages, $this->option('from-env'), $this->option('from'))) { - $imports->each(function (string $url, string $package) use ($packager) { + $imports->each(function (string $_url, string $package) use ($packager) { if ($packager->packaged($package)) { - if ($this->option('download')) { - $this->info(sprintf('Unpinning and removing "%s"', $package)); - } else { - $this->info(sprintf('Unpinning "%s"', $package)); - } + $this->info(sprintf('Unpinning and removing "%s"', $package)); $packager->remove($package); }