https://github.com/wix-incubator/resolve-unpkg
Keep your UNPKG links up to date!
https://github.com/wix-incubator/resolve-unpkg
cli unpkg unpkg-links
Last synced: about 1 year ago
JSON representation
Keep your UNPKG links up to date!
- Host: GitHub
- URL: https://github.com/wix-incubator/resolve-unpkg
- Owner: wix-incubator
- License: mit
- Created: 2018-02-22T18:34:58.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-03-20T06:46:59.000Z (about 3 years ago)
- Last Synced: 2025-04-14T18:09:11.287Z (about 1 year ago)
- Topics: cli, unpkg, unpkg-links
- Language: TypeScript
- Homepage:
- Size: 30.3 KB
- Stars: 5
- Watchers: 160
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/wix-incubator/resolve-unpkg) [](https://github.com/semantic-release/semantic-release)
The **Resolve-Unpkg** library helps you to keep your unpkg links up to date by patching the unpkg URLs with the package version installed in your environment. The **Resolve-Unpkg** may be used in command line and as a webloader.
**TL;DR;**
> In CLI:
> ```bash
> npm i --save-dev resolve-unpkg
> resolve-unpkg path-to-optional-config.json
> ```
>
> Webloader:
> ```javascript
> import scripts from '!resolve-unpkg?unpkgPrefix=unpkg.com&versionPlaceholder=a.b.c&onlyByVersionPlaceholder=true!scripts.json';
> ```
- [Import](#import)
- [Configuration](#configuration)
- [Usage in CLI](#usage-in-cli)
- [Usage in JS](#usage-in-js)
# Import
To add **Resolve-Unpkg** to the project, `cd` to the project root folder and run:
`npm i --save-dev resolve-unpkg`
This will add **Resolve-Unpkg** as a dependency to *package.json* enabling you to run the `resolve-unpkg` command manually and use resolve-unpkg as a webpack loader to resolve versions in unpkg URLs when they are imported from the JSON file.
# Configuration
By default, **Resolve-Unpkg** processes URLs with `unpkg.com` prefix in the *index.html* file and replaces the *x.x.x* version placeholders with the actual version of the packages that are installed in your local environment.
To customize the default configuration, use the following options:
| Property | Type | Required | Description | CLI | Loader |
| ---------------- | ---------- | :------: | ---------------------------------------- | :-: | :----: |
| `unpkgPrefix` | `string` | No | The prefix of the unpkg URLs. Default: `'unpkg.com'` | Yes | Yes |
| `versionPlaceholder`| `string` | No | A version number placeholder to be replaced when occurs in the position of package version. Default: `'x.x.x'` | Yes | Yes |
| `onlyByVersionPlaceholder` | `boolean` | No | A flag that may limit the update to those URLs that contain the versionPlaceholder (e.g. x.x.x); when set to `true`, the exact package version numbers, like 1.9.1, are ignored. Default: `true` | Yes | Yes |
| `files` | `string[]` | No | Path to file(s) to update/resolve URLs in. Default: `['index.html']` | Yes | No |
| `dist` | `string` | No | Folder where the updated file(s) should be saved. Default: overwrite original | Yes | No |
# Usage in CLI
With **Resolve-Unpkg**, you can automatically update the package versions that are mentioned in the unpkg URLs in some of your project files.
> The package versions used are those installed in your local environment.
Before you begin, prepare the resolve-unpkg configuration file to override the default options if necessary. For example:
**Sample Resolve-Unpkg configuration file**
```
{
"unpkgPrefix": "unpkg",
"files": ["index.vm, index.ejs"],
"dist": "dist/"
}
```
To update the unpkg links, run:
`resolve-unpkg [optional-config.json]`
As a result, the unpkg URLs in the provided files will be updated.
As an illustration to this process, let's consider the following file (`index.html`) :
```html
```
If the version of the `jquery` installed in the local environment is `'2.21'`, and `spectrum-colorpicker` is `'1.6.1'`, and we run the `resolve-unpkg` with default configuration parameters, the original `index.html` file will be overwritten with this one:
```html
```
Note that as the `onlyByVersionPlaceholder` is `true` by default, the older version for *spectrum-colorpicker* was not modified.
# Usage in JS
For the unpkg links that are imported into your JS code from the JSON file, use **resolve-unpkg** loader to inject the updated versions into the URLs. To do so, update the import and provide the necessary values as parameters:
```javscript
...
import scripts from '!resolve-unpkg?unpkgPrefix=unpkg.com&versionPlaceholder=a.b.c&onlyByVersionPlaceholder=true!scripts.json';
...
```
# Testing
Make sure to test the new version since every time your links are updated you are getting code that has not yet run through your tests. So if for example you use `resolve-unpkg` in your build phase, you should have your code tested afterwards (e.g. in E2E tests).