Skip to content

Commit d9086cc

Browse files
authored
Merge pull request #13 from swup/next
Compatibility with swup 3
2 parents 1753b6a + d876c57 commit d9086cc

26 files changed

Lines changed: 9173 additions & 6304 deletions

.github/workflows/npm-publish.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This workflow publishes a package to NPM
2+
3+
name: Publish package
4+
5+
on:
6+
release:
7+
types: [published]
8+
9+
jobs:
10+
publish-npm:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- uses: actions/setup-node@v1
15+
with:
16+
node-version: 12
17+
registry-url: https://registry.npmjs.org/
18+
- run: npm ci
19+
- run: npm publish --access public
20+
env:
21+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This workflow bumps the package.json version and creates a PR
2+
3+
name: Update package version
4+
5+
on:
6+
workflow_dispatch:
7+
inputs:
8+
segment:
9+
description: 'Semver segment to update'
10+
required: true
11+
default: 'patch'
12+
type: choice
13+
options:
14+
- minor
15+
- patch
16+
17+
jobs:
18+
update-version:
19+
name: Update package version
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v2
23+
- uses: actions/setup-node@v2
24+
with:
25+
node-version: 16
26+
- run: echo "Updating ${{ inputs.segment }} version"
27+
- name: Update version
28+
run: npm --no-git-tag-version version ${{ inputs.segment }}
29+
- uses: peter-evans/create-pull-request@v4
30+
with:
31+
base: 'master'
32+
branch: 'version/automated'
33+
title: 'Update package version (automated)'
34+
commit-message: 'Update package version'
35+
body: 'Automated update to ${{ inputs.segment }} version'

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ wiki-wishlist
1313
*.sublime-workspace
1414
.editorconfig
1515
.idea
16-
lib
17-
/plugins
16+
/lib
17+
/dist
18+
/plugins

.npmignore

Lines changed: 0 additions & 6 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
3+
<!-- ## [Unreleased] -->
4+
5+
## [2.0.0] - 2023-01
6+
7+
- Allow plugins to specify swup version requirements
8+
- Switch to microbundle for bundling
9+
- Provide a simple CLI for plugins to bundle and lint files
10+
11+
[Unreleased]: https://github.com/swup/plugin/compare/2.0.0...HEAD
12+
13+
[2.0.0]: https://github.com/swup/plugin/releases/tag/2.0.0

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Swup Base Plugin
2+
3+
Base class for creating swup 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). It comes with detailed instructions and the required tooling.
8+
9+
## Usage
10+
11+
```js
12+
import Plugin from '@swup/plugin';
13+
14+
export default class PluginName extends Plugin {
15+
name = 'PluginName';
16+
mount() {}
17+
unmount() {}
18+
}
19+
```
20+
21+
or alternatively with TS
22+
23+
```ts
24+
import Plugin, { PluginType } from '@swup/plugin';
25+
26+
export default class PluginName extends Plugin implements PluginType {
27+
name = 'PluginName';
28+
mount() {}
29+
unmount() {}
30+
}
31+
```
32+
33+
34+
## Commands
35+
36+
The base plugin provides a few simple command line tools to help with bundling and linting.
37+
38+
### Bundling
39+
40+
Bundle the plugin for production using [microbundle](https://github.com/developit/microbundle), creating ESM and UMD builds.
41+
42+
```bash
43+
# Bundle and transpile plugin code
44+
swup-plugin bundle
45+
46+
# Bundle plugin code in watch mode
47+
swup-plugin dev
48+
```
49+
50+
### Linting & formatting
51+
52+
Lint the plugin code using [prettier](https://prettier.io/) and swup's recommended rules.
53+
54+
```bash
55+
# Lint plugin code
56+
swup-plugin lint
57+
58+
# Fix and format any lint errors
59+
swup-plugin format
60+
```
61+
62+
### Package info
63+
64+
Check that the plugin's package.json file contains the required information for microbundle: input, output, export map, amd name, etc.
65+
66+
```bash
67+
# Check plugin package info
68+
swup-plugin check
69+
```

bin/commands/build.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { exec, echo } from '../lib/shell.js';
2+
3+
export default async function build() {
4+
echo('Bundling plugin with microbundle');
5+
exec('microbundle -f modern,esm,cjs');
6+
exec('microbundle -f umd --external none');
7+
return true;
8+
};

bin/commands/check.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { echo, error } from '../lib/shell.js';
2+
import { resolve } from '../lib/helpers.js';
3+
4+
export default async function check() {
5+
const pkg = await loadPluginPackageInfo();
6+
7+
echo(`Checking package info of ${pkg.name}`);
8+
9+
const { errors } = checkPluginPackageInfo(pkg);
10+
11+
if (errors.length) {
12+
error(errors);
13+
return false;
14+
} else {
15+
echo('package.json looks good!');
16+
return true;
17+
}
18+
};
19+
20+
function checkPluginPackageInfo(pkg) {
21+
const errors = [];
22+
23+
if (!pkg.amdName) {
24+
errors.push('package.json missing `amdName` property');
25+
}
26+
if (pkg.type !== 'module') {
27+
errors.push('package.json `type` property must be "module"');
28+
}
29+
if (!pkg.source) {
30+
errors.push('package.json missing `source` property');
31+
}
32+
if (!pkg.main) {
33+
errors.push('package.json missing `main` property');
34+
}
35+
if (!pkg.module) {
36+
errors.push('package.json missing `module` property');
37+
}
38+
if (!pkg.unpkg) {
39+
errors.push('package.json missing `unpkg` property');
40+
}
41+
if (!pkg.exports) {
42+
errors.push('package.json missing `exports` property');
43+
}
44+
45+
return { errors };
46+
}
47+
48+
async function loadPluginPackageInfo() {
49+
const importPath = resolve('package.json');
50+
const { default: pkg } = await import(importPath, { assert: { type: 'json' } });
51+
return pkg;
52+
}

bin/commands/dev.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { exec, echo } from '../lib/shell.js';
2+
3+
export default async function build() {
4+
echo('Run plugin in dev mode');
5+
exec('microbundle -w');
6+
return true;
7+
};

bin/commands/format.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { exec, echo } from '../lib/shell.js';
2+
3+
export default async function format() {
4+
echo('Formatting with prettier');
5+
exec('prettier src/**/*.js --write');
6+
return true;
7+
};

0 commit comments

Comments
 (0)