UNPKG

@serwist/next

Version:

A module that integrates Serwist into your Next.js application.

1 lines 9.57 kB
{"version":3,"file":"index.config.mjs","names":["PHASE_DEVELOPMENT_SERVER","PHASE_PRODUCTION_BUILD","NextConfigComplete","nextConfig","loadNextConfig","cwd","isDev","Promise","nextPhase","default","silent","generateGlobPatterns","distDir","fs","path","rebasePath","BuildOptions","browserslistToEsbuild","browserslist","MODERN_BROWSERSLIST_TARGET","NextConfigComplete","SerwistOptions","generateGlobPatterns","loadNextConfig","_cwd","process","cwd","_isDev","env","NODE_ENV","SerwistContext","isDev","Serwist","options","nextConfig","context","Promise","withNextConfig","optionsFunction","serwist","basePath","distDir","slice","length","distServerDir","distAppDir","distPagesDir","precachePrerendered","globDirectory","cliOptions","file","swDest","rmSync","force","dontCacheBustURLsMatching","RegExp","disablePrecacheManifest","globPatterns","globIgnores","baseDirectory","swSrc","manifestTransforms","manifestEntries","manifest","map","m","url","startsWith","endsWith","lastIndexOf","substring","posix","join","assetPrefix","warnings","esbuildOptions","target"],"sources":["../src/lib/config/utils.ts","../src/index.config.ts"],"sourcesContent":["import { PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD } from \"next/constants.js\";\nimport type { NextConfigComplete } from \"next/dist/server/config-shared.js\";\n\nimport nextConfig = require(\"next/dist/server/config.js\");\n\nexport const loadNextConfig = (cwd: string, isDev: boolean): Promise<NextConfigComplete> => {\n const nextPhase = isDev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_BUILD;\n return nextConfig.default(nextPhase, cwd, {\n silent: false,\n });\n};\n\nexport const generateGlobPatterns = (distDir: string): string[] => [\n `${distDir}static/**/*.{js,css,html,ico,apng,png,avif,jpg,jpeg,jfif,pjpeg,pjp,gif,svg,webp,json,webmanifest}`,\n \"public/**/*\",\n];\n","import fs from \"node:fs\";\nimport path from \"node:path\";\nimport { rebasePath } from \"@serwist/build\";\nimport type { BuildOptions } from \"@serwist/cli\";\nimport { browserslistToEsbuild } from \"@serwist/utils\";\nimport browserslist from \"browserslist\";\nimport { MODERN_BROWSERSLIST_TARGET } from \"next/constants.js\";\nimport type { NextConfigComplete } from \"next/dist/server/config-shared.js\";\nimport type { SerwistOptions } from \"./lib/config/types.js\";\nimport { generateGlobPatterns, loadNextConfig } from \"./lib/config/utils.js\";\n\nconst _cwd = process.cwd();\nconst _isDev = process.env.NODE_ENV === \"development\";\n\n/**\n * Additional build context.\n */\nexport interface SerwistContext {\n /**\n * The current working directory.\n */\n cwd?: string;\n /**\n * Whether Serwist is in development mode. This option determines how Next.js configuration\n * is resolved. Note that it doesn't change how the service worker is built.\n */\n isDev?: boolean;\n}\n\nexport interface Serwist {\n /**\n * Integrates Serwist into your Next.js app.\n * @param options\n * @returns\n */\n (options: SerwistOptions, nextConfig?: NextConfigComplete, context?: SerwistContext): Promise<BuildOptions>;\n /**\n * Integrates Serwist into your Next.js app. Allows reading fully resolved Next.js configuration.\n * @param optionsFunction\n * @returns\n */\n withNextConfig: (\n optionsFunction: (nextConfig: NextConfigComplete) => Promise<SerwistOptions> | SerwistOptions,\n context?: SerwistContext,\n ) => Promise<BuildOptions>;\n}\n\nexport const serwist: Serwist = async (options, nextConfig, { cwd = _cwd, isDev = _isDev } = {}) => {\n if (!nextConfig) nextConfig = await loadNextConfig(cwd, isDev);\n const basePath = nextConfig.basePath || \"/\";\n let distDir = nextConfig.distDir;\n if (distDir[0] === \"/\") distDir = distDir.slice(1);\n if (distDir[distDir.length - 1] !== \"/\") distDir += \"/\";\n const distServerDir = `${distDir}server/`;\n const distAppDir = `${distServerDir}app/`;\n const distPagesDir = `${distServerDir}pages/`;\n const { precachePrerendered = true, globDirectory = cwd, ...cliOptions } = options;\n for (const file of [cliOptions.swDest, `${cliOptions.swDest}.map`]) {\n fs.rmSync(file, { force: true });\n }\n return {\n dontCacheBustURLsMatching: new RegExp(`^${distDir}static/`),\n disablePrecacheManifest: isDev,\n ...cliOptions,\n globDirectory,\n globPatterns: [\n ...(cliOptions.globPatterns ?? generateGlobPatterns(distDir)),\n ...(precachePrerendered ? [`${distServerDir}{app,pages}/**/*.html`] : []),\n ],\n globIgnores: [\n `${distAppDir}**/_not-found.html`,\n `${distAppDir}_global-error*`,\n `${distPagesDir}404.html`,\n `${distPagesDir}500.html`,\n ...(cliOptions.globIgnores ?? []),\n rebasePath({ baseDirectory: globDirectory, file: cliOptions.swSrc }),\n rebasePath({ baseDirectory: globDirectory, file: cliOptions.swDest }),\n rebasePath({ baseDirectory: globDirectory, file: `${cliOptions.swDest}.map` }),\n ],\n manifestTransforms: [\n ...(cliOptions.manifestTransforms ?? []),\n (manifestEntries) => {\n const manifest = manifestEntries.map((m) => {\n if (m.url.startsWith(distAppDir)) {\n // Keep the prefixing slash.\n m.url = m.url.slice(distAppDir.length - 1);\n }\n if (m.url.startsWith(distPagesDir)) {\n // Keep the prefixing slash.\n m.url = m.url.slice(distPagesDir.length - 1);\n }\n if (m.url.endsWith(\".html\")) {\n // trailingSlash: true && output: 'export'\n // or root index.html.\n // https://nextjs.org/docs/app/api-reference/config/next-config-js/trailingSlash\n // \"/abc/index.html\" -> \"/abc/\"\n // \"/index.html\" -> \"/\"\n if (m.url.endsWith(\"/index.html\")) {\n m.url = m.url.slice(0, m.url.lastIndexOf(\"/\") + 1);\n }\n // \"/xxx.html\" -> \"/xxx\"\n else {\n m.url = m.url.substring(0, m.url.lastIndexOf(\".\"));\n }\n m.url = path.posix.join(basePath, m.url);\n }\n // Replace all references to \"$(distDir)\" with \"$(assetPrefix)/_next/\".\n if (m.url.startsWith(distDir)) {\n m.url = `${nextConfig.assetPrefix ?? \"\"}/_next/${m.url.slice(distDir.length)}`;\n }\n // Replace all references to public/ with \"$(basePath)/\".\n if (m.url.startsWith(\"public/\")) {\n m.url = path.posix.join(basePath, m.url.slice(7));\n }\n return m;\n });\n return { manifest, warnings: [] };\n },\n ],\n esbuildOptions: {\n ...cliOptions.esbuildOptions,\n target: cliOptions.esbuildOptions?.target ?? browserslistToEsbuild(browserslist, cwd, MODERN_BROWSERSLIST_TARGET),\n },\n };\n};\n\nserwist.withNextConfig = async (optionsFunction, { cwd = _cwd, isDev = _isDev } = {}) => {\n const nextConfig = await loadNextConfig(cwd, isDev);\n return serwist(await optionsFunction(nextConfig), nextConfig, { cwd, isDev });\n};\n\nexport { generateGlobPatterns };\n\nexport type { SerwistOptions };\n"],"mappings":";;;;;;;;;MAGOG,cAAAA,8CAAAA,EAAqB,6BAAA;AAE5B,MAAaC,kBAAkBC,KAAaC,UAAgD;CAC1F,MAAME,YAAYF,QAAQN,2BAA2BC;AACrD,QAAOE,WAAWM,QAAQD,WAAWH,KAAK,EACxCK,QAAQ,OACT,CAAC;;AAGJ,MAAaC,wBAAwBC,YAA8B,CACjE,GAAGA,QAAO,oGACV,cACD;;;ACJD,MAAMY,OAAOC,QAAQC,KAAK;AAC1B,MAAMC,SAASF,QAAQG,IAAIC,aAAa;AAmCxC,MAAaU,UAAmB,OAAON,SAASC,YAAY,EAAER,MAAMF,MAAMO,QAAQJ,WAAW,EAAE,KAAK;AAClG,KAAI,CAACO,WAAYA,cAAa,MAAMX,eAAeG,KAAKK,MAAM;CAC9D,MAAMS,WAAWN,WAAWM,YAAY;CACxC,IAAIC,UAAUP,WAAWO;AACzB,KAAIA,QAAQ,OAAO,IAAKA,WAAUA,QAAQC,MAAM,EAAE;AAClD,KAAID,QAAQA,QAAQE,SAAS,OAAO,IAAKF,YAAW;CACpD,MAAMG,gBAAgB,GAAGH,QAAO;CAChC,MAAMI,aAAa,GAAGD,cAAa;CACnC,MAAME,eAAe,GAAGF,cAAa;CACrC,MAAM,EAAEG,sBAAsB,MAAMC,gBAAgBtB,KAAK,GAAGuB,eAAehB;AAC3E,MAAK,MAAMiB,QAAQ,CAACD,WAAWE,QAAQ,GAAGF,WAAWE,OAAM,MAAO,CAChEtC,IAAGuC,OAAOF,MAAM,EAAEG,OAAO,MAAM,CAAC;AAElC,QAAO;EACLC,2BAA2B,IAAIC,OAAO,IAAId,QAAO,SAAU;EAC3De,yBAA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