diff --git a/docs/config/server-options.md b/docs/config/server-options.md index d216bbed486fca..f10cca7ce1db61 100644 --- a/docs/config/server-options.md +++ b/docs/config/server-options.md @@ -318,3 +318,22 @@ export default defineConfig({ }, }) ``` + +## server.sourcemapIgnoreList + +- **Type:** `false | (sourcePath: string, sourcemapPath: string) => boolean` +- **Default:** `(sourcePath) => sourcePath.includes('node_modules')` + +Whether or not to ignore source files in the server sourcemap, used to populate the [`x_google_ignoreList` source map extension](https://developer.chrome.com/blog/devtools-better-angular-debugging/#the-x_google_ignorelist-source-map-extension). + +By default, it excludes all paths containing `node_modules`. You can pass `false` to disable this behavior, or, for full control, a function that takes the source path and sourcemap path and returns whether to ignore the source path. + +```js +export default defineConfig({ + server: { + // This is the default value, and will add all files with node_modules in their paths + // to the ignore list. + sourcemapIgnoreList: (sourcePath, sourcemapPath) => sourcePath.includes('node_modules') + } +}; +``` diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index d90761c6dfed9e..0edf8692835375 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -115,6 +115,17 @@ export interface ServerOptions extends CommonServerOptions { * @default true */ preTransformRequests?: boolean + /** + * Whether or not to ignore-list source files in the dev server sourcemap, used to populate + * the [`x_google_ignoreList` source map extension](https://developer.chrome.com/blog/devtools-better-angular-debugging/#the-x_google_ignorelist-source-map-extension). + * + * By default, it excludes all paths containing `node_modules`. You can pass `false` to + * disable this behavior, or, for full control, a function that takes the source path and + * sourcemap path and returns whether to ignore the source path. + */ + sourcemapIgnoreList?: + | false + | ((sourcePath: string, sourcemapPath: string) => boolean) /** * Force dep pre-optimization regardless of whether deps have changed. * @@ -127,6 +138,10 @@ export interface ServerOptions extends CommonServerOptions { export interface ResolvedServerOptions extends ServerOptions { fs: Required middlewareMode: boolean + sourcemapIgnoreList: Exclude< + ServerOptions['sourcemapIgnoreList'], + false | undefined + > } export interface FileSystemServeOptions { @@ -746,7 +761,12 @@ export function resolveServerOptions( ): ResolvedServerOptions { const server: ResolvedServerOptions = { preTransformRequests: true, - ...(raw as ResolvedServerOptions), + ...(raw as Omit), + sourcemapIgnoreList: + raw?.sourcemapIgnoreList === false + ? () => false + : raw?.sourcemapIgnoreList || + ((sourcePath) => sourcePath.includes('node_modules')), middlewareMode: !!raw?.middlewareMode, } let allowDirs = server.fs?.allow diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index 662db957b7cc8d..ec80c852ec41c5 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -278,6 +278,23 @@ async function loadAndTransform( ) { const sourcePath = map.sources[sourcesIndex] + const sourcemapPath = `${mod.file}.map` + const ignoreList = config.server.sourcemapIgnoreList( + sourcePath, + sourcemapPath, + ) + if (typeof ignoreList !== 'boolean') { + logger.warn('sourcemapIgnoreList function must return a boolean.') + } + if (ignoreList) { + if (map.x_google_ignoreList === undefined) { + map.x_google_ignoreList = [] + } + if (!map.x_google_ignoreList.includes(sourcesIndex)) { + map.x_google_ignoreList.push(sourcesIndex) + } + } + // Rewrite sources to relative paths to give debuggers the chance // to resolve and display them in a meaningful way (rather than // with absolute paths).