| 1 |
# Swup Base Plugin |
| 2 |
|
| 3 |
Base class for creating [swup](https://swup.js.org) plugins. |
| 4 |
|
| 5 |
## Creating a Plugin |
| 6 |
|
| 7 |
To create a new plugin, use the official [swup plugin template](https://github.com/swup/plugin-template). |
| 8 |
It comes with detailed instructions and the required tooling. |
| 9 |
|
| 10 |
## Usage |
| 11 |
|
| 12 |
Import the base class and extend your plugin from it. |
| 13 |
|
| 14 |
```js |
| 15 |
import Plugin from '@swup/plugin'; |
| 16 |
|
| 17 |
export default class PluginName extends Plugin { |
| 18 |
name = 'PluginName'; |
| 19 |
mount() {} |
| 20 |
unmount() {} |
| 21 |
} |
| 22 |
``` |
| 23 |
|
| 24 |
## Development |
| 25 |
|
| 26 |
The official [swup command-line interface](https://github.com/swup/cli) has commands to help with |
| 27 |
bundling and linting plugins. |
| 28 |
|
| 29 |
```sh |
| 30 |
npm install --save-dev @swup/cli |
| 31 |
``` |
| 32 |
|
| 33 |
### Bundling |
| 34 |
|
| 35 |
Bundle the plugin for production using [microbundle](https://github.com/developit/microbundle), |
| 36 |
creating ESM and UMD builds. |
| 37 |
|
| 38 |
```bash |
| 39 |
# Bundle and transpile plugin code for distribution |
| 40 |
swup package:bundle |
| 41 |
|
| 42 |
# Bundle plugin code in watch mode |
| 43 |
swup package:dev |
| 44 |
``` |
| 45 |
|
| 46 |
### Linting & formatting |
| 47 |
|
| 48 |
Lint the plugin code using [prettier](https://prettier.io/) and swup's recommended rules. |
| 49 |
|
| 50 |
```bash |
| 51 |
# Lint plugin code |
| 52 |
swup package:lint |
| 53 |
|
| 54 |
# Fix and format any lint errors |
| 55 |
swup package:format |
| 56 |
``` |
| 57 |
|
| 58 |
### Package info |
| 59 |
|
| 60 |
Check that the plugin's package.json file contains the required information for microbundle: |
| 61 |
input, output, export map, amd name, etc. |
| 62 |
|
| 63 |
```bash |
| 64 |
# Check plugin package info |
| 65 |
swup package:check |
| 66 |
``` |