@vite-pwa/vitepress 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Anthony Fu<https://github.com/antfu>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,85 @@
1
+ <p align='center'>
2
+ <img src='./hero.png' alt="@vite-pwa/vitepress - Zero-config PWA for VitePress"><br>
3
+ Zero-config PWA Plugin for VitePress
4
+ </p>
5
+
6
+ <p align='center'>
7
+ <a href='https://www.npmjs.com/package/vite-plugin-pwa' target="__blank">
8
+ <img src='https://img.shields.io/npm/v/vite-plugin-pwa?color=33A6B8&label=' alt="NPM version">
9
+ </a>
10
+ <a href="https://www.npmjs.com/package/vite-plugin-pwa" target="__blank">
11
+ <img alt="NPM Downloads" src="https://img.shields.io/npm/dm/vite-plugin-pwa?color=476582&label=">
12
+ </a>
13
+ <a href="https://vite-pwa-org.netlify.app/frameworks/vitepress" target="__blank">
14
+ <img src="https://img.shields.io/static/v1?label=&message=docs%20%26%20guides&color=2e859c" alt="Docs & Guides">
15
+ </a>
16
+ <br>
17
+ <a href="https://github.com/antfu/vite-plugin-pwa" target="__blank">
18
+ <img alt="GitHub stars" src="https://img.shields.io/github/stars/antfu/vite-plugin-pwa?style=social">
19
+ </a>
20
+ </p>
21
+
22
+ <br>
23
+
24
+ <p align="center">
25
+ <a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg">
26
+ <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>
27
+ </a>
28
+ </p>
29
+
30
+
31
+ ## 🚀 Features
32
+
33
+ - 📖 [**Documentation & guides**](https://vite-pwa-org.netlify.app/)
34
+ - 👌 **Zero-Config**: sensible built-in default configs for common use cases
35
+ - 🔩 **Extensible**: expose the full ability to customize the behavior of the plugin
36
+ - 🦾 **Type Strong**: written in [TypeScript](https://www.typescriptlang.org/)
37
+ - 🔌 **Offline Support**: generate service worker with offline support (via Workbox)
38
+ - ⚡ **Fully tree shakable**: auto inject Web App Manifest
39
+ - 💬 **Prompt for new content**: built-in support for Vanilla JavaScript, Vue 3, React, Svelte, SolidJS and Preact
40
+ - ⚙️ **Stale-while-revalidate**: automatic reload when new content is available
41
+ - ✨ **Static assets handling**: configure static assets for offline support
42
+ - 🐞 **Development Support**: debug your custom service worker logic as you develop your application
43
+
44
+ ## 📦 Install
45
+
46
+ ```bash
47
+ npm i @vite-pwa/vitepress -D
48
+
49
+ # yarn
50
+ yarn add @vite-pwa/vitepress -D
51
+
52
+ # pnpm
53
+ pnpm add @vite-pwa/vitepress -D
54
+ ```
55
+
56
+ ## 🦄 Usage
57
+
58
+ You will need wrap your VitePress config with `withPwa`:
59
+
60
+ ```ts
61
+ // .vitepress/config.ts
62
+ import { defineConfig } from 'vitepress'
63
+ import { withPwa } from '@vite-pwa/vitepress'
64
+
65
+ export default withPwa(defineConfig({
66
+ /* your VitePress options */
67
+ /* Vite PWA Options */
68
+ pwa: {}
69
+ }))
70
+ ```
71
+
72
+ Read the [📖 documentation](https://vite-pwa-org.netlify.app/frameworks/vitepress) for a complete guide on how to configure and use
73
+ this plugin.
74
+
75
+ ## 👀 Full config
76
+
77
+ Check out the type declaration [src/types.ts](./src/types.ts) and the following links for more details.
78
+
79
+ - [Web app manifests](https://developer.mozilla.org/en-US/docs/Web/Manifest)
80
+ - [Workbox](https://developers.google.com/web/tools/workbox)
81
+
82
+
83
+ ## 📄 License
84
+
85
+ MIT License © 2022-PRESENT [Anthony Fu](https://github.com/antfu)
package/dist/index.cjs ADDED
@@ -0,0 +1,91 @@
1
+ 'use strict';
2
+
3
+ const vitepress = require('vitepress');
4
+ const vitePluginPwa = require('vite-plugin-pwa');
5
+
6
+ function configurePWAOptions(options) {
7
+ if (!options.outDir)
8
+ options.outDir = ".vitepress/dist";
9
+ if (options.strategies === "injectManifest") {
10
+ options.injectManifest = options.injectManifest ?? {};
11
+ } else {
12
+ options.workbox = options.workbox ?? {};
13
+ if (options.registerType === "autoUpdate" && (options.injectRegister === "script" || options.injectRegister === "inline")) {
14
+ options.workbox.clientsClaim = true;
15
+ options.workbox.skipWaiting = true;
16
+ }
17
+ }
18
+ }
19
+
20
+ function withPwa(config) {
21
+ let viteConf = config.vite;
22
+ if (!viteConf) {
23
+ viteConf = {};
24
+ config.vite = viteConf;
25
+ }
26
+ let vitePlugins = viteConf.plugins;
27
+ if (typeof vitePlugins === "undefined") {
28
+ vitePlugins = [];
29
+ viteConf.plugins = vitePlugins;
30
+ }
31
+ if (vitePlugins && vitePlugins.length > 0) {
32
+ const pwaPlugin = vitePlugins.find((i) => i && typeof i === "object" && "name" in i && i.name === "vite-plugin-pwa");
33
+ if (pwaPlugin)
34
+ throw new Error("Remove vite-plugin-pwa plugin from Vite Plugins entry in VitePress config file");
35
+ }
36
+ const { pwa = {}, ...vitePressOptions } = config;
37
+ configurePWAOptions(pwa);
38
+ let api;
39
+ vitePlugins.push(
40
+ vitePluginPwa.VitePWA({ ...pwa }),
41
+ {
42
+ name: "vite-plugin-pwa:vitepress",
43
+ apply: "build",
44
+ enforce: "post",
45
+ configResolved(resolvedViteconfig) {
46
+ if (!resolvedViteconfig.build.ssr)
47
+ api = resolvedViteconfig.plugins.find((p) => p.name === "vite-plugin-pwa")?.api;
48
+ }
49
+ }
50
+ );
51
+ const vitePressConfig = vitepress.defineConfigWithTheme(vitePressOptions);
52
+ const userTransformHead = vitePressConfig.transformHead;
53
+ const userBuildEnd = vitePressConfig.buildEnd;
54
+ vitePressConfig.transformHead = async (ctx) => {
55
+ const head = await userTransformHead?.(ctx) ?? [];
56
+ const webManifestData = api?.webManifestData();
57
+ if (webManifestData) {
58
+ const href = webManifestData.href;
59
+ if (webManifestData.useCredentials)
60
+ head.push(["link", { rel: "manifest", href, crossorigin: "use-credentials" }]);
61
+ else
62
+ head.push(["link", { rel: "manifest", href }]);
63
+ }
64
+ const registerSWData = api?.registerSWData();
65
+ if (registerSWData && registerSWData.shouldRegisterSW) {
66
+ if (registerSWData.inline) {
67
+ head.push([
68
+ "script",
69
+ { id: "vite-plugin-pwa:inline-sw" },
70
+ `if('serviceWorker' in navigator) {window.addEventListener('load', () => {navigator.serviceWorker.register('${registerSWData.inlinePath}', { scope: '${registerSWData.scope}' })})}`
71
+ ]);
72
+ } else {
73
+ head.push([
74
+ "script",
75
+ {
76
+ id: "vite-plugin-pwa:register-sw",
77
+ src: registerSWData.registerPath
78
+ }
79
+ ]);
80
+ }
81
+ }
82
+ return head;
83
+ };
84
+ vitePressConfig.buildEnd = async (siteConfig) => {
85
+ await userBuildEnd?.(siteConfig);
86
+ api && !api.disabled && await api.generateSW();
87
+ };
88
+ return vitePressConfig;
89
+ }
90
+
91
+ exports.withPwa = withPwa;
@@ -0,0 +1,14 @@
1
+ import { UserConfig } from 'vitepress';
2
+ import { VitePWAOptions } from 'vite-plugin-pwa';
3
+
4
+ interface PwaOptions extends Partial<VitePWAOptions> {
5
+ }
6
+ declare module 'vitepress' {
7
+ interface UserConfig {
8
+ pwa?: PwaOptions;
9
+ }
10
+ }
11
+
12
+ declare function withPwa(config: UserConfig): UserConfig<any>;
13
+
14
+ export { PwaOptions, withPwa };
package/dist/index.mjs ADDED
@@ -0,0 +1,89 @@
1
+ import { defineConfigWithTheme } from 'vitepress';
2
+ import { VitePWA } from 'vite-plugin-pwa';
3
+
4
+ function configurePWAOptions(options) {
5
+ if (!options.outDir)
6
+ options.outDir = ".vitepress/dist";
7
+ if (options.strategies === "injectManifest") {
8
+ options.injectManifest = options.injectManifest ?? {};
9
+ } else {
10
+ options.workbox = options.workbox ?? {};
11
+ if (options.registerType === "autoUpdate" && (options.injectRegister === "script" || options.injectRegister === "inline")) {
12
+ options.workbox.clientsClaim = true;
13
+ options.workbox.skipWaiting = true;
14
+ }
15
+ }
16
+ }
17
+
18
+ function withPwa(config) {
19
+ let viteConf = config.vite;
20
+ if (!viteConf) {
21
+ viteConf = {};
22
+ config.vite = viteConf;
23
+ }
24
+ let vitePlugins = viteConf.plugins;
25
+ if (typeof vitePlugins === "undefined") {
26
+ vitePlugins = [];
27
+ viteConf.plugins = vitePlugins;
28
+ }
29
+ if (vitePlugins && vitePlugins.length > 0) {
30
+ const pwaPlugin = vitePlugins.find((i) => i && typeof i === "object" && "name" in i && i.name === "vite-plugin-pwa");
31
+ if (pwaPlugin)
32
+ throw new Error("Remove vite-plugin-pwa plugin from Vite Plugins entry in VitePress config file");
33
+ }
34
+ const { pwa = {}, ...vitePressOptions } = config;
35
+ configurePWAOptions(pwa);
36
+ let api;
37
+ vitePlugins.push(
38
+ VitePWA({ ...pwa }),
39
+ {
40
+ name: "vite-plugin-pwa:vitepress",
41
+ apply: "build",
42
+ enforce: "post",
43
+ configResolved(resolvedViteconfig) {
44
+ if (!resolvedViteconfig.build.ssr)
45
+ api = resolvedViteconfig.plugins.find((p) => p.name === "vite-plugin-pwa")?.api;
46
+ }
47
+ }
48
+ );
49
+ const vitePressConfig = defineConfigWithTheme(vitePressOptions);
50
+ const userTransformHead = vitePressConfig.transformHead;
51
+ const userBuildEnd = vitePressConfig.buildEnd;
52
+ vitePressConfig.transformHead = async (ctx) => {
53
+ const head = await userTransformHead?.(ctx) ?? [];
54
+ const webManifestData = api?.webManifestData();
55
+ if (webManifestData) {
56
+ const href = webManifestData.href;
57
+ if (webManifestData.useCredentials)
58
+ head.push(["link", { rel: "manifest", href, crossorigin: "use-credentials" }]);
59
+ else
60
+ head.push(["link", { rel: "manifest", href }]);
61
+ }
62
+ const registerSWData = api?.registerSWData();
63
+ if (registerSWData && registerSWData.shouldRegisterSW) {
64
+ if (registerSWData.inline) {
65
+ head.push([
66
+ "script",
67
+ { id: "vite-plugin-pwa:inline-sw" },
68
+ `if('serviceWorker' in navigator) {window.addEventListener('load', () => {navigator.serviceWorker.register('${registerSWData.inlinePath}', { scope: '${registerSWData.scope}' })})}`
69
+ ]);
70
+ } else {
71
+ head.push([
72
+ "script",
73
+ {
74
+ id: "vite-plugin-pwa:register-sw",
75
+ src: registerSWData.registerPath
76
+ }
77
+ ]);
78
+ }
79
+ }
80
+ return head;
81
+ };
82
+ vitePressConfig.buildEnd = async (siteConfig) => {
83
+ await userBuildEnd?.(siteConfig);
84
+ api && !api.disabled && await api.generateSW();
85
+ };
86
+ return vitePressConfig;
87
+ }
88
+
89
+ export { withPwa };
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist'
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@vite-pwa/vitepress",
3
+ "type": "module",
4
+ "version": "0.0.2",
5
+ "packageManager": "pnpm@7.14.2",
6
+ "description": "Zero-config PWA for VitePress",
7
+ "author": "antfu <anthonyfu117@hotmail.com>",
8
+ "license": "MIT",
9
+ "funding": "https://github.com/sponsors/antfu",
10
+ "homepage": "https://github.com/vite-pwa/vitepress#readme",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/vite-pwa/vitepress.git"
14
+ },
15
+ "bugs": "https://github.com/vite-pwa/vitepress/issues",
16
+ "keywords": [
17
+ "vitepress",
18
+ "workbox",
19
+ "pwa",
20
+ "vite",
21
+ "vite-plugin"
22
+ ],
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "require": "./dist/index.cjs",
27
+ "import": "./dist/index.mjs"
28
+ },
29
+ "./dist/*": "./dist/*"
30
+ },
31
+ "main": "dist/index.cjs",
32
+ "module": "dist/index.mjs",
33
+ "types": "dist/index.d.ts",
34
+ "files": [
35
+ "dist",
36
+ "*.d.ts"
37
+ ],
38
+ "scripts": {
39
+ "build": "unbuild",
40
+ "lint": "eslint .",
41
+ "lint-fix": "nr lint --fix",
42
+ "xprepublishOnly": "npm run build",
43
+ "xrelease": "npx bumpp --push --tag --commit && npm publish"
44
+ },
45
+ "peerDependencies": {
46
+ "@types/workbox-build": "^5.0.1",
47
+ "vite-plugin-pwa": "^0.13.3"
48
+ },
49
+ "devDependencies": {
50
+ "@antfu/eslint-config": "^0.29.4",
51
+ "@antfu/ni": "^0.18.3",
52
+ "@types/debug": "^4.1.7",
53
+ "@types/workbox-build": "^5.0.1",
54
+ "@typescript-eslint/eslint-plugin": "^5.42.1",
55
+ "eslint": "^8.27.0",
56
+ "https-localhost": "^4.7.1",
57
+ "typescript": "^4.8.4",
58
+ "unbuild": "^0.9.4",
59
+ "vite": "^3.2.3",
60
+ "vite-plugin-pwa": "^0.13.3",
61
+ "vitepress": "1.0.0-alpha.27"
62
+ },
63
+ "pnpm": {
64
+ "peerDependencyRules": {
65
+ "ignoreMissing": [
66
+ "@algolia/client-search"
67
+ ]
68
+ }
69
+ }
70
+ }