{"version":3,"sources":["../../../../src/build/webpack/plugins/nextjs-require-cache-hot-reloader.ts"],"sourcesContent":["import type { webpack } from 'next/dist/compiled/webpack/webpack'\nimport { deleteCache } from '../../../server/dev/require-cache'\nimport { clearModuleContext } from '../../../server/web/sandbox'\nimport path from 'path'\n\ntype Compiler = webpack.Compiler\ntype WebpackPluginInstance = webpack.WebpackPluginInstance\n\nconst RUNTIME_NAMES = ['webpack-runtime', 'webpack-api-runtime']\nconst PLUGIN_NAME = 'NextJsRequireCacheHotReloader'\n\n// This plugin flushes require.cache after emitting the files. Providing 'hot reloading' of server files.\nexport class NextJsRequireCacheHotReloader implements WebpackPluginInstance {\n  prevAssets: any = null\n  serverComponents: boolean\n\n  constructor(opts: { serverComponents: boolean }) {\n    this.serverComponents = opts.serverComponents\n  }\n\n  apply(compiler: Compiler) {\n    compiler.hooks.assetEmitted.tap(PLUGIN_NAME, (_file, { targetPath }) => {\n      // Clear module context in this process\n      clearModuleContext(targetPath)\n      deleteCache([targetPath])\n    })\n\n    compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {\n      // we need to make sure to clear all server entries from cache\n      // since they can have a stale webpack-runtime cache\n      // which needs to always be in-sync\n      const outputPath = compilation.outputOptions.path!\n      const allPaths = RUNTIME_NAMES.map((name) =>\n        path.join(outputPath, `${name}.js`)\n      )\n      for (const entry of compilation.entrypoints.keys()) {\n        if (entry.startsWith('pages/') || entry.startsWith('app/')) {\n          allPaths.push(path.join(outputPath, `${entry}.js`))\n        }\n      }\n\n      deleteCache(allPaths)\n    })\n  }\n}\n"],"names":["NextJsRequireCacheHotReloader","RUNTIME_NAMES","PLUGIN_NAME","constructor","opts","prevAssets","serverComponents","apply","compiler","hooks","assetEmitted","tap","_file","targetPath","clearModuleContext","deleteCache","afterEmit","tapPromise","compilation","outputPath","outputOptions","path","allPaths","map","name","join","entry","entrypoints","keys","startsWith","push"],"mappings":";;;;+BAYaA;;;eAAAA;;;8BAXe;yBACO;6DAClB;;;;;;AAKjB,MAAMC,gBAAgB;IAAC;IAAmB;CAAsB;AAChE,MAAMC,cAAc;AAGb,MAAMF;IAIXG,YAAYC,IAAmC,CAAE;aAHjDC,aAAkB;QAIhB,IAAI,CAACC,gBAAgB,GAAGF,KAAKE,gBAAgB;IAC/C;IAEAC,MAAMC,QAAkB,EAAE;QACxBA,SAASC,KAAK,CAACC,YAAY,CAACC,GAAG,CAACT,aAAa,CAACU,OAAO,EAAEC,UAAU,EAAE;YACjE,uCAAuC;YACvCC,IAAAA,2BAAkB,EAACD;YACnBE,IAAAA,yBAAW,EAAC;gBAACF;aAAW;QAC1B;QAEAL,SAASC,KAAK,CAACO,SAAS,CAACC,UAAU,CAACf,aAAa,OAAOgB;YACtD,8DAA8D;YAC9D,oDAAoD;YACpD,mCAAmC;YACnC,MAAMC,aAAaD,YAAYE,aAAa,CAACC,IAAI;YACjD,MAAMC,WAAWrB,cAAcsB,GAAG,CAAC,CAACC,OAClCH,aAAI,CAACI,IAAI,CAACN,YAAY,GAAGK,KAAK,GAAG,CAAC;YAEpC,KAAK,MAAME,SAASR,YAAYS,WAAW,CAACC,IAAI,GAAI;gBAClD,IAAIF,MAAMG,UAAU,CAAC,aAAaH,MAAMG,UAAU,CAAC,SAAS;oBAC1DP,SAASQ,IAAI,CAACT,aAAI,CAACI,IAAI,CAACN,YAAY,GAAGO,MAAM,GAAG,CAAC;gBACnD;YACF;YAEAX,IAAAA,yBAAW,EAACO;QACd;IACF;AACF","ignoreList":[0]}