{"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":["ConcatenatedModule","makePathsAbsolute","ModuleFilenameHelpers","NormalModule","RuntimeGlobals","SourceMapDevToolModuleOptionsPlugin","cache","WeakMap","devtoolWarningMessage","EvalSourceMapDevToolPlugin","constructor","inputOptions","options","append","sourceMapComment","moduleFilenameTemplate","namespace","shouldIgnorePath","apply","compiler","hooks","compilation","tap","JavascriptModulesPlugin","webpack","javascript","RawSource","ConcatSource","sources","devtoolWarning","getCompilationHooks","matchModule","matchObject","bind","renderModuleContent","source","m","chunk","runtimeTemplate","chunkGraph","cachedSource","get","undefined","result","r","set","module","resource","concatModule","rootModule","getPath","sourceMap","content","sourceAndMap","map","context","root","modules","sourceMapSource","startsWith","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","createScript","inlineInRuntimeBailout","render","id","sourceURL","URL","sourceMappingURL","version","mappings","chunkHash","_chunk","hash","update","additionalModuleRuntimeRequirements","_module","_context","add"],"mappings":"AAAA;;;;;AAKA,GACA,SAGEA,kBAAkB,EAClBC,iBAAiB,EACjBC,qBAAqB,EACrBC,YAAY,EACZC,cAAc,EACdC,mCAAmC,QAC9B,qCAAoC;AAG3C,MAAMC,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