Skip to content

Sourcemap 选项

¥Sourcemap Options

  • 类型:boolean | 'inline' | 'hidden' | SourcemapOptions

    ¥Type: boolean | 'inline' | 'hidden' | SourcemapOptions

  • 默认值:false

    ¥Default: false

控制源码映射的生成和配置。

¥Control source map generation and configuration.

sourcemap

  • 类型:boolean | 'inline' | 'hidden'

    ¥Type: boolean | 'inline' | 'hidden'

  • 默认值:false

    ¥Default: false

  • 路径:output.sourcemap

    ¥Path: output.sourcemap

控制源码映射生成。

¥Controls source map generation.

示例

¥Examples

js
export default {
  output: {
    sourcemap: true,
  },
};

深入探讨

¥In-depth

可用的选项:

¥The available options:

  • false:无源映射

    ¥false: No source maps

  • true:生成单独的 .map 文件

    ¥true: Generates separate .map files

  • 'inline':将源码映射作为数据 URI 嵌入到输出中

    ¥'inline': Embeds source map as data URI in output

  • 'hidden':生成源码映射时不添加 //# sourceMappingURL 注释

    ¥'hidden': Generates source maps without adding //# sourceMappingURL comment

sourcemapBaseUrl

  • 类型:string

    ¥Type: string

  • 可选:是 ✅

    ¥Optional: Yes ✅

  • 路径:output.sourcemapBaseUrl

    ¥Path: output.sourcemapBaseUrl

在源映射中添加到源路径前的基准 URL。这在将源映射部署到与代码不同的位置(例如 CDN 或单独的调试服务器)时非常有用。

¥Base URL to prepend to source paths in the source map. This is useful when deploying source maps to a different location than your code, such as a CDN or separate debugging server.

示例

¥Examples

js
export default {
  output: {
    sourcemap: true,
    sourcemapBaseUrl: 'https://example.com/src/',
  },
};

sourcemapDebugIds

  • 类型:boolean

    ¥Type: boolean

  • 默认值:false

    ¥Default: false

  • 路径:output.sourcemapDebugIds

    ¥Path: output.sourcemapDebugIds

将调试 ID 添加到源映射,以便更好地跟踪和调试错误。启用后,Rolldown 会向源映射添加唯一标识符,错误跟踪服务可以使用这些标识符将堆栈跟踪与特定构建进行匹配。

¥Add debug IDs to source maps for better error tracking and debugging. When enabled, Rolldown adds unique identifiers to source maps, which can be used by error tracking services to match stack traces to specific builds.

示例

¥Examples

js
export default {
  output: {
    sourcemap: true,
    sourcemapDebugIds: true,
  },
};

sourcemapIgnoreList

  • 类型:boolean | string | RegExp | ((source: string, sourcemapPath: string) => boolean)

    ¥Type: boolean | string | RegExp | ((source: string, sourcemapPath: string) => boolean)

  • 默认值:/node_modules/

    ¥Default: /node_modules/

  • 路径:output.sourcemapIgnoreList

    ¥Path: output.sourcemapIgnoreList

控制哪些源文件包含在源码映射的忽略列表中。忽略列表中的文件将被排除在调试器单步执行和错误堆栈跟踪之外。

¥Control which source files are included in the sourcemap ignore list. Files in the ignore list are excluded from debugger stepping and error stack traces.

示例

¥Examples

js
// Use RegExp for better performance
export default {
  output: {
    sourcemap: true,
    sourcemapIgnoreList: /node_modules/,
  },
};
js
// Use string pattern
export default {
  output: {
    sourcemap: true,
    sourcemapIgnoreList: 'vendor',
  },
};
js
// Use function (has performance overhead)
export default {
  output: {
    sourcemap: true,
    sourcemapIgnoreList: (source, sourcemapPath) => {
      return source.includes('node_modules') || source.includes('.min.');
    },
  },
};

深入探讨

¥In-depth

可用的选项:

¥The available options:

  • false:忽略列表中不包含任何源文件(不忽略任何文件)

    ¥false: Include no source files in the ignore list (do not ignore any files)

  • true:使用默认忽略列表(例如,忽略 node_modules 和压缩文件)

    ¥true: Use the default ignore list (e.g., ignore node_modules and minified files)

  • string:路径中包含此字符串的文件将被包含在忽略列表中。

    ¥string: Files containing this string in their path will be included in the ignore list

  • RegExp:与此正则表达式匹配的文件将被包含在忽略列表中。

    ¥RegExp: Files matching this regular expression will be included in the ignore list

  • function:自定义函数,用于确定是否应忽略源

    ¥function: Custom function to determine if a source should be ignored

性能

使用静态值(booleanstringRegExp)的性能明显优于函数。从 Rust 调用 JavaScript 函数的开销非常高,因此尽可能优先使用静态模式。

¥Using static values (boolean, string, or RegExp) is significantly more performant than functions. Calling JavaScript functions from Rust has extremely high overhead, so prefer static patterns when possible.

sourcemapPathTransform

  • 类型:(relativeSourcePath: string, sourcemapPath: string) => string

    ¥Type: (relativeSourcePath: string, sourcemapPath: string) => string

  • 可选:是 ✅

    ¥Optional: Yes ✅

  • 路径:output.sourcemapPathTransform

    ¥Path: output.sourcemapPathTransform

用于转换源映射中的源路径的函数。

¥Function to transform source paths in the source map.

示例

¥Examples

js
export default {
  output: {
    sourcemap: true,
    sourcemapPathTransform: (relativeSourcePath, sourcemapPath) => {
      // Remove 'src/' prefix from all source paths
      return relativeSourcePath.replace(/^src\//, '');
    },
  },
};
js
import path from 'node:path';

export default {
  output: {
    sourcemap: true,
    sourcemapPathTransform: (relativeSourcePath, sourcemapPath) => {
      return path.relative(process.cwd(), relativeSourcePath);
    },
  },
};

深入探讨

¥In-depth

这允许你自定义源文件路径在生成的源映射中的显示方式,这在以下情况下很有用:

¥This allows you to customize how source file paths appear in the generated source map, which is useful for:

  • 删除或修改路径前缀

    ¥Removing or modifying path prefixes

  • 跨不同环境规范化路径

    ¥Normalizing paths across different environments

  • 根据部署场景调整路径。

    ¥Adjusting paths for deployment scenarios