{"version":3,"sources":["../../../../src/build/webpack/plugins/eval-source-map-dev-tool-plugin.ts"],"sourcesContent":["/*\n\tMIT License http://www.opensource.org/licenses/mit-license.php\n\tAuthor Tobias Koppers @sokra\n\n  Forked to add support for `ignoreList`.\n*/\nimport {\n  type webpack,\n  type SourceMapDevToolPluginOptions,\n  ConcatenatedModule,\n  makePathsAbsolute,\n  ModuleFilenameHelpers,\n  NormalModule,\n  RuntimeGlobals,\n  SourceMapDevToolModuleOptionsPlugin,\n} from 'next/dist/compiled/webpack/webpack'\nimport type { RawSourceMap } from 'next/dist/compiled/source-map'\n\nconst cache = new WeakMap<webpack.sources.Source, webpack.sources.Source>()\n\nconst devtoolWarningMessage = `/*\n * ATTENTION: An \"eval-source-map\" devtool has been used.\n * This devtool is neither made for production nor for readable output files.\n * It uses \"eval()\" calls to create a separate source file with attached SourceMaps in the browser devtools.\n * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)\n * or disable the default devtool with \"devtool: false\".\n * If you are looking for production-ready output files, see mode: \"production\" (https://webpack.js.org/configuration/mode/).\n */\n`\n\n// @ts-expect-error -- can't compare `string` with `number` in `version`Ï\ninterface SourceMap extends RawSourceMap {\n  ignoreList?: number[]\n  version: number\n}\n\nexport interface EvalSourceMapDevToolPluginOptions\n  extends SourceMapDevToolPluginOptions {\n  // Fork\n  shouldIgnorePath?: (modulePath: string) => boolean\n}\n\n// Fork of webpack's EvalSourceMapDevToolPlugin with support for adding `ignoreList`.\n// https://github.com/webpack/webpack/blob/e237b580e2bda705c5ab39973f786f7c5a7026bc/lib/EvalSourceMapDevToolPlugin.js#L37\nexport default class EvalSourceMapDevToolPlugin {\n  sourceMapComment: string\n  moduleFilenameTemplate: NonNullable<\n    EvalSourceMapDevToolPluginOptions['moduleFilenameTemplate']\n  >\n  namespace: NonNullable<EvalSourceMapDevToolPluginOptions['namespace']>\n  options: EvalSourceMapDevToolPluginOptions\n  shouldIgnorePath: (modulePath: string) => boolean\n\n  /**\n   * @param inputOptions Options object\n   */\n  constructor(inputOptions: EvalSourceMapDevToolPluginOptions) {\n    let options: EvalSourceMapDevToolPluginOptions\n    if (typeof inputOptions === 'string') {\n      options = {\n        append: inputOptions,\n      }\n    } else {\n      options = inputOptions\n    }\n    this.sourceMapComment =\n      options.append && typeof options.append !== 'function'\n        ? options.append\n        : '//# sourceURL=[module]\\n//# sourceMappingURL=[url]'\n    this.moduleFilenameTemplate =\n      options.moduleFilenameTemplate ||\n      'webpack://[namespace]/[resource-path]?[hash]'\n    this.namespace = options.namespace || ''\n    this.options = options\n\n    // fork\n    this.shouldIgnorePath = options.shouldIgnorePath ?? (() => false)\n  }\n\n  /**\n   * Apply the plugin\n   * @param compiler the compiler instance\n   */\n  apply(compiler: webpack.Compiler): void {\n    const options = this.options\n    compiler.hooks.compilation.tap(\n      'NextJSEvalSourceMapDevToolPlugin',\n      (compilation) => {\n        const { JavascriptModulesPlugin } = compiler.webpack.javascript\n        const { RawSource, ConcatSource } = compiler.webpack.sources\n\n        const devtoolWarning = new RawSource(devtoolWarningMessage)\n\n        const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation)\n\n        new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation)\n        const matchModule = ModuleFilenameHelpers.matchObject.bind(\n          ModuleFilenameHelpers,\n          options\n        )\n\n        hooks.renderModuleContent.tap(\n          'NextJSEvalSourceMapDevToolPlugin',\n          (source, m, { chunk, runtimeTemplate, chunkGraph }) => {\n            const cachedSource = cache.get(source)\n            if (cachedSource !== undefined) {\n              return cachedSource\n            }\n\n            const result = (\n              r: webpack.sources.Source\n            ): webpack.sources.Source => {\n              cache.set(source, r)\n              return r\n            }\n\n            if (m instanceof NormalModule) {\n              const module = m\n              if (!matchModule(module.resource)) {\n                return result(source)\n              }\n            } else if (m instanceof ConcatenatedModule) {\n              const concatModule = m\n              if (concatModule.rootModule instanceof NormalModule) {\n                const module = concatModule.rootModule\n                if (!matchModule(module.resource)) {\n                  return result(source)\n                }\n              } else {\n                return result(source)\n              }\n            } else {\n              return result(source)\n            }\n\n            const namespace = compilation.getPath(this.namespace, {\n              chunk,\n            })\n            let sourceMap: SourceMap\n            let content\n            if (source.sourceAndMap) {\n              const sourceAndMap = source.sourceAndMap(options)\n              sourceMap = sourceAndMap.map as SourceMap\n              content = sourceAndMap.source\n            } else {\n              sourceMap = source.map(options) as SourceMap\n              content = source.source()\n            }\n            if (!sourceMap) {\n              return result(source)\n            }\n\n            // Clone (flat) the sourcemap to ensure that the mutations below do not persist.\n            sourceMap = { ...sourceMap }\n            const context = compiler.options.context!\n            const root = compiler.root\n            const modules = sourceMap.sources.map((sourceMapSource) => {\n              if (!sourceMapSource.startsWith('webpack://'))\n                return sourceMapSource\n              sourceMapSource = makePathsAbsolute(\n                context,\n                sourceMapSource.slice(10),\n                root\n              )\n              const module = compilation.findModule(sourceMapSource)\n              return module || sourceMapSource\n            })\n            let moduleFilenames = modules.map((module) =>\n              ModuleFilenameHelpers.createFilename(\n                module,\n                {\n                  moduleFilenameTemplate: this.moduleFilenameTemplate,\n                  namespace,\n                },\n                {\n                  requestShortener: runtimeTemplate.requestShortener,\n                  chunkGraph,\n                  hashFunction: compilation.outputOptions.hashFunction!,\n                }\n              )\n            )\n            moduleFilenames = ModuleFilenameHelpers.replaceDuplicates(\n              moduleFilenames,\n              (filename, _i, n) => {\n                for (let j = 0; j < n; j++) filename += '*'\n                return filename\n              }\n            )\n            sourceMap.sources = moduleFilenames\n            sourceMap.ignoreList = []\n            for (let index = 0; index < moduleFilenames.length; index++) {\n              if (this.shouldIgnorePath(moduleFilenames[index])) {\n                sourceMap.ignoreList.push(index)\n              }\n            }\n            if (options.noSources) {\n              sourceMap.sourcesContent = undefined\n            }\n            sourceMap.sourceRoot = options.sourceRoot || ''\n            const moduleId = chunkGraph.getModuleId(m)\n            if (moduleId) {\n              sourceMap.file =\n                typeof moduleId === 'number' ? `${moduleId}.js` : moduleId\n            }\n\n            const footer = `${this.sourceMapComment.replace(\n              /\\[url\\]/g,\n              `data:application/json;charset=utf-8;base64,${Buffer.from(\n                JSON.stringify(sourceMap),\n                'utf8'\n              ).toString('base64')}`\n            )}\\n//# sourceURL=webpack-internal:///${moduleId}\\n` // workaround for chrome bug\n\n            return result(\n              new RawSource(\n                `eval(${\n                  compilation.outputOptions.trustedTypes\n                    ? `${RuntimeGlobals.createScript}(${JSON.stringify(\n                        content + footer\n                      )})`\n                    : JSON.stringify(content + footer)\n                });`\n              )\n            )\n          }\n        )\n        hooks.inlineInRuntimeBailout.tap(\n          'EvalDevToolModulePlugin',\n          () => 'the eval-source-map devtool is used.'\n        )\n        hooks.render.tap('EvalSourceMapDevToolPlugin', (source, context) => {\n          if (\n            context.chunk.id === 'webpack' ||\n            context.chunk.id === 'webpack-runtime'\n          ) {\n            const sourceURL = new URL(\n              'webpack-internal://nextjs/' + context.chunk.id + '.js'\n            )\n            // Webpack runtime chunks are not sourcemapped.\n            // We insert a dummy source map that ignore-lists everything.\n            // The mappings will be incorrect but end-users will almost never interact\n            // with the webpack runtime. Users who do, can follow the instructions\n            // in the sources content and disable sourcemaps in their debugger.\n            const sourceMappingURL = new URL(\n              'data:application/json;charset=utf-8;base64,' +\n                Buffer.from(\n                  JSON.stringify({\n                    version: 3,\n                    ignoreList: [0],\n                    // Minimal, parseable mappings.\n                    mappings: 'AAAA',\n                    sources: [\n                      // TODO: This should be the original source URL so that CTRL+Click works in the terminal.\n                      sourceURL.toString(),\n                    ],\n                    sourcesContent: [\n                      '' +\n                        '// This source was generated by Next.js based off of the generated Webpack runtime.\\n' +\n                        '// The mappings are incorrect.\\n' +\n                        '// To get the correct line/column mappings, turn off sourcemaps in your debugger.\\n' +\n                        '\\n' +\n                        source.source(),\n                    ],\n                  })\n                ).toString('base64')\n            )\n\n            return new ConcatSource(\n              source,\n              new RawSource(\n                '\\n//# sourceMappingURL=' +\n                  sourceMappingURL +\n                  // Webpack will add a trailing semicolon. Adding a newline\n                  // ensures the semicolon is not part of the sourceURL and actually\n                  // terminates the previous statement.\n                  '\\n'\n              )\n              // We don't need to add a sourceURL here because that's handled by\n              // whatever is running this script. It'll be the file in Node.js\n              // and the static asset path in the browser.\n            )\n          } else {\n            return new ConcatSource(devtoolWarning, source)\n          }\n        })\n        hooks.chunkHash.tap('EvalSourceMapDevToolPlugin', (_chunk, hash) => {\n          hash.update('EvalSourceMapDevToolPlugin')\n          hash.update('2')\n        })\n        if (compilation.outputOptions.trustedTypes) {\n          compilation.hooks.additionalModuleRuntimeRequirements.tap(\n            'EvalSourceMapDevToolPlugin',\n            (_module, set, _context) => {\n              set.add(RuntimeGlobals.createScript)\n            }\n          )\n        }\n      }\n    )\n  }\n}\n"],"names":["EvalSourceMapDevToolPlugin","cache","WeakMap","devtoolWarningMessage","constructor","inputOptions","options","append","sourceMapComment","moduleFilenameTemplate","namespace","shouldIgnorePath","apply","compiler","hooks","compilation","tap","JavascriptModulesPlugin","webpack","javascript","RawSource","ConcatSource","sources","devtoolWarning","getCompilationHooks","SourceMapDevToolModuleOptionsPlugin","matchModule","ModuleFilenameHelpers","matchObject","bind","renderModuleContent","source","m","chunk","runtimeTemplate","chunkGraph","cachedSource","get","undefined","result","r","set","NormalModule","module","resource","ConcatenatedModule","concatModule","rootModule","getPath","sourceMap","content","sourceAndMap","map","context","root","modules","sourceMapSource","startsWith","makePathsAbsolute","slice","findModule","moduleFilenames","createFilename","requestShortener","hashFunction","outputOptions","replaceDuplicates","filename","_i","n","j","ignoreList","index","length","push","noSources","sourcesContent","sourceRoot","moduleId","getModuleId","file","footer","replace","Buffer","from","JSON","stringify","toString","trustedTypes","RuntimeGlobals","createScript","inlineInRuntimeBailout","render","id","sourceURL","URL","sourceMappingURL","version","mappings","chunkHash","_chunk","hash","update","additionalModuleRuntimeRequirements","_module","_context","add"],"mappings":"AAAA;;;;;AAKA;;;;+BAqCA,qFAAqF;AACrF,yHAAyH;AACzH;;;eAAqBA;;;yBA7Bd;AAGP,MAAMC,QAAQ,IAAIC;AAElB,MAAMC,wBAAwB,CAAC;;;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