@serwist/next 9.4.3 → 9.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,36 @@
1
1
  import type { BuildOptions } from "@serwist/cli";
2
+ import type { NextConfigComplete } from "next/dist/server/config-shared.js";
2
3
  import type { SerwistOptions } from "./lib/config/types.js";
3
4
  import { generateGlobPatterns } from "./lib/config/utils.js";
4
- export declare const serwist: (options: SerwistOptions) => Promise<BuildOptions>;
5
+ /**
6
+ * Additional build context.
7
+ */
8
+ export interface SerwistContext {
9
+ /**
10
+ * The current working directory.
11
+ */
12
+ cwd?: string;
13
+ /**
14
+ * Whether Serwist is in development mode. This option determines how Next.js configuration
15
+ * is resolved. Note that it doesn't change how the service worker is built.
16
+ */
17
+ isDev?: boolean;
18
+ }
19
+ export interface Serwist {
20
+ /**
21
+ * Integrates Serwist into your Next.js app.
22
+ * @param options
23
+ * @returns
24
+ */
25
+ (options: SerwistOptions, nextConfig?: NextConfigComplete, context?: SerwistContext): Promise<BuildOptions>;
26
+ /**
27
+ * Integrates Serwist into your Next.js app. Allows reading fully resolved Next.js configuration.
28
+ * @param optionsFunction
29
+ * @returns
30
+ */
31
+ withNextConfig: (optionsFunction: (nextConfig: NextConfigComplete) => Promise<SerwistOptions> | SerwistOptions, context?: SerwistContext) => Promise<BuildOptions>;
32
+ }
33
+ export declare const serwist: Serwist;
5
34
  export { generateGlobPatterns };
6
35
  export type { SerwistOptions };
7
36
  //# sourceMappingURL=index.config.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.config.d.ts","sourceRoot":"","sources":["../src/index.config.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAoB,MAAM,uBAAuB,CAAC;AAI/E,eAAO,MAAM,OAAO,GAAU,SAAS,cAAc,KAAG,OAAO,CAAC,YAAY,CAiF3E,CAAC;AAEF,OAAO,EAAE,oBAAoB,EAAE,CAAC;AAEhC,YAAY,EAAE,cAAc,EAAE,CAAC"}
1
+ {"version":3,"file":"index.config.d.ts","sourceRoot":"","sources":["../src/index.config.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AAC5E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAoC,MAAM,uBAAuB,CAAC;AAK/F;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,OAAO;IACtB;;;;OAIG;IACH,CAAC,OAAO,EAAE,cAAc,EAAE,UAAU,CAAC,EAAE,kBAAkB,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IAC5G;;;;OAIG;IACH,cAAc,EAAE,CACd,eAAe,EAAE,CAAC,UAAU,EAAE,kBAAkB,KAAK,OAAO,CAAC,cAAc,CAAC,GAAG,cAAc,EAC7F,OAAO,CAAC,EAAE,cAAc,KACrB,OAAO,CAAC,YAAY,CAAC,CAAC;CAC5B;AAED,eAAO,MAAM,OAAO,EAAE,OA4ErB,CAAC;AAOF,OAAO,EAAE,oBAAoB,EAAE,CAAC;AAEhC,YAAY,EAAE,cAAc,EAAE,CAAC"}
@@ -1,9 +1,9 @@
1
- import { createRequire } from 'module';
2
1
  import fs from 'node:fs';
3
2
  import path from 'node:path';
4
3
  import { rebasePath } from '@serwist/build';
5
- import { MODERN_BROWSERSLIST_TARGET, PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD } from 'next/constants.js';
4
+ import { createRequire } from 'module';
6
5
  import browserslist from 'browserslist';
6
+ import { PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD, MODERN_BROWSERSLIST_TARGET } from 'next/constants.js';
7
7
 
8
8
  const SUPPORTED_ESBUILD_TARGETS = [
9
9
  "chrome",
@@ -24,6 +24,14 @@ const UNSUPPORTED_BROWSERLIST_TARGETS = [
24
24
  "android 2"
25
25
  ];
26
26
 
27
+ const __require = createRequire(import.meta.url);
28
+ const nextConfig = __require("next/dist/server/config.js");
29
+ const loadNextConfig = (cwd, isDev)=>{
30
+ const nextPhase = isDev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_BUILD;
31
+ return nextConfig.default(nextPhase, cwd, {
32
+ silent: false
33
+ });
34
+ };
27
35
  const generateGlobPatterns = (distDir)=>[
28
36
  `${distDir}static/**/*.{js,css,html,ico,apng,png,avif,jpg,jpeg,jfif,pjpeg,pjp,gif,svg,webp,json,webmanifest}`,
29
37
  "public/**/*"
@@ -76,17 +84,12 @@ const loadBrowserslist = (cwd)=>{
76
84
  }, []).map((split)=>split.join(""));
77
85
  };
78
86
 
79
- const __require = createRequire(import.meta.url);
80
- const loadNextConfig = __require("next/dist/server/config.js");
81
- const serwist = async (options)=>{
82
- const cwd = process.cwd();
83
- const isDev = process.env.NODE_ENV === "development";
84
- const nextPhase = isDev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_BUILD;
85
- const config = await loadNextConfig.default(nextPhase, cwd, {
86
- silent: false
87
- });
88
- const basePath = config.basePath || "/";
89
- let distDir = config.distDir;
87
+ const _cwd = process.cwd();
88
+ const _isDev = process.env.NODE_ENV === "development";
89
+ const serwist = async (options, nextConfig, { cwd = _cwd, isDev = _isDev } = {})=>{
90
+ if (!nextConfig) nextConfig = await loadNextConfig(cwd, isDev);
91
+ const basePath = nextConfig.basePath || "/";
92
+ let distDir = nextConfig.distDir;
90
93
  if (distDir[0] === "/") distDir = distDir.slice(1);
91
94
  if (distDir[distDir.length - 1] !== "/") distDir += "/";
92
95
  const distServerDir = `${distDir}server/`;
@@ -149,7 +152,7 @@ const serwist = async (options)=>{
149
152
  m.url = path.posix.join(basePath, m.url);
150
153
  }
151
154
  if (m.url.startsWith(distDir)) {
152
- m.url = `${config.assetPrefix ?? ""}/_next/${m.url.slice(distDir.length)}`;
155
+ m.url = `${nextConfig.assetPrefix ?? ""}/_next/${m.url.slice(distDir.length)}`;
153
156
  }
154
157
  if (m.url.startsWith("public/")) {
155
158
  m.url = path.posix.join(basePath, m.url.slice(7));
@@ -168,5 +171,12 @@ const serwist = async (options)=>{
168
171
  }
169
172
  };
170
173
  };
174
+ serwist.withNextConfig = async (optionsFunction, { cwd = _cwd, isDev = _isDev } = {})=>{
175
+ const nextConfig = await loadNextConfig(cwd, isDev);
176
+ return serwist(await optionsFunction(nextConfig), nextConfig, {
177
+ cwd,
178
+ isDev
179
+ });
180
+ };
171
181
 
172
182
  export { generateGlobPatterns, serwist };
@@ -1,3 +1,4 @@
1
+ export declare const loadNextConfig: (cwd: string, isDev: boolean) => Promise<import("next/dist/server/config-shared.js").NextConfigComplete>;
1
2
  export declare const generateGlobPatterns: (distDir: string) => string[];
2
3
  /**
3
4
  * Loads and converts Browserslist into esbuild's `target` option.
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/lib/config/utils.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,oBAAoB,GAAI,SAAS,MAAM,aAGnD,CAAC;AAmBF;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,GAAI,KAAK,MAAM,KAAG,MAAM,EAqDpD,CAAC"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/lib/config/utils.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,cAAc,GAAI,KAAK,MAAM,EAAE,OAAO,OAAO,4EAKzD,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAAI,SAAS,MAAM,aAGnD,CAAC;AAmBF;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,GAAI,KAAK,MAAM,KAAG,MAAM,EAqDpD,CAAC"}
@@ -41,7 +41,7 @@ export declare const injectManifestOptions: z.ZodObject<{
41
41
  url: z.ZodString;
42
42
  }, z.core.$strip>>;
43
43
  warnings: z.ZodOptional<z.ZodArray<z.ZodString>>;
44
- }, z.core.$strict>>>, z.ZodTransform<z.core.$InferInnerFunctionTypeAsync<z.ZodTuple<[z.ZodArray<z.ZodObject<{
44
+ }, z.core.$strict>>>, z.ZodTransform<z.core.$InferOuterFunctionTypeAsync<z.ZodTuple<[z.ZodArray<z.ZodObject<{
45
45
  size: z.ZodNumber;
46
46
  integrity: z.ZodOptional<z.ZodString>;
47
47
  revision: z.ZodOptional<z.ZodNullable<z.ZodString>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/next",
3
- "version": "9.4.3",
3
+ "version": "9.5.0",
4
4
  "type": "module",
5
5
  "description": "A module that integrates Serwist into your Next.js application.",
6
6
  "files": [
@@ -80,32 +80,32 @@
80
80
  "glob": "10.5.0",
81
81
  "kolorist": "1.8.0",
82
82
  "semver": "7.7.3",
83
- "zod": "4.2.1",
84
- "@serwist/build": "9.4.3",
85
- "@serwist/utils": "9.4.3",
86
- "@serwist/webpack-plugin": "9.4.3",
87
- "@serwist/window": "9.4.3",
88
- "serwist": "9.4.3"
83
+ "zod": "4.3.5",
84
+ "@serwist/build": "9.5.0",
85
+ "@serwist/utils": "9.5.0",
86
+ "@serwist/webpack-plugin": "9.5.0",
87
+ "@serwist/window": "9.5.0",
88
+ "serwist": "9.5.0"
89
89
  },
90
90
  "devDependencies": {
91
91
  "@types/node": "25.0.3",
92
92
  "@types/react": "19.2.7",
93
93
  "@types/semver": "7.7.1",
94
- "next": "16.1.0",
94
+ "next": "16.1.1",
95
95
  "react": "19.2.3",
96
96
  "react-dom": "19.2.3",
97
- "rollup": "4.54.0",
97
+ "rollup": "4.55.1",
98
98
  "type-fest": "5.3.1",
99
99
  "typescript": "5.9.3",
100
100
  "webpack": "5.104.1",
101
- "@serwist/cli": "9.4.3",
102
- "@serwist/configs": "9.4.3"
101
+ "@serwist/cli": "9.5.0",
102
+ "@serwist/configs": "9.5.0"
103
103
  },
104
104
  "peerDependencies": {
105
105
  "next": ">=14.0.0",
106
106
  "react": ">=18.0.0",
107
107
  "typescript": ">=5.0.0",
108
- "@serwist/cli": "^9.4.3"
108
+ "@serwist/cli": "^9.5.0"
109
109
  },
110
110
  "peerDependenciesMeta": {
111
111
  "@serwist/cli": {
@@ -2,21 +2,50 @@ import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { rebasePath } from "@serwist/build";
4
4
  import type { BuildOptions } from "@serwist/cli";
5
- import { PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD } from "next/constants.js";
5
+ import type { NextConfigComplete } from "next/dist/server/config-shared.js";
6
6
  import type { SerwistOptions } from "./lib/config/types.js";
7
- import { generateGlobPatterns, loadBrowserslist } from "./lib/config/utils.js";
7
+ import { generateGlobPatterns, loadBrowserslist, loadNextConfig } from "./lib/config/utils.js";
8
8
 
9
- import loadNextConfig = require("next/dist/server/config.js");
9
+ const _cwd = process.cwd();
10
+ const _isDev = process.env.NODE_ENV === "development";
10
11
 
11
- export const serwist = async (options: SerwistOptions): Promise<BuildOptions> => {
12
- const cwd = process.cwd();
13
- const isDev = process.env.NODE_ENV === "development";
14
- const nextPhase = isDev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_BUILD;
15
- const config = await loadNextConfig.default(nextPhase, cwd, {
16
- silent: false,
17
- });
18
- const basePath = config.basePath || "/";
19
- let distDir = config.distDir;
12
+ /**
13
+ * Additional build context.
14
+ */
15
+ export interface SerwistContext {
16
+ /**
17
+ * The current working directory.
18
+ */
19
+ cwd?: string;
20
+ /**
21
+ * Whether Serwist is in development mode. This option determines how Next.js configuration
22
+ * is resolved. Note that it doesn't change how the service worker is built.
23
+ */
24
+ isDev?: boolean;
25
+ }
26
+
27
+ export interface Serwist {
28
+ /**
29
+ * Integrates Serwist into your Next.js app.
30
+ * @param options
31
+ * @returns
32
+ */
33
+ (options: SerwistOptions, nextConfig?: NextConfigComplete, context?: SerwistContext): Promise<BuildOptions>;
34
+ /**
35
+ * Integrates Serwist into your Next.js app. Allows reading fully resolved Next.js configuration.
36
+ * @param optionsFunction
37
+ * @returns
38
+ */
39
+ withNextConfig: (
40
+ optionsFunction: (nextConfig: NextConfigComplete) => Promise<SerwistOptions> | SerwistOptions,
41
+ context?: SerwistContext,
42
+ ) => Promise<BuildOptions>;
43
+ }
44
+
45
+ export const serwist: Serwist = async (options, nextConfig, { cwd = _cwd, isDev = _isDev } = {}) => {
46
+ if (!nextConfig) nextConfig = await loadNextConfig(cwd, isDev);
47
+ const basePath = nextConfig.basePath || "/";
48
+ let distDir = nextConfig.distDir;
20
49
  if (distDir[0] === "/") distDir = distDir.slice(1);
21
50
  if (distDir[distDir.length - 1] !== "/") distDir += "/";
22
51
  const distServerDir = `${distDir}server/`;
@@ -73,7 +102,7 @@ export const serwist = async (options: SerwistOptions): Promise<BuildOptions> =>
73
102
  }
74
103
  // Replace all references to "$(distDir)" with "$(assetPrefix)/_next/".
75
104
  if (m.url.startsWith(distDir)) {
76
- m.url = `${config.assetPrefix ?? ""}/_next/${m.url.slice(distDir.length)}`;
105
+ m.url = `${nextConfig.assetPrefix ?? ""}/_next/${m.url.slice(distDir.length)}`;
77
106
  }
78
107
  // Replace all references to public/ with "$(basePath)/".
79
108
  if (m.url.startsWith("public/")) {
@@ -91,6 +120,11 @@ export const serwist = async (options: SerwistOptions): Promise<BuildOptions> =>
91
120
  };
92
121
  };
93
122
 
123
+ serwist.withNextConfig = async (optionsFunction, { cwd = _cwd, isDev = _isDev } = {}) => {
124
+ const nextConfig = await loadNextConfig(cwd, isDev);
125
+ return serwist(await optionsFunction(nextConfig), nextConfig, { cwd, isDev });
126
+ };
127
+
94
128
  export { generateGlobPatterns };
95
129
 
96
130
  export type { SerwistOptions };
@@ -1,7 +1,16 @@
1
1
  import browserslist from "browserslist";
2
- import { MODERN_BROWSERSLIST_TARGET } from "next/constants.js";
2
+ import { MODERN_BROWSERSLIST_TARGET, PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD } from "next/constants.js";
3
3
  import { SUPPORTED_ESBUILD_TARGETS, UNSUPPORTED_BROWSERLIST_TARGETS } from "./constants.js";
4
4
 
5
+ import nextConfig = require("next/dist/server/config.js");
6
+
7
+ export const loadNextConfig = (cwd: string, isDev: boolean) => {
8
+ const nextPhase = isDev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_BUILD;
9
+ return nextConfig.default(nextPhase, cwd, {
10
+ silent: false,
11
+ });
12
+ };
13
+
5
14
  export const generateGlobPatterns = (distDir: string) => [
6
15
  `${distDir}static/**/*.{js,css,html,ico,apng,png,avif,jpg,jpeg,jfif,pjpeg,pjp,gif,svg,webp,json,webmanifest}`,
7
16
  "public/**/*",