import type { WebAppManifest } from "web-app-manifest"; import type { GenerateSWOptions, InjectManifestOptions } from "workbox-build"; export type { WebAppManifest }; export interface WebAppManifestConfig { /** * @defaultValue "manifest.webmanifest" */ filename?: string; content?: WebAppManifest; /** * Skip the injection? */ skipHtmlInjection?: boolean; minify?: boolean; } export type RegisterSwScriptInjectionTarget = "head" | "body"; export type RegisterSwScriptInjectionPosition = "start" | "end"; export interface DefaultRegisterSwScriptFeatures { /** * reloads the page when the new SW starts controlling the page. * this is enabled by default if `autoSkipWaiting` is true */ autoReloadPage?: boolean; /** * sends "SKIP_WAITING" message to the new SW when it enters "waiting" state. * useless if your SW is unconditionally calling `self.skipWaiting()` (for example if you pass `{skipWaiting: true}` to workbox options in `generateSw` mode); */ autoSkipWaiting?: boolean; } export interface DefaultRegisterSwScriptOptions { /** * @defaultValue "head" */ injectTarget?: RegisterSwScriptInjectionTarget; /** * @defaultValue "end" */ injectPosition?: RegisterSwScriptInjectionPosition; /** * when any of these fields are defined the default SW registration script size will be significantly larger (~3kb gzipped) because the script will include `workbox-window` */ features?: DefaultRegisterSwScriptFeatures; } export interface RegisterSwSharedOptions { scope?: string; } export interface RegisterSwVirtualModuleConfig extends RegisterSwSharedOptions { type: "virtual-module"; /** * can be accessed later in `debug` virtual module. * must be serializable. * @example * ```ts * "my value" * ``` * @example * ```ts * { key: "value" } * ``` */ customDebugValues?: any; } export interface RegisterSwInlineConfig extends RegisterSwSharedOptions, DefaultRegisterSwScriptOptions { type: "inline"; } export interface RegisterSwScriptConfig extends RegisterSwSharedOptions, DefaultRegisterSwScriptOptions { type: "script"; /** * @defaultValue "register-sw.js" */ scriptName?: string; /** * @defaultValue true */ defer?: boolean; } export type WorkboxGenerateSWOptions = Omit; export type WorkboxInjectManifestOptions = Omit; export interface SharedSwConfig { /** * @defaultValue "sw.js" */ filename?: string; /** * should precache the icons defined in {@link WebAppManifestConfig}? * it can be a boolean or an array of the icons' indexes that should be precached. * @example * ```ts * true * ``` * @example * ```ts * [0, 1] * ``` */ includeWebAppManifestIcons?: boolean | number[]; /** * This array (or the array returned by the callback) is passed directly to workbox's `globPatterns`. * By default all assets bundled by rsbuild are included (except the ones that match *.map or *.LICENSE.txt patterns) plus the sw registration script and the web app manifest * @example * ```ts * ["**\/*.{js,wasm,css,html}"] * ``` * @example * ```ts * (assets) => [...assets.map(asset => asset), "**\/*.{ico,svg,png}"] * ``` * @example * ```ts * // initialAssets is the assets before "*.map" and other patterns (see above) were filtered out * (_assets, initialAssets) => initialAssets * ``` */ include?: string[] | ((assets: string[], initialAssets: string[]) => string[]); } export interface GenerateSwModeConfig extends SharedSwConfig { mode: "generateSw"; workboxOptions?: WorkboxGenerateSWOptions; } export interface InjectManifestModeConfig extends SharedSwConfig { mode: "injectManifest"; /** * path to your sw relative to the project root * @example * ```ts * path.join("src", "my-sw.js") * ``` * @example * ```ts * "/home/user/my-project/src/sw.ts" * ``` */ srcFile: string; minify?: boolean; workboxOptions?: WorkboxInjectManifestOptions; } export type ServiceWorkerConfig = GenerateSwModeConfig | InjectManifestModeConfig; export type RegisterSwConfig = RegisterSwInlineConfig | RegisterSwScriptConfig | RegisterSwVirtualModuleConfig; export interface IconHtmlTagConfig { href: string; type?: string; media?: string; crossorigin?: string; fetchpriority?: string; sizes?: string; } export interface HtmlTagsConfig { /** * generates * if `true` then `theme_color` will be extracted from the web app manifest config if it's defined or the default one will be used. */ themeColor?: string | true; /** * generates */ appleTouchIcon?: { href: string; sizes?: string; }; /** * generates */ icon?: IconHtmlTagConfig | IconHtmlTagConfig[]; } export interface PWAPluginOptions { htmlTags?: HtmlTagsConfig; sw?: ServiceWorkerConfig; /** * false to disable * @defaultValue {@link RegisterSwScriptConfig} */ registerSw?: RegisterSwConfig | false; /** * false to disable */ webAppManifest?: WebAppManifestConfig | false; /** * Disable the plugin? * By default the plugin is disabled if the environment is not "web" * @example * ```ts * ({environmentName}) => environmentName !== "web" * ``` */ disabled?: boolean | ((ctx: { environmentName: string; }) => boolean); /** * Enable with "rsbuild dev" command? * @defaultValue false */ dev?: boolean; }