Skip to content

Commit

Permalink
fix: read from vite config
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 10, 2021
1 parent b7d74d9 commit b73db30
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
57 changes: 31 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,48 @@ import { cachePreset } from './cache'
import { ManifestOptions, VitePWAOptions } from './types'

export function VitePWA(options: Partial<VitePWAOptions> = {}): Plugin {
const root = options.root || process.cwd()
const pkg = JSON.parse(fs.readFileSync(resolve(root, 'package.json'), 'utf-8'))
const outDir = options.outDir || 'dist'

const defaultWorkbox: GenerateSWConfig = {
swDest: `${outDir}/sw.js`,
globDirectory: outDir,
offlineGoogleAnalytics: false,
runtimeCaching: cachePreset,
// prevent tsup replacing `process.env`
// eslint-disable-next-line dot-notation
mode: process['env']['NODE_ENV'] || 'production',
}

const defaultManifest: Partial<ManifestOptions> = {
name: pkg.name,
short_name: pkg.name,
start_url: '/',
display: 'standalone',
background_color: '#ffffff',
lang: 'en',
}

const workbox = Object.assign({}, defaultWorkbox, options.workbox || {})
const manifest = Object.assign({}, defaultManifest, options.manifest || {})
let viteConfig: ResolvedConfig | undefined
let workbox: GenerateSWConfig | undefined
let manifest: Partial<ManifestOptions> = {}

return {
name: 'vite-plugin-pwa',
enforce: 'post',
configResolved(config) {
viteConfig = config
const root = viteConfig.root
const pkg = fs.existsSync('package.json')
? JSON.parse(fs.readFileSync('package.json', 'utf-8'))
: {}

const defaultWorkbox: GenerateSWConfig = {
swDest: resolve(root, `${outDir}/sw.js`),
globDirectory: resolve(root, outDir),
offlineGoogleAnalytics: false,
runtimeCaching: cachePreset,
// prevent tsup replacing `process.env`
// eslint-disable-next-line dot-notation
mode: process['env']['NODE_ENV'] || 'production',
}

const defaultManifest: Partial<ManifestOptions> = {
name: pkg.name,
short_name: pkg.name,
start_url: '/',
display: 'standalone',
background_color: '#ffffff',
lang: 'en',
}

workbox = Object.assign({}, defaultWorkbox, options.workbox || {})
manifest = Object.assign({}, defaultManifest, options.manifest || {})
},
transformIndexHtml: {
enforce: 'post',
transform(html) {
if (!viteConfig?.isProduction)
if (viteConfig!.command !== 'build')
return html

return html.replace(
Expand All @@ -52,7 +57,7 @@ export function VitePWA(options: Partial<VitePWAOptions> = {}): Plugin {
<script>
if('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('${workbox.swDest.replace(outDir, '')}', { scope: './' })
navigator.serviceWorker.register('${workbox!.swDest.replace(outDir, '')}', { scope: './' })
})
}
</script>
Expand All @@ -70,7 +75,7 @@ export function VitePWA(options: Partial<VitePWAOptions> = {}): Plugin {
}
},
buildEnd() {
generateSW(workbox)
generateSW(workbox!)
},
}
}
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export interface ManifestOptions {
*/
export interface VitePWAOptions {
outDir?: string
root?: string
manifest: Partial<ManifestOptions>
workbox: Partial<GenerateSWConfig>
}

0 comments on commit b73db30

Please sign in to comment.