Skip to content

Commit

Permalink
feat: add sourcemapIgnoreList configuration option (#12174)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 1, 2023
1 parent e3f725f commit f875580
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
19 changes: 19 additions & 0 deletions docs/config/server-options.md
Expand Up @@ -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')
}
};
```
22 changes: 21 additions & 1 deletion packages/vite/src/node/server/index.ts
Expand Up @@ -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.
*
Expand All @@ -127,6 +138,10 @@ export interface ServerOptions extends CommonServerOptions {
export interface ResolvedServerOptions extends ServerOptions {
fs: Required<FileSystemServeOptions>
middlewareMode: boolean
sourcemapIgnoreList: Exclude<
ServerOptions['sourcemapIgnoreList'],
false | undefined
>
}

export interface FileSystemServeOptions {
Expand Down Expand Up @@ -746,7 +761,12 @@ export function resolveServerOptions(
): ResolvedServerOptions {
const server: ResolvedServerOptions = {
preTransformRequests: true,
...(raw as ResolvedServerOptions),
...(raw as Omit<ResolvedServerOptions, 'sourcemapIgnoreList'>),
sourcemapIgnoreList:
raw?.sourcemapIgnoreList === false
? () => false
: raw?.sourcemapIgnoreList ||
((sourcePath) => sourcePath.includes('node_modules')),
middlewareMode: !!raw?.middlewareMode,
}
let allowDirs = server.fs?.allow
Expand Down
17 changes: 17 additions & 0 deletions packages/vite/src/node/server/transformRequest.ts
Expand Up @@ -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).
Expand Down

0 comments on commit f875580

Please sign in to comment.