Skip to content

Commit

Permalink
chore: register virtual message plugin in vite
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Feb 10, 2024
1 parent 938f8d3 commit 3d8633e
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions examples/vue-router/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import process from 'node:process'
import type { Plugin } from 'vite'
import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import type { ManifestOptions, VitePWAOptions } from 'vite-plugin-pwa'
Expand Down Expand Up @@ -44,28 +45,32 @@ const pwaOptions: Partial<VitePWAOptions> = {
const claims = process.env.CLAIMS === 'true'
const selfDestroying = process.env.SW_DESTROY === 'true'

function virtualMessagePlugin() {
const virtual = 'virtual:message'
const resolvedVirtual = `\0${virtual}`
return {
name: 'vite-plugin-test',
resolveId(id) {
return id === virtual ? resolvedVirtual : null
},
load(id) {
if (id === resolvedVirtual)
return `export const message = 'Message from Virtual Module Plugin'`
},
} satisfies Plugin
}

if (process.env.SW === 'true') {
pwaOptions.srcDir = 'src'
pwaOptions.filename = claims ? 'claims-sw.ts' : 'prompt-sw.ts'
pwaOptions.strategies = 'injectManifest'
;(pwaOptions.manifest as Partial<ManifestOptions>).name = 'PWA Inject Manifest'
;(pwaOptions.manifest as Partial<ManifestOptions>).short_name = 'PWA Inject'
const virtual = 'virtual:message'
const resolvedVirtual = `\0${virtual}`
pwaOptions.injectManifest = {
minify: false,
enableWorkboxModulesLogs: true,
buildPlugins: {
vite: [{
name: 'vite-plugin-test',
resolveId(id) {
return id === virtual ? resolvedVirtual : null
},
load(id) {
if (id === resolvedVirtual)
return `export const message = 'Message from Virtual Module Plugin'`
},
}],
vite: [virtualMessagePlugin()],
},
}
}
Expand All @@ -83,6 +88,7 @@ export default defineConfig({
},
plugins: [
Vue(),
virtualMessagePlugin(),
VitePWA(pwaOptions),
replace({
__DATE__: new Date().toISOString(),
Expand Down

0 comments on commit 3d8633e

Please sign in to comment.