Skip to content

Commit

Permalink
fix: custom sw build failing (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed May 24, 2023
1 parent ce87e2e commit bc80d19
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'path'
import { existsSync } from 'fs'
import { resolve } from 'node:path'
import { existsSync } from 'node:fs'
import type { OutputBundle } from 'rollup'
import { generateInjectManifest, generateServiceWorker } from './modules'
import { generateWebManifestFile } from './assets'
Expand Down
6 changes: 3 additions & 3 deletions src/assets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve as resolveFs } from 'path'
import fs from 'fs'
import crypto from 'crypto'
import { resolve as resolveFs } from 'node:path'
import fs from 'node:fs'
import crypto from 'node:crypto'
import fg from 'fast-glob'
import type { GenerateSWOptions, InjectManifestOptions, ManifestEntry } from 'workbox-build'
import type { ResolvedConfig } from 'vite'
Expand Down
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ export const DEV_SW_VIRTUAL = `${VIRTUAL_MODULES_RESOLVE_PREFIX}pwa-entry-point-
export const RESOLVED_DEV_SW_VIRTUAL = `\0${DEV_SW_VIRTUAL}`
export const DEV_READY_NAME = 'vite-pwa-plugin:dev-ready'
export const DEV_REGISTER_SW_NAME = 'vite-plugin-pwa:register-sw'

2 changes: 1 addition & 1 deletion src/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import { relative } from 'path'
import { relative } from 'node:path'
import type { BuildResult } from 'workbox-build'
import type { ResolvedConfig } from 'vite'
import { cyan, dim, green, magenta, yellow } from 'kolorist'
Expand Down
8 changes: 5 additions & 3 deletions src/modules.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dirname, resolve } from 'path'
import { promises as fs } from 'fs'
import { fileURLToPath } from 'url'
import { dirname, resolve } from 'node:path'
import { promises as fs } from 'node:fs'
import { fileURLToPath } from 'node:url'
import type { BuildResult } from 'workbox-build'
import type { ResolvedConfig } from 'vite'
import type { ResolvedVitePWAOptions } from './types'
Expand All @@ -20,6 +20,7 @@ async function loadWorkboxBuild(): Promise<typeof import('workbox-build')> {
return workbox.default ?? workbox
}
catch (_) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require('workbox-build')
}
}
Expand Down Expand Up @@ -96,6 +97,7 @@ export async function generateInjectManifest(options: ResolvedVitePWAOptions, vi
const { format, plugins, rollupOptions } = options.injectManifestRollupOptions

await build({
root: viteOptions.root,
base: viteOptions.base,
build: {
sourcemap: viteOptions.build.sourcemap,
Expand Down
6 changes: 3 additions & 3 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs'
import { extname, resolve } from 'path'
import fs from 'node:fs'
import { extname, resolve } from 'node:path'
import type { ResolvedConfig } from 'vite'
import type { GenerateSWOptions, InjectManifestOptions } from 'workbox-build'
import type { ManifestOptions, ResolvedVitePWAOptions, VitePWAOptions } from './types'
Expand Down Expand Up @@ -101,7 +101,7 @@ export async function resolveOptions(options: Partial<VitePWAOptions>, viteConfi
}

const workbox = Object.assign({}, defaultWorkbox, options.workbox || {})
const manifest = typeof options.manifest === 'boolean' && !options.manifest
const manifest = (typeof options.manifest === 'boolean' && !options.manifest)
? false
: Object.assign({}, defaultManifest, options.manifest || {})
const {
Expand Down
9 changes: 4 additions & 5 deletions src/plugins/dev.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { basename, resolve } from 'path'
import { existsSync, promises as fs, mkdirSync } from 'fs'
import { basename, resolve } from 'node:path'
import { existsSync, promises as fs, mkdirSync } from 'node:fs'
import type { Plugin, ResolvedConfig, ViteDevServer } from 'vite'
import {
generateRegisterDevSW,
Expand All @@ -26,7 +26,7 @@ export const swDevOptions = {
workboxPaths: new Map<string, string>(),
}

export function DevPlugin(ctx: PWAPluginContext): Plugin {
export function DevPlugin(ctx: PWAPluginContext) {
return <Plugin>{
name: 'vite-plugin-pwa:dev-sw',
apply: 'serve',
Expand Down Expand Up @@ -77,7 +77,7 @@ export function DevPlugin(ctx: PWAPluginContext): Plugin {
// - the .ts source file
// in both cases we need to resolve the id to the source file to load it and add empty injection point on loadDev
// we need to always return the path to source file name to resolve imports on the sw
return name === swDevOptions.swUrl || name === options.injectManifest.swSrc
return (name === swDevOptions.swUrl || name === options.injectManifest.swSrc)
? options.injectManifest.swSrc
: undefined
}
Expand Down Expand Up @@ -216,4 +216,3 @@ function createSWResponseHandler(server: ViteDevServer, ctx: PWAPluginContext):
}
}
}

7 changes: 3 additions & 4 deletions src/plugins/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type { PWAPluginContext } from '../context'
import type { VitePluginPWAAPI } from '../types'
import { swDevOptions } from './dev'

export function MainPlugin(ctx: PWAPluginContext, api: VitePluginPWAAPI): Plugin {
return {
export function MainPlugin(ctx: PWAPluginContext, api: VitePluginPWAAPI) {
return <Plugin>{
name: 'vite-plugin-pwa',
enforce: 'pre',
config() {
Expand Down Expand Up @@ -49,7 +49,7 @@ export function MainPlugin(ctx: PWAPluginContext, api: VitePluginPWAAPI): Plugin
else {
return generateRegisterSW(
ctx.options,
!ctx.options.disable && ctx.viteConfig.command === 'build' ? 'build' : 'dev',
(!ctx.options.disable && ctx.viteConfig.command === 'build') ? 'build' : 'dev',
VIRTUAL_MODULES_MAP[id],
)
}
Expand All @@ -58,4 +58,3 @@ export function MainPlugin(ctx: PWAPluginContext, api: VitePluginPWAAPI): Plugin
api,
}
}

2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function slash(str: string) {
export function resolveBasePath(base: string) {
if (isAbsolute(base))
return base
return !base.startsWith('/') && !base.startsWith('./')
return (!base.startsWith('/') && !base.startsWith('./'))
? `/${base}`
: base
}
Expand Down

0 comments on commit bc80d19

Please sign in to comment.