Skip to content

Commit

Permalink
fix(types): fixed JSDoc not working properly
Browse files Browse the repository at this point in the history
BREAKING CHANGE: you should now pass workbox-webpack-plugin's GenerateSW
and InjectManifest options to workboxOptions instead of the general
options (PluginOptions).
  • Loading branch information
DuCanhGH committed Dec 5, 2022
1 parent 3b9191e commit 259ac35
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 41 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ yarn-error.log
**/public/worker-*.js.map
**/public/fallback-*.js

examples/**/pnpm-lock.yaml

dist/
6 changes: 5 additions & 1 deletion examples/lifecycle/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// @ts-check

const withPWA = require("@ducanh2912/next-pwa").default({
dest: "public",
register: false,
skipWaiting: false,
workboxOptions: {
skipWaiting: false,
},
});

module.exports = withPWA();
4 changes: 3 additions & 1 deletion examples/offline-fallback/next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const withPWA = require("@ducanh2912/next-pwa").default({
dest: "public",
swSrc: "service-worker.js",
workboxOptions: {
swSrc: "service-worker.js",
},
});

module.exports = withPWA({
Expand Down
35 changes: 19 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import WorkboxPlugin from "workbox-webpack-plugin";
import buildCustomWorker from "./build-custom-worker";
import buildFallbackWorker from "./build-fallback-worker";
import defaultCache from "./cache";
import type { FullPluginOptions } from "./private_types";
import type { Fallbacks } from "./types";
import type { Fallbacks, PluginOptions } from "./types";
import {
isGenerateSWConfig,
isInjectManifestConfig,
Expand All @@ -27,7 +26,7 @@ const getRevision = (file: fs.PathOrFileDescriptor) =>
crypto.createHash("md5").update(fs.readFileSync(file)).digest("hex");

const withPWAInit = (
pluginOptions: FullPluginOptions = {}
pluginOptions: PluginOptions = {}
): ((_?: NextConfig) => NextConfig) => {
return (nextConfig: NextConfig = {}) => ({
...nextConfig,
Expand All @@ -53,24 +52,28 @@ const withPWAInit = (
dest = distDir,
sw = "sw.js",
// not actually used
swSrc,
cacheStartUrl = true,
dynamicStartUrl = true,
dynamicStartUrlRedirect,
additionalManifestEntries,
publicExcludes = ["!noprecache/**/*"],
buildExcludes = [],
modifyURLPrefix = {},
manifestTransforms = [],
fallbacks = {},
cacheOnFrontEndNav = false,
reloadOnOnline = true,
scope = basePath,
customWorkerDir = "worker",
subdomainPrefix, // deprecated, use basePath in next.config.js instead
...workbox
workboxOptions = {},
} = pluginOptions;

const {
swSrc,
additionalManifestEntries,
modifyURLPrefix = {},
manifestTransforms = [],
...workbox
} = workboxOptions;

let importScripts: string[] = [];
let runtimeCaching: RuntimeCaching[] = defaultCache;

Expand All @@ -95,12 +98,12 @@ const withPWAInit = (
}...`
);

if (isGenerateSWConfig(pluginOptions)) {
if (pluginOptions.runtimeCaching) {
runtimeCaching = pluginOptions.runtimeCaching;
if (isGenerateSWConfig(workboxOptions)) {
if (workboxOptions.runtimeCaching) {
runtimeCaching = workboxOptions.runtimeCaching;
}
if (pluginOptions.importScripts) {
importScripts = pluginOptions.importScripts;
if (workboxOptions.importScripts) {
importScripts = workboxOptions.importScripts;
}
}

Expand Down Expand Up @@ -330,8 +333,8 @@ const withPWAInit = (
},
],
};
if (isInjectManifestConfig(pluginOptions)) {
const swSrc = path.join(options.dir, pluginOptions.swSrc);
if (isInjectManifestConfig(workboxOptions)) {
const swSrc = path.join(options.dir, workboxOptions.swSrc);
console.log(`> [PWA] Using InjectManifest with ${swSrc}`);
const workboxPlugin = new WorkboxPlugin.InjectManifest({
...workboxCommon,
Expand All @@ -348,7 +351,7 @@ const withPWAInit = (
clientsClaim = true,
cleanupOutdatedCaches = true,
ignoreURLParametersMatching = [],
} = pluginOptions;
} = workboxOptions;
let shutWorkboxAfterCalledMessageUp = false;
if (dev) {
console.log(
Expand Down
15 changes: 4 additions & 11 deletions src/private_types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import type { WebpackInjectManifestOptions } from "workbox-build";
import type { GenerateSWConfig } from "workbox-webpack-plugin";

import type { PluginOptions } from "./types";

export type GenerateSWPluginOptions = PluginOptions &
GenerateSWConfig & {
export type WorkboxTypes = {
GenerateSW: GenerateSWConfig & {
swSrc?: undefined;
};

export type InjectManifestPluginOptions = PluginOptions &
WebpackInjectManifestOptions;

export type FullPluginOptions =
| GenerateSWPluginOptions
| InjectManifestPluginOptions;
InjectManifest: WebpackInjectManifestOptions;
};
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { GenerateSWConfig } from "workbox-webpack-plugin";

import type { WorkboxTypes } from "./private_types";

export interface PluginOptions {
/**
* Disable PWA. Set to `true` to completely disable PWA, set to `false` to
Expand Down Expand Up @@ -132,6 +134,8 @@ export interface PluginOptions {
* @deprecated Use `basePath` in `next.config.js` instead.
*/
subdomainPrefix?: string;
/** Pass options to workbox-webpack-plugin */
workboxOptions?: WorkboxTypes[keyof WorkboxTypes];
}

export interface Fallbacks {
Expand Down
16 changes: 6 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { GenerateSW, InjectManifest } from "workbox-webpack-plugin";

import type {
FullPluginOptions,
GenerateSWPluginOptions,
InjectManifestPluginOptions,
} from "./private_types";
import type { WorkboxTypes } from "./private_types";

export const overrideAfterCalledMethod = (
workboxPlugin: InjectManifest | GenerateSW
Expand All @@ -20,13 +16,13 @@ export const overrideAfterCalledMethod = (
};

export const isInjectManifestConfig = (
config: FullPluginOptions
): config is InjectManifestPluginOptions => {
return typeof config.swSrc === "string";
config: WorkboxTypes[keyof WorkboxTypes] | undefined
): config is WorkboxTypes["InjectManifest"] => {
return typeof config !== "undefined" && typeof config.swSrc === "string";
};

export const isGenerateSWConfig = (
config: FullPluginOptions
): config is GenerateSWPluginOptions => {
config: WorkboxTypes[keyof WorkboxTypes] | undefined
): config is WorkboxTypes["GenerateSW"] => {
return !isInjectManifestConfig(config);
};

0 comments on commit 259ac35

Please sign in to comment.