{"version":3,"file":"index.config.mjs","names":["PHASE_DEVELOPMENT_SERVER","PHASE_PRODUCTION_BUILD","nextConfig","loadNextConfig","cwd","isDev","nextPhase","default","silent","generateGlobPatterns","distDir","fs","path","rebasePath","browserslistToEsbuild","browserslist","MODERN_BROWSERSLIST_TARGET","generateGlobPatterns","loadNextConfig","_cwd","process","cwd","_isDev","env","NODE_ENV","isDev","options","nextConfig","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":";;;;;;;;AAGOE,MAAAA,cAAAA,uB,gCAAAA,EAAAA,CAAqB,4BAAA;AAE5B,MAAaC,kBAAkBC,KAAaC,UAAgD;CAC1F,MAAMC,YAAYD,QAAQL,2BAA2BC;CACrD,OAAOC,WAAWK,QAAQD,WAAWF,KAAK,EACxCI,QAAQ,MACV,CAAC;AACH;AAEA,MAAaC,wBAAwBC,YAA8B,CACjE,GAAGA,QAAO,oGACV,aAAa;;ACHf,MAAMS,OAAOC,QAAQC,IAAI;AACzB,MAAMC,SAASF,QAAQG,IAAIC,aAAa;AAmCxC,MAAaM,UAAmB,OAAOJ,SAASC,YAAY,EAAEN,MAAMF,MAAMM,QAAQH,WAAW,CAAC,MAAM;CAClG,IAAI,CAACK,YAAYA,aAAa,MAAMT,eAAeG,KAAKI,KAAK;CAC7D,MAAMM,WAAWJ,WAAWI,YAAY;CACxC,IAAIC,UAAUL,WAAWK;CACzB,IAAIA,QAAQ,OAAO,KAAKA,UAAUA,QAAQC,MAAM,CAAC;CACjD,IAAID,QAAQA,QAAQE,SAAS,OAAO,KAAKF,WAAW;CACpD,MAAMG,gBAAgB,GAAGH,QAAO;CAChC,MAAMI,aAAa,GAAGD,cAAa;CACnC,MAAME,eAAe,GAAGF,cAAa;CACrC,MAAM,EAAEG,sBAAsB,MAAMC,gBAAgBlB,KAAK,GAAGmB,eAAed;CAC3E,KAAK,MAAMe,QAAQ,CAACD,WAAWE,QAAQ,GAAGF,WAAWE,OAAM,KAAM,GAC/D/B,GAAGgC,OAAOF,MAAM,EAAEG,OAAO,KAAK,CAAC;CAEjC,OAAO;EACLC,2BAA2B,IAAIC,OAAO,I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