diff --git a/package.json b/package.json index 2f2663f2..0c1d6e16 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6e7b4d6b..ecc9e8c2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: debug: specifier: ^4.3.6 version: 4.3.6 + defu: + specifier: 6.1.2 + version: 6.1.2 pretty-bytes: specifier: ^6.1.1 version: 6.1.1 @@ -1483,12 +1486,15 @@ packages: '@esbuild-kit/cjs-loader@2.4.2': resolution: {integrity: sha512-BDXFbYOJzT/NBEtp71cvsrGPwGAMGRB/349rwKuoxNSiKjPraNNnlK6MIIabViCjqZugu6j+xeMDlEkWdHHJSg==} + deprecated: 'Merged into tsx: https://tsx.is' '@esbuild-kit/core-utils@3.2.2': resolution: {integrity: sha512-Ub6LaRaAgF80dTSzUdXpFLM1pVDdmEVB9qb5iAzSpyDlX/mfJTFGOnZ516O05p5uWWteNviMKi4PAyEuRxI5gA==} + deprecated: 'Merged into tsx: https://tsx.is' '@esbuild-kit/esm-loader@2.5.5': resolution: {integrity: sha512-Qwfvj/qoPbClxCRNuac1Du01r9gvNOT+pMYtJDapfB1eoGN1YlJ1BixLyL9WVENRx5RXgNLdfYdx/CuswlGhMw==} + deprecated: 'Merged into tsx: https://tsx.is' '@esbuild/aix-ppc64@0.21.5': resolution: {integrity: sha512-1SDgH6ZSPTlggy1yI6+Dbkiz8xzpHJEVAlF/AM1tHPLsf5STom9rwtjE4hKAF20FfXXNTFqEYXyJNWh1GiZedQ==} @@ -1922,6 +1928,7 @@ packages: '@humanwhocodes/config-array@0.11.13': resolution: {integrity: sha512-JSBDMiDKSzQVngfRjOdFXgFfklaXI4K9nLF49Auh21lmBWRLIK3+xTErTWD4KU54pb6coM6ESE7Awz/FNU3zgQ==} engines: {node: '>=10.10.0'} + deprecated: Use @eslint/config-array instead '@humanwhocodes/module-importer@1.0.1': resolution: {integrity: sha512-bxveV4V8v5Yb4ncFTT3rPSgZBOpCkjfK0y4oVVVJwIuDVBRMDXrPyXRL988i5ap9m9bnyEEjWfm5WkBmtffLfA==} @@ -1929,6 +1936,7 @@ packages: '@humanwhocodes/object-schema@2.0.1': resolution: {integrity: sha512-dvuCeX5fC9dXgJn9t+X5atfmgQAzUOWqS1254Gh0m6i8wKd10ebXkfNKiRK+1GWi/yTvvLDHpoxLr0xxxeslWw==} + deprecated: Use @eslint/object-schema instead '@iconify/types@1.1.0': resolution: {integrity: sha512-Jh0llaK2LRXQoYsorIH8maClebsnzTcve+7U3rQUSnC11X4jtPnFuyatqFLvMxZ8MLG8dB4zfHsbPfuvxluONw==} @@ -2012,6 +2020,7 @@ packages: '@playwright/test@1.40.0': resolution: {integrity: sha512-PdW+kn4eV99iP5gxWNSDQCbhMaDVej+RXL5xr6t04nbKLCBwYtA046t7ofoczHOm8u6c+45hpDKQVZqtqwkeQg==} engines: {node: '>=16'} + deprecated: Please update to the latest version of Playwright to test up-to-date browsers. hasBin: true '@polka/url@1.0.0-next.21': @@ -2818,6 +2827,7 @@ packages: abab@2.0.6: resolution: {integrity: sha512-j2afSsaIENvHZN2B8GOpF566vZ5WVk5opAiMTvWgaQT8DkbOqsTfvNAvHoRGU2zzP8cPoqys+xHTRDWW8L+/BA==} + deprecated: Use your platform's native atob() and btoa() methods instead accepts@1.3.8: resolution: {integrity: sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==} @@ -3219,7 +3229,7 @@ packages: engines: {node: '>= 0.8.0'} concat-map@0.0.1: - resolution: {integrity: sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=} + resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==} configent@2.2.0: resolution: {integrity: sha512-yIN6zfOWk2nycNJ2JFNiWEai0oiqAhISIht8+pbEBP8bdcpwoQ74AhCZPbUv9aRVJwo7wh1MbCBDUV44UJa7Kw==} @@ -3441,6 +3451,7 @@ packages: domexception@2.0.1: resolution: {integrity: sha512-yxJ2mFy/sibVQlu5qHjOkf9J3K6zgmCxgJ94u2EdvDOV09H+32LtRswEcUsmUWN72pVLOEnTSRaIVVzVQgS0dg==} engines: {node: '>=8'} + deprecated: Use your platform's native DOMException instead dotenv@16.3.1: resolution: {integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==} @@ -3727,6 +3738,7 @@ packages: eslint@8.54.0: resolution: {integrity: sha512-NY0DfAkM8BIZDVl6PgSa1ttZbx3xHgJzSNJKYcQglem6CppHyMhRIQkBVSSMaSRnLhig3jsDbEzOjwCVt4AmmA==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} + deprecated: This version is no longer supported. Please see https://eslint.org/version-support for other options. hasBin: true esm-env@1.0.0: @@ -4475,6 +4487,7 @@ packages: loupe@2.3.6: resolution: {integrity: sha512-RaPMZKiMy8/JruncMU5Bt6na1eftNoo++R4Y+N2FrxkDVTrGvcyzFTsaGif4QTeKESheMGegbhw6iUAq+5A8zA==} + deprecated: Please upgrade to 2.3.7 which fixes GHSA-4q6p-r6v2-jvc5 loupe@2.3.7: resolution: {integrity: sha512-zSMINGVYkdpYSOBmLi0D1Uo7JU9nVdQKrHxC8eYlV+9YKK9WePqAlL7lSlorG/U2Fw1w0hTBmaa/jrQ3UbPHtA==} @@ -4794,6 +4807,7 @@ packages: ohmyfetch@0.4.18: resolution: {integrity: sha512-MslzNrQzBLtZHmiZBI8QMOcMpdNFlK61OJ34nFNFynZ4v+4BonfCQ7VIN4EGXvGGq5zhDzgdJoY3o9S1l2T7KQ==} + deprecated: Package renamed to https://github.com/unjs/ofetch on-finished@2.4.1: resolution: {integrity: sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==} @@ -5214,10 +5228,12 @@ packages: rimraf@2.7.1: resolution: {integrity: sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==} + deprecated: Rimraf versions prior to v4 are no longer supported hasBin: true rimraf@3.0.2: resolution: {integrity: sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==} + deprecated: Rimraf versions prior to v4 are no longer supported hasBin: true rimraf@5.0.5: @@ -5766,6 +5782,7 @@ packages: tsup@7.3.0: resolution: {integrity: sha512-Ja1eaSRrE+QarmATlNO5fse2aOACYMBX+IZRKy1T+gpyH+jXgRrl5l4nHIQJQ1DoDgEjHDTw8cpE085UdBZuWQ==} engines: {node: '>=18'} + deprecated: Breaking node 16 hasBin: true peerDependencies: '@swc/core': ^1 diff --git a/src/api.ts b/src/api.ts index 2dd77955..4fd52cf6 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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' @@ -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) @@ -142,11 +143,11 @@ export function createAPI(ctx: PWAPluginContext) { }, } }, - generateBundle(bundle) { - return _generateBundle(ctx, bundle) + generateBundle(bundle, optionsOverride?: Partial) { + return _generateBundle(defu({options: optionsOverride}, ctx), bundle) }, - async generateSW() { - return await _generateSW(ctx) + async generateSW(optionsOverride?: Partial) { + return await _generateSW(defu({options: optionsOverride}, ctx)) }, extendManifestEntries(fn: ExtendManifestEntriesHook) { const { options } = ctx diff --git a/src/client/build/preact.ts b/src/client/build/preact.ts index 847817e8..f898865a 100644 --- a/src/client/build/preact.ts +++ b/src/client/build/preact.ts @@ -7,6 +7,7 @@ export type { RegisterSWOptions } export function useRegisterSW(options: RegisterSWOptions = {}) { const { immediate = true, + baseUrl, onNeedRefresh, onOfflineReady, onRegistered, @@ -20,6 +21,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { const [updateServiceWorker] = useState(() => { return registerSW({ immediate, + baseUrl, onOfflineReady() { setOfflineReady(true) onOfflineReady?.() diff --git a/src/client/build/react.ts b/src/client/build/react.ts index b0105819..6efc2b4d 100644 --- a/src/client/build/react.ts +++ b/src/client/build/react.ts @@ -7,6 +7,7 @@ export type { RegisterSWOptions } export function useRegisterSW(options: RegisterSWOptions = {}) { const { immediate = true, + baseUrl, onNeedRefresh, onOfflineReady, onRegistered, @@ -20,6 +21,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { const [updateServiceWorker] = useState(() => { return registerSW({ immediate, + baseUrl, onOfflineReady() { setOfflineReady(true) onOfflineReady?.() diff --git a/src/client/build/register.ts b/src/client/build/register.ts index de4d7af3..5b6320c0 100644 --- a/src/client/build/register.ts +++ b/src/client/build/register.ts @@ -17,6 +17,7 @@ export type { RegisterSWOptions } export function registerSW(options: RegisterSWOptions = {}) { const { immediate = false, + baseUrl = "", onNeedRefresh, onOfflineReady, onRegistered, @@ -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 diff --git a/src/client/build/solid.ts b/src/client/build/solid.ts index 76e378ea..7a929643 100644 --- a/src/client/build/solid.ts +++ b/src/client/build/solid.ts @@ -7,6 +7,7 @@ export type { RegisterSWOptions } export function useRegisterSW(options: RegisterSWOptions = {}) { const { immediate = true, + baseUrl, onNeedRefresh, onOfflineReady, onRegistered, @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { const updateServiceWorker = registerSW({ immediate, + baseUrl, onOfflineReady() { setOfflineReady(true) onOfflineReady?.() diff --git a/src/client/build/svelte.ts b/src/client/build/svelte.ts index f1e71cc1..4d69af73 100644 --- a/src/client/build/svelte.ts +++ b/src/client/build/svelte.ts @@ -7,6 +7,7 @@ export type { RegisterSWOptions } export function useRegisterSW(options: RegisterSWOptions = {}) { const { immediate = true, + baseUrl, onNeedRefresh, onOfflineReady, onRegistered, @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { const updateServiceWorker = registerSW({ immediate, + baseUrl, onOfflineReady() { offlineReady.set(true) onOfflineReady?.() diff --git a/src/client/build/vue.ts b/src/client/build/vue.ts index 4fd6801a..2dca7efc 100644 --- a/src/client/build/vue.ts +++ b/src/client/build/vue.ts @@ -7,6 +7,7 @@ export type { RegisterSWOptions } export function useRegisterSW(options: RegisterSWOptions = {}) { const { immediate = true, + baseUrl, onNeedRefresh, onOfflineReady, onRegistered, @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { const updateServiceWorker = registerSW({ immediate, + baseUrl, onNeedRefresh() { needRefresh.value = true onNeedRefresh?.() diff --git a/src/client/type.d.ts b/src/client/type.d.ts index fcb2a563..bf195365 100644 --- a/src/client/type.d.ts +++ b/src/client/type.d.ts @@ -1,5 +1,6 @@ export interface RegisterSWOptions { immediate?: boolean + baseUrl?: string onNeedRefresh?: () => void onOfflineReady?: () => void /**