Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
05849ab
Switch to microbundle
daun Aug 29, 2022
f2eaca2
Upgrade prettier
daun Aug 29, 2022
dd0129e
Simplify build script
daun Aug 29, 2022
954f1ec
Create commands for bundling and linting
daun Aug 30, 2022
8b2f341
Create simple abstraction around CLI
daun Aug 30, 2022
4395741
Fix CLI errors
daun Aug 30, 2022
e1ffc91
Add readme
daun Dec 6, 2022
84392fe
Formatting
daun Dec 6, 2022
4eb79cd
Move shelljs into dependencies
daun Dec 6, 2022
3cceba8
Update build CLI interface
daun Jan 6, 2023
a069f56
Implement bundle version check
daun Jan 6, 2023
b6ea571
Fix package info import and clean up CLI
daun Jan 6, 2023
52fa329
Reorganize CLI folder
daun Jan 7, 2023
ef75677
Reorganize CLI bin folder
daun Jan 8, 2023
22a0e2f
Remove microbundle JS integration
daun Jan 8, 2023
b69ffa7
Clean up check command
daun Jan 8, 2023
2c8d181
Remove mjs files from prettier command
daun Jan 8, 2023
8489dc1
Simplify scripts
daun Jan 8, 2023
64d6d46
Switch to prepublishOnly
daun Jan 8, 2023
6150b57
Make sure prettier config is installed for users
daun Jan 8, 2023
512e6c7
Define swup peer dependency
daun Jan 8, 2023
3380d68
Clean up version check code
daun Jan 8, 2023
88c1c9d
Update readme
daun Jan 8, 2023
a50816c
Add changelog
daun Jan 8, 2023
9a10abc
Rename _checkRequirements
daun Jan 8, 2023
cf8ef58
Add update and publish workflows
daun Jan 8, 2023
0a34906
Rename readme file
daun Jan 8, 2023
accfd27
Update version
daun Jan 8, 2023
a4716ce
Fix check return type
daun Jan 8, 2023
41de11a
Improve version mismatch output
daun Jan 9, 2023
4bed4e0
Update npm dependencies
daun Jan 9, 2023
fa7ec84
Release
daun Jan 11, 2023
0a3412a
Pin swup dependency
daun Jan 16, 2023
4c74726
Generate lockfile
daun Jan 16, 2023
8f9fd3c
Update to latest swup release
daun Jan 16, 2023
82420cc
Separate lint and format scripts
daun Jan 16, 2023
caf48c2
Remove peer dependency
daun Jan 17, 2023
b723c95
Bump version
daun Jan 17, 2023
1844014
Convert to TypeScript (WIP)
daun Jan 17, 2023
a4f4203
Apply swup types
daun Jan 17, 2023
e6804aa
Override findPlugin return type
daun Jan 17, 2023
f7ffa4e
use Plugin type from swup and check for name being defined
gmrchk Jan 18, 2023
5022b59
revert defining of isSwupPlugin on the Plugin class
gmrchk Jan 18, 2023
c773340
revert defining of isSwupPlugin on the Plugin class
gmrchk Jan 18, 2023
258e950
Merge pull request #14 from swup/typescript
daun Jan 18, 2023
2cf8f45
Bump version
daun Jan 18, 2023
d876c57
Stable channel version
daun Jan 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This workflow publishes a package to NPM

name: Publish package

on:
release:
types: [published]

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
35 changes: 35 additions & 0 deletions .github/workflows/version-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This workflow bumps the package.json version and creates a PR

name: Update package version

on:
workflow_dispatch:
inputs:
segment:
description: 'Semver segment to update'
required: true
default: 'patch'
type: choice
options:
- minor
- patch

jobs:
update-version:
name: Update package version
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- run: echo "Updating ${{ inputs.segment }} version"
- name: Update version
run: npm --no-git-tag-version version ${{ inputs.segment }}
- uses: peter-evans/create-pull-request@v4
with:
base: 'master'
branch: 'version/automated'
title: 'Update package version (automated)'
commit-message: 'Update package version'
body: 'Automated update to ${{ inputs.segment }} version'
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ wiki-wishlist
*.sublime-workspace
.editorconfig
.idea
lib
/plugins
/lib
/dist
/plugins
6 changes: 0 additions & 6 deletions .npmignore

This file was deleted.

13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

<!-- ## [Unreleased] -->

## [2.0.0] - 2023-01

- Allow plugins to specify swup version requirements
- Switch to microbundle for bundling
- Provide a simple CLI for plugins to bundle and lint files

[Unreleased]: https://github.com/swup/plugin/compare/2.0.0...HEAD

[2.0.0]: https://github.com/swup/plugin/releases/tag/2.0.0
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Swup Base Plugin

Base class for creating swup plugins.

## Creating a Plugin

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.

## Usage

```js
import Plugin from '@swup/plugin';

export default class PluginName extends Plugin {
name = 'PluginName';
mount() {}
unmount() {}
}
```

or alternatively with TS

```ts
import Plugin, { PluginType } from '@swup/plugin';

export default class PluginName extends Plugin implements PluginType {
name = 'PluginName';
mount() {}
unmount() {}
}
```


## Commands

The base plugin provides a few simple command line tools to help with bundling and linting.

### Bundling

Bundle the plugin for production using [microbundle](https://github.com/developit/microbundle), creating ESM and UMD builds.

```bash
# Bundle and transpile plugin code
swup-plugin bundle

# Bundle plugin code in watch mode
swup-plugin dev
```

### Linting & formatting

Lint the plugin code using [prettier](https://prettier.io/) and swup's recommended rules.

```bash
# Lint plugin code
swup-plugin lint

# Fix and format any lint errors
swup-plugin format
```

### Package info

Check that the plugin's package.json file contains the required information for microbundle: input, output, export map, amd name, etc.

```bash
# Check plugin package info
swup-plugin check
```
8 changes: 8 additions & 0 deletions bin/commands/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { exec, echo } from '../lib/shell.js';

export default async function build() {
echo('Bundling plugin with microbundle');
exec('microbundle -f modern,esm,cjs');
exec('microbundle -f umd --external none');
return true;
};
52 changes: 52 additions & 0 deletions bin/commands/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { echo, error } from '../lib/shell.js';
import { resolve } from '../lib/helpers.js';

export default async function check() {
const pkg = await loadPluginPackageInfo();

echo(`Checking package info of ${pkg.name}`);

const { errors } = checkPluginPackageInfo(pkg);

if (errors.length) {
error(errors);
return false;
} else {
echo('package.json looks good!');
return true;
}
};

function checkPluginPackageInfo(pkg) {
const errors = [];

if (!pkg.amdName) {
errors.push('package.json missing `amdName` property');
}
if (pkg.type !== 'module') {
errors.push('package.json `type` property must be "module"');
}
if (!pkg.source) {
errors.push('package.json missing `source` property');
}
if (!pkg.main) {
errors.push('package.json missing `main` property');
}
if (!pkg.module) {
errors.push('package.json missing `module` property');
}
if (!pkg.unpkg) {
errors.push('package.json missing `unpkg` property');
}
if (!pkg.exports) {
errors.push('package.json missing `exports` property');
}

return { errors };
}

async function loadPluginPackageInfo() {
const importPath = resolve('package.json');
const { default: pkg } = await import(importPath, { assert: { type: 'json' } });
return pkg;
}
7 changes: 7 additions & 0 deletions bin/commands/dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { exec, echo } from '../lib/shell.js';

export default async function build() {
echo('Run plugin in dev mode');
exec('microbundle -w');
return true;
};
7 changes: 7 additions & 0 deletions bin/commands/format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { exec, echo } from '../lib/shell.js';

export default async function format() {
echo('Formatting with prettier');
exec('prettier src/**/*.js --write');
return true;
};
7 changes: 7 additions & 0 deletions bin/commands/lint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { exec, echo } from '../lib/shell.js';

export default async function lint() {
echo('Linting with prettier');
exec('prettier src/**/*.js --check');
return true;
};
12 changes: 12 additions & 0 deletions bin/lib/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path from 'path';
import fs from 'fs';

const appDirectory = fs.realpathSync(process.cwd());

export function resolve(relativePath = '') {
if (relativePath) {
return path.resolve(appDirectory, relativePath);
} else {
return appDirectory;
}
}
21 changes: 21 additions & 0 deletions bin/lib/shell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import shell from 'shelljs';
import live from 'shelljs-live';
import chalk from 'chalk';

export function exec(command) {
return live(command);
// return shell.exec(command);
}

export function echo(message) {
console.log(chalk.gray('[swup]'), message);
}

export function error(message) {
if (Array.isArray(message)) {
message.forEach(msg => error(msg));
} else {
console.log(chalk.red.bold(message));
}
shell.exit(1);
}
35 changes: 35 additions & 0 deletions bin/swup-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env node

import build from './commands/build.js';
import check from './commands/check.js';
import dev from './commands/dev.js';
import lint from './commands/lint.js';
import format from './commands/format.js';

import { error } from './lib/shell.js';

const commands = {
build: async () => (await check()) && (await build()),
dev: async () => (await check()) && (await dev()),
lint,
format,
check,
};

async function main() {
const args = process.argv.slice(2);

const command = args[0] || '';
if (!command) {
return error(`Missing command (available: ${Object.keys(commands).join(', ')}`);
}

const handler = commands[command];
if (!handler) {
return error(`Unknown command: ${args.join(' ')}`);
}

await handler(args);
}

main();
Loading