Skip to content

Commit

Permalink
feat(injectManifest): add sw build env options and change vite config…
Browse files Browse the repository at this point in the history
…uration (#690)
  • Loading branch information
userquin authored Mar 21, 2024
1 parent d5c4ed0 commit 1474e50
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export async function resolveOptions(ctx: PWAPluginContext): Promise<ResolvedVit
sourcemap = viteConfig.build.sourcemap,
enableWorkboxModulesLogs,
buildPlugins,
envOptions = {},
...userInjectManifest
} = options.injectManifest || {}
const injectManifest = Object.assign({}, defaultInjectManifest, userInjectManifest)
Expand Down Expand Up @@ -174,6 +175,11 @@ export async function resolveOptions(ctx: PWAPluginContext): Promise<ResolvedVit
}
}

const {
envDir = viteConfig.envDir,
envPrefix = viteConfig.envPrefix,
} = envOptions

const resolvedVitePWAOptions: ResolvedVitePWAOptions = {
base: basePath,
mode,
Expand Down Expand Up @@ -213,6 +219,10 @@ export async function resolveOptions(ctx: PWAPluginContext): Promise<ResolvedVit
sourcemap,
enableWorkboxModulesLogs,
},
injectManifestEnvOptions: {
envDir,
envPrefix,
},
pwaAssets: resolvePWAAssetsOptions(pwaAssets),
}

Expand Down
34 changes: 33 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BuildOptions, Plugin, ResolvedConfig, UserConfig } from 'vite'
import type { BuildOptions, InlineConfig, Plugin, ResolvedConfig, UserConfig } from 'vite'
import type { GenerateSWOptions, InjectManifestOptions, ManifestEntry } from 'workbox-build'
import type { OutputBundle, RollupOptions } from 'rollup'
import type { BuiltInPreset, Preset } from '@vite-pwa/assets-generator/config'
Expand Down Expand Up @@ -85,6 +85,26 @@ export type CustomInjectManifestOptions = InjectManifestOptions & {
* Since `v0.15.0` you can add custom Rollup options to build your service worker: we expose the same configuration to build a worker using Vite.
*/
rollupOptions?: Omit<RollupOptions, 'plugins' | 'output'>

/**
* Environment options.
*
* @since v0.19.6
*/
envOptions?: {
/**
* Configure Vite `envDir` option.
*
* @default Vite `envDir`.
*/
envDir?: UserConfig['envDir']
/**
* Configure Vite `envPrefix` option.
*
* @default Vite `envPrefix`.
*/
envPrefix?: UserConfig['envPrefix']
}
}

export interface PWAIntegration {
Expand All @@ -94,6 +114,14 @@ export interface PWAIntegration {
viteOptions: ResolvedConfig,
options: Partial<VitePWAOptions>,
) => void | Promise<void>
/**
* Allow integrations to configure Vite options for custom service worker build.
*
* @param options Vite options for custom service worker build.
* @see src/vite-build.ts module
* @since v0.19.6
*/
configureCustomSWViteBuild?: (options: InlineConfig) => void | Promise<void>
}

/**
Expand Down Expand Up @@ -388,6 +416,10 @@ export interface ResolvedVitePWAOptions extends Required<Omit<VitePWAOptions, 'p
sourcemap?: BuildOptions['sourcemap']
enableWorkboxModulesLogs?: true
}
injectManifestEnvOptions: {
envDir: ResolvedConfig['envDir']
envPrefix: ResolvedConfig['envPrefix']
}
pwaAssets: false | ResolvedPWAAssetsOptions
}

Expand Down
11 changes: 10 additions & 1 deletion src/vite-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export async function buildSW(
// log sw build
logSWViteBuild(normalizePath(relative(viteOptions.root, options.swSrc)), viteOptions, format)

// allow integrations to modify the build config
await options.integration?.configureCustomSWViteBuild?.(inlineConfig)

await build(inlineConfig)

if (format !== 'iife') {
Expand Down Expand Up @@ -86,13 +89,19 @@ function prepareViteBuild(
define['process.env.NODE_ENV'] = JSON.stringify(nodeEnv)

const buildPlugins = options.buildPlugins
const { format, plugins, rollupOptions } = options.injectManifestRollupOptions
const {
format,
plugins,
rollupOptions,
} = options.injectManifestRollupOptions

const inlineConfig = {
root: viteOptions.root,
base: viteOptions.base,
resolve: viteOptions.resolve,
mode: viteOptions.mode,
envDir: options.injectManifestEnvOptions.envDir,
envPrefix: options.injectManifestEnvOptions.envPrefix,
// don't copy anything from public folder
publicDir: false,
build: {
Expand Down

0 comments on commit 1474e50

Please sign in to comment.