Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): allow overriding options #811

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
},
"dependencies": {
"debug": "^4.3.6",
"defu": "6.1.2",
"pretty-bytes": "^6.1.1",
"tinyglobby": "^0.2.10",
"workbox-build": "^7.3.0",
Expand Down
19 changes: 18 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { resolve } from 'node:path'
import { existsSync } from 'node:fs'
import type { OutputBundle } from 'rollup'
import { cyan, yellow } from 'kolorist'
import { defu } from "defu";
import { generateInjectManifest, generateServiceWorker } from './modules'
import { generateWebManifestFile } from './assets'
import { DEV_SW_NAME, FILE_SW_REGISTER } from './constants'
Expand All @@ -12,7 +13,7 @@ import {
generateWebManifest,
} from './html'
import type { PWAPluginContext } from './context'
import type { ExtendManifestEntriesHook, VitePluginPWAAPI } from './types'
import type { ExtendManifestEntriesHook, VitePluginPWAAPI, ResolvedVitePWAOptions } from './types'

export async function _generateSW({ options, version, viteConfig }: PWAPluginContext) {
if (options.disable)
Expand Down Expand Up @@ -142,11 +143,11 @@ export function createAPI(ctx: PWAPluginContext) {
},
}
},
generateBundle(bundle) {
return _generateBundle(ctx, bundle)
generateBundle(bundle, optionsOverride?: Partial<ResolvedVitePWAOptions>) {
return _generateBundle(defu({options: optionsOverride}, ctx), bundle)
},
async generateSW() {
return await _generateSW(ctx)
async generateSW(optionsOverride?: Partial<ResolvedVitePWAOptions>) {
return await _generateSW(defu({options: optionsOverride}, ctx))
},
extendManifestEntries(fn: ExtendManifestEntriesHook) {
const { options } = ctx
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/preact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type { RegisterSWOptions }
export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
baseUrl,
onNeedRefresh,
onOfflineReady,
onRegistered,
Expand All @@ -20,6 +21,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {
const [updateServiceWorker] = useState(() => {
return registerSW({
immediate,
baseUrl,
onOfflineReady() {
setOfflineReady(true)
onOfflineReady?.()
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type { RegisterSWOptions }
export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
baseUrl,
onNeedRefresh,
onOfflineReady,
onRegistered,
Expand All @@ -20,6 +21,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {
const [updateServiceWorker] = useState(() => {
return registerSW({
immediate,
baseUrl,
onOfflineReady() {
setOfflineReady(true)
onOfflineReady?.()
Expand Down
3 changes: 2 additions & 1 deletion src/client/build/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type { RegisterSWOptions }
export function registerSW(options: RegisterSWOptions = {}) {
const {
immediate = false,
baseUrl = "",
onNeedRefresh,
onOfflineReady,
onRegistered,
Expand All @@ -39,7 +40,7 @@ export function registerSW(options: RegisterSWOptions = {}) {
if ('serviceWorker' in navigator) {
wb = await import('workbox-window').then(({ Workbox }) => {
// __SW__, __SCOPE__ and __TYPE__ will be replaced by virtual module
return new Workbox('__SW__', { scope: '__SCOPE__', type: '__TYPE__' })
return new Workbox(baseUrl+'__SW__', { scope: baseUrl+'__SCOPE__', type: '__TYPE__' })
}).catch((e) => {
onRegisterError?.(e)
return undefined
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/solid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type { RegisterSWOptions }
export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
baseUrl,
onNeedRefresh,
onOfflineReady,
onRegistered,
Expand All @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {

const updateServiceWorker = registerSW({
immediate,
baseUrl,
onOfflineReady() {
setOfflineReady(true)
onOfflineReady?.()
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type { RegisterSWOptions }
export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
baseUrl,
onNeedRefresh,
onOfflineReady,
onRegistered,
Expand All @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {

const updateServiceWorker = registerSW({
immediate,
baseUrl,
onOfflineReady() {
offlineReady.set(true)
onOfflineReady?.()
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type { RegisterSWOptions }
export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
baseUrl,
onNeedRefresh,
onOfflineReady,
onRegistered,
Expand All @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {

const updateServiceWorker = registerSW({
immediate,
baseUrl,
onNeedRefresh() {
needRefresh.value = true
onNeedRefresh?.()
Expand Down
1 change: 1 addition & 0 deletions src/client/type.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface RegisterSWOptions {
immediate?: boolean
baseUrl?: string
onNeedRefresh?: () => void
onOfflineReady?: () => void
/**
Expand Down