Skip to content

Commit

Permalink
feat!: add experimental PWA assets generation and injection (#621)
Browse files Browse the repository at this point in the history
* feat: pwa assets generation and injection

* chore: update logic for assets 0.2.1

* chore: load assets generator when assets configured

* chore: refactor `ResolvedIconAsset`

* chore: cleanup assets entry from jsdocs

* chore: remove cache control cache

* chore: refactor assets loader logic

* chore: include pwa assets reload support

* chore: include test with png image

* chore: early return if url is not an image

* chore: use test in early return if url is not an image

* chore: use any for PWA assets generator (+ jsdocs)

* chore: refactor html injection

* chore: use any for `lookupPWAAssetsInstructions`

* chore: generate assets only when pwa enabled

* chore: refactor helper functions

* chore: return if missing icon asset

* chore: refactor assets generator

* chore: vite base can be empty

* chore: change logic for overrideManifestIcons

* chore: update logic for overrideManifestIcons

* chore: update hint for overrideManifestIcons

* chore: update icon last modified entry with Date.now() when created

* chore: update `overrideManifestIcons` hint

* chore: change `handleHotUpdate` logic

* chore: find existing links in html injection

* chore: add pwa assets in vue router examples via env var

* chore: update lockfile

* chore: fix lint

* chore: move pwa assets generator to optional dependency

* chore: add integration options

* chore: add integration option to resolved options

* chore: add public and output integration options

* chore: update transform index html logic

* chore: update ypes and docs
  • Loading branch information
userquin committed Feb 17, 2024
1 parent d18c510 commit d9ffcb5
Show file tree
Hide file tree
Showing 43 changed files with 1,758 additions and 188 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Zero-config PWA Framework-agnostic Plugin for Vite
- 🐞 **Development Support**: debug your custom service worker logic as you develop your application
- 🛠️ **Versatile**: integration with meta frameworks: [îles](https://github.com/ElMassimo/iles), [SvelteKit](https://github.com/sveltejs/kit), [VitePress](https://github.com/vuejs/vitepress), [Astro](https://github.com/withastro/astro), and [Nuxt 3](https://github.com/nuxt/nuxt)
- 💥 **PWA Assets Generator**: generate all the PWA assets from a single command and a single source image
- 🚀 **PWA Assets Integration**: serving, generating and injecting PWA Assets on the fly in your application

## 📦 Install

Expand Down
17 changes: 17 additions & 0 deletions examples/assets-generator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PWA Assets Generator</title>
<style>
#app {
text-align: center;
}
</style>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
24 changes: 24 additions & 0 deletions examples/assets-generator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "assets-generator",
"type": "module",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"dev:png": "PNG=true vite",
"build:png": "PNG=true vite build",
"preview:png": "PNG=true vite preview"
},
"devDependencies": {
"rimraf": "^5.0.5",
"sharp": "^0.32.6",
"sharp-ico": "^0.1.5",
"typescript": "^5.3.3",
"vite": "^5.0.10",
"vite-plugin-pwa": "workspace:*",
"@vite-pwa/assets-generator": "^0.2.4",
"workbox-window": "^7.0.0"
}
}
130 changes: 130 additions & 0 deletions examples/assets-generator/public/favicon.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/assets-generator/public/source-test.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions examples/assets-generator/pwa-assets.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
createAppleSplashScreens,
defineConfig,
minimal2023Preset,
} from '@vite-pwa/assets-generator/config'

export default defineConfig({
headLinkOptions: {
preset: '2023',
},
preset: {
...minimal2023Preset,
appleSplashScreens: createAppleSplashScreens({
padding: 0.3,
resizeOptions: { fit: 'contain', background: 'white' },
darkResizeOptions: { fit: 'contain', background: 'black' },
linkMediaOptions: {
log: true,
addMediaScreen: true,
basePath: '/',
xhtml: true,
},
}, ['iPad Air 9.7"']),
},
images: process.env.PNG ? 'public/source-test.png' : 'public/favicon.svg',
})
17 changes: 17 additions & 0 deletions examples/assets-generator/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const date = __DATE__

const app = document.querySelector<HTMLDivElement>('#app')!

app.innerHTML = `
<div>
<img src="/favicon.svg" alt="PWA Logo" width="60" height="60">
<h1>PWA Assets Generator</h1>
<br/>
<p>${date}</p>
<br/>
</div>
`

window.addEventListener('load', () => {
import('./pwa.ts')
})
15 changes: 15 additions & 0 deletions examples/assets-generator/src/pwa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { pwaInfo } from 'virtual:pwa-info'
import { pwaAssetsHead } from 'virtual:pwa-assets/head'
import { pwaAssetsIcons } from 'virtual:pwa-assets/icons'
import { registerSW } from 'virtual:pwa-register'

console.log(pwaInfo)
console.log(pwaAssetsHead)
console.log(pwaAssetsIcons)

registerSW({
immediate: true,
onNeedRefresh() {
console.log('onNeedRefresh message should not appear')
},
})
5 changes: 5 additions & 0 deletions examples/assets-generator/src/vite-env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="vite/client" />
/// <reference types="vite-plugin-pwa/vanillajs" />
/// <reference types="vite-plugin-pwa/info" />

declare const __DATE__: string
24 changes: 24 additions & 0 deletions examples/assets-generator/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020", "DOM", "DOM.Iterable", "WebWorker"],
"useDefineForClassFields": true,
"module": "ESNext",

/* Bundler mode */
"moduleResolution": "bundler",
"resolveJsonModule": true,
"types": ["vite/client", "vite-plugin-pwa/vanillajs", "vite-plugin-pwa/info", "vite-plugin-pwa/pwa-assets"],
"allowImportingTsExtensions": true,

/* Linting */
"strict": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noEmit": true,
"isolatedModules": true,
"skipLibCheck": true
},
"include": ["src"]
}
42 changes: 42 additions & 0 deletions examples/assets-generator/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { defineConfig } from 'vite'
import { VitePWA } from 'vite-plugin-pwa'

export default defineConfig({
logLevel: 'info',
define: {
__DATE__: `'${new Date().toISOString()}'`,
},
build: {
sourcemap: true,
},
plugins: [
VitePWA({
mode: 'development',
base: '/',
/* buildBase: '/test-build-base/', */
strategies: 'generateSW',
registerType: 'autoUpdate',
includeAssets: ['favicon.svg'],
manifest: {
name: 'Vite PWA',
short_name: 'Vite PWA',
theme_color: '#ffffff',
},
pwaAssets: {
config: true,
overrideManifestIcons: true,
},
workbox: {
globPatterns: ['**/*.{js,css,html,png,svg,ico}'],
cleanupOutdatedCaches: true,
clientsClaim: true,
skipWaiting: true,
},
devOptions: {
enabled: true,
suppressWarnings: true,
type: 'module',
},
}),
],
})
4 changes: 2 additions & 2 deletions examples/preact-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
"@rollup/plugin-replace": "^5.0.5",
"https-localhost": "^4.7.1",
"rimraf": "^5.0.5",
"typescript": "^5.2.2",
"vite": "^5.0.0",
"typescript": "^5.3.3",
"vite": "^5.0.10",
"vite-plugin-pwa": "workspace:*",
"workbox-core": "^7.0.0",
"workbox-precaching": "^7.0.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/react-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
"@vitejs/plugin-react": "^4.2.0",
"https-localhost": "^4.7.1",
"rimraf": "^5.0.5",
"typescript": "^5.2.2",
"vite": "^5.0.0",
"typescript": "^5.3.3",
"vite": "^5.0.10",
"vite-plugin-pwa": "workspace:*",
"workbox-core": "^7.0.0",
"workbox-precaching": "^7.0.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/solid-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
"@rollup/plugin-replace": "^5.0.5",
"https-localhost": "^4.7.1",
"rimraf": "^5.0.5",
"typescript": "^5.2.2",
"vite": "^5.0.0",
"typescript": "^5.3.3",
"vite": "^5.0.10",
"vite-plugin-pwa": "workspace:*",
"vite-plugin-solid": "^2.7.2",
"workbox-core": "^7.0.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/svelte-routify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"svelte": "^4.2.5",
"svelte-check": "^3.6.0",
"svelte-preprocess": "^5.1.0",
"typescript": "^5.2.2",
"vite": "^5.0.0",
"typescript": "^5.3.3",
"vite": "^5.0.10",
"vite-plugin-pwa": "workspace:*",
"workbox-core": "^7.0.0",
"workbox-precaching": "^7.0.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/vanilla-ts-dev-options/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
},
"devDependencies": {
"rimraf": "^5.0.5",
"typescript": "^5.2.2",
"vite": "^5.0.0",
"typescript": "^5.3.3",
"vite": "^5.0.10",
"vite-plugin-pwa": "workspace:*"
}
}
4 changes: 2 additions & 2 deletions examples/vanilla-ts-no-ip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"devDependencies": {
"lodash-es": "^4.17.21",
"rimraf": "^5.0.5",
"typescript": "^5.2.2",
"vite": "^5.0.0",
"typescript": "^5.3.3",
"vite": "^5.0.10",
"vite-plugin-pwa": "workspace:*",
"workbox-cacheable-response": "^7.0.0",
"workbox-core": "^7.0.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/vue-basic-cdn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"@vitejs/plugin-vue": "^4.3.4",
"@vueuse/core": "^10.6.1",
"https-localhost": "^4.7.1",
"typescript": "^5.2.2",
"vite": "^5.0.0",
"typescript": "^5.3.3",
"vite": "^5.0.10",
"vite-plugin-pwa": "workspace:*"
}
}

0 comments on commit d9ffcb5

Please sign in to comment.