Skip to content

Commit

Permalink
Merge pull request #45 from tonysm/download-vendors
Browse files Browse the repository at this point in the history
Always download dependencies
  • Loading branch information
tonysm authored Jan 6, 2024
2 parents 69dadc7 + 7113d55 commit 3f173e1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 45 deletions.
24 changes: 6 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]/dist/module.esm.js");
Importmap::pin("alpinejs", to: "/js/vendor/alpinejs.js"); //@3.8.1
```

Then, in your JavaScript files you can safely do:
Expand Down Expand Up @@ -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:[email protected]/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/[email protected]/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
Expand Down
29 changes: 9 additions & 20 deletions src/Commands/PinCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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*}
';
Expand Down Expand Up @@ -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...
Expand Down
9 changes: 2 additions & 7 deletions src/Commands/UnpinCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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*}
';
Expand All @@ -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);
}
Expand Down

0 comments on commit 3f173e1

Please sign in to comment.