Skip to content

Commit

Permalink
cleanup: remove kit plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Sep 21, 2022
1 parent 526a000 commit 1a9d484
Showing 1 changed file with 1 addition and 91 deletions.
92 changes: 1 addition & 91 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Plugin } from 'vite'
import type { VitePluginPWAAPI } from 'vite-plugin-pwa'
import { VitePWA } from 'vite-plugin-pwa'
import type { SvelteKitPWAOptions } from './types'
import { configureSvelteKitOptions } from './config'
Expand All @@ -18,96 +17,7 @@ export function SvelteKitPWA(userOptions: Partial<SvelteKitPWAOptions> = {}): Pl
options,
)

let api: VitePluginPWAAPI | undefined
let command: 'build' | 'serve' | undefined

const plugins = VitePWA(userOptions)

const virtual = 'virtual:vite-pwa/sveltekit'
const resolvedVirtual = `\0${virtual}.js`

plugins.push(
{
name: 'vite-plugin-pwa:sveltekit',
enforce: 'pre',
configResolved(viteConfig) {
if (!viteConfig.build.ssr) {
command = viteConfig.command
api = viteConfig.plugins.find(p => p.name === 'vite-plugin-pwa')?.api
}
},
resolveId(id) {
if (id === virtual)
return resolvedVirtual
},
load(id) {
if (id === resolvedVirtual) {
const pwaEnabled = api && !api.disabled && (command === 'build' || userOptions.devOptions?.enabled === true)
let webManifest = ''
if (pwaEnabled) {
const webManifestData = api!.webManifestData()
if (webManifestData)
webManifest = `<link rel="manifest" href="${webManifestData.href}"${webManifestData.useCredentials ? ' crossorigin="use-credentials"' : ''} />`
}
// TODO: add missing logic
return `
export const pwaEnabled = ${pwaEnabled};
export function registerDevServiceWorker() { return undefined; }
export function webManifestLink() { return '${webManifest}'; }
export function registerSWScript() { return undefined; }
`
}
},

/*
resolveId(id) {
if (id === '$vite-pwa/sveltekit')
return 'vite-pwa-sveltekit-pwa-enabled.js'
if (id === '$vite-pwa/sveltekit/webmanifest')
return 'vite-pwa-sveltekit-webmanifest.svelte'
},
load(id) {
let code: string | undefined
if (id === 'vite-pwa-sveltekit-pwa-enabled.js') {
code = command === 'serve'
? `export const pwaEnabled = ${api && !api.disabled && userOptions.devOptions?.enabled === true}`
: `export const pwaEnabled = ${api && !api.disabled}`
}
if (id === 'vite-pwa-sveltekit-webmanifest.svelte') {
const href = command === 'serve'
? userOptions.devOptions?.enabled === true
? api?.webManifestUrl
: undefined
: api?.webManifestUrl
code = href
? `<link rel="manifest" href="${href}" />`
: '<!---->'
code = `{@html \`${escapeSvelte(code)}\`}`
}
if (code) {
return {
code,
map: { version: 3, mappings: '', sources: [] } as any,
}
}
},
*/
},
)

// escape curlies, backtick, \\t, \\r, \\n to avoid breaking output of {@html \`here\`} in .svelte
// function escapeSvelte(str: string): string {
// return str
// .replace(/{/g, '&#123;')
// .replace(/}/g, '&#125;')
// .replace(/\`/g, '&#96;')
// .replace(/\\\\([trn])/g, ' ')
// }

return plugins
return VitePWA(userOptions)
}

export * from './types'

0 comments on commit 1a9d484

Please sign in to comment.