{"modulePath":"github.com/ejfkdev/dj","version":"v0.6.5","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package sourcemap\n\nPackage sourcemap 提供 source map（v3）的解析与原始源码还原功能。\n\n还原策略采用\"sourcesContent 优先 + mappings 回退\"两者结合：\n\n  - 优先路径：直接读取 source map 的 sourcesContent 字段（打包前的原始明文源码）， 按 sources 路径写成分离的源文件。速度快、准确，但依赖打包工具保留了该字段。\n  - 回退路径：当 sourcesContent 缺失或对应元素为空时，解析 mappings（VLQ 编码）， 把压缩后的 JS 按 source 索引切分重组为可读片段。注意：mappings 只能提供位置 映射，无法 100% 还原原文，回退产物会在文件头加注释标注\"非完整源码\"。\n\n## Functions\n\n```go\nfunc NormalizeSourcePath(source, sourceRoot string) string\n```\nNormalizeSourcePath 规范化 source map 中 sources 字段的路径， 使其可作为安全的相对文件路径写入磁盘，保留原始目录层级。\n\n输入示例：\n\n\twebpack:///./src/App.jsx          -\u003e src/App.jsx\n\twebpack:///webpack/bootstrap       -\u003e webpack/bootstrap\n\twebpack-internal:///./node_modules -\u003e node_modules/foo.js\n\t./src/utils.js                     -\u003e src/utils.js\n\t/static/js/app.js                  -\u003e static/js/app.js\n\t../../../../node_modules/foo.js    -\u003e node_modules/foo.js  (.. 按相对路径回溯)\n\t../../etc/passwd                   -\u003e etc/passwd  (超出根的 .. 被丢弃，不逃逸)\n\tfile:///home/user/a.js             -\u003e home/user/a.js\n\nsourceRoot 非空时会作为前缀拼接到规范化后的路径前（同样经过清理）。 返回空字符串表示路径无法规范化（如纯 scheme 无实际路径）。\n\n```go\nfunc SafeFilePath(normalizedPath string) string\n```\nSafeFilePath 将规范化的 source path 转为当前 OS 的文件路径 确保结果始终是相对路径，不会逃逸到上级目录\n\n## Types\n\n```go\ntype Mapping struct {\n\t// GeneratedLine 生成代码（压缩后）的行号，0-based\n\tGeneratedLine int\n\t// GeneratedColumn 生成代码的列号，0-based\n\tGeneratedColumn int\n\t// SourceIndex sources 数组的索引（-1 表示无）\n\tSourceIndex int\n\t// SourceLine 原始源码的行号，0-based（-1 表示无）\n\tSourceLine int\n\t// SourceColumn 原始源码的列号，0-based（-1 表示无）\n\tSourceColumn int\n\t// NameIndex names 数组的索引（-1 表示无）\n\tNameIndex int\n}\n```\nMapping 表示 source map mappings 中的单条映射\n\n```go\nfunc ParseMappings(mappings string) []Mapping\n```\nParseMappings 解析 source map 的 mappings 字符串 mappings 字符串格式：行之间用 ; 分隔，段之间用 , 分隔 每个段是 1、4 或 5 个 VLQ 编码的值：\n\n  - 1 个值：GeneratedColumn（与前一段同 source）\n  - 4 个值：GeneratedColumn, SourceIndex, SourceLine, SourceColumn\n  - 5 个值：GeneratedColumn, SourceIndex, SourceLine, SourceColumn, NameIndex\n\n除 GeneratedColumn 外，所有值都是相对前一段的增量（跨行也保持增量）\n\n```go\ntype RestoreMode int\n```\nRestoreMode 标记还原出的源文件来源\n\n```go\nconst (\n\t// RestoreModeNone 未还原（无可用内容）\n\tRestoreModeNone RestoreMode = iota\n\t// RestoreModeSourcesContent 来自 sourcesContent 字段（完整原文）\n\tRestoreModeSourcesContent\n\t// RestoreModeMappings 来自 mappings 重组（非完整源码）\n\tRestoreModeMappings\n)\n```\n\n```go\nfunc (m RestoreMode) String() string\n```\nString 实现 RestoreMode 的字符串表示，便于调试输出\n\n```go\ntype SourceFile struct {\n\t// Path 规范化后的相对路径（如 src/App.jsx）\n\tPath string\n\t// Content 文件内容\n\tContent []byte\n\t// Mode 还原方式\n\tMode RestoreMode\n}\n```\nSourceFile 还原出的单个源文件\n\n```go\nfunc RestoreFiles(sm *SourceMap, minifiedContent []byte) ([]SourceFile, error)\n```\nRestoreFiles 从 source map 还原出原始源文件列表。\n\n还原顺序：\n\n 1. 优先用 sourcesContent（完整原文）\n 2. 若 sourcesContent 对应元素为空，且 minifiedContent 非空，则用 mappings 重组压缩 JS 片段作为回退\n\n参数 minifiedContent 是压缩后的 JS 内容，仅在 sourcesContent 缺失时用于回退。 若 sourcesContent 完整，可传 nil。\n\n```go\ntype SourceMap struct {\n\t// Version source map 规范版本（通常为 3）\n\tVersion int `json:\"version\"`\n\t// File 生成文件（压缩后的 JS）的名称\n\tFile string `json:\"file,omitempty\"`\n\t// SourceRoot 所有 sources 路径的前缀，用于解析相对路径\n\tSourceRoot string `json:\"sourceRoot,omitempty\"`\n\t// Sources 原始源文件路径列表\n\tSources []string `json:\"sources\"`\n\t// SourcesContent 原始源文件内容列表，与 sources 一一对应；\n\t// 可能为 null、缺失、或某些元素为 null\n\tSourcesContent []string `json:\"sourcesContent,omitempty\"`\n\t// Names mappings 中引用的标识符名称列表\n\tNames []string `json:\"names,omitempty\"`\n\t// Mappings VLQ 编码的位置映射字符串\n\tMappings string `json:\"mappings\"`\n}\n```\nSourceMap 对应 source map v3 规范的 JSON 结构 参考：[https://sourcemaps.info/spec.html](https://sourcemaps.info/spec.html)\n\n```go\nfunc Parse(content []byte) (*SourceMap, error)\n```\nParse 解析 source map JSON 内容\n\n```go\nfunc (sm *SourceMap) HasSourcesContent() bool\n```\nHasSourcesContent 判断 source map 是否包含可用的 sourcesContent 返回 true 当且仅当至少有一个 sourcesContent 元素非空\n\n","path":"github.com/ejfkdev/dj/pkg/sourcemap","name":"sourcemap","synopsis":"Package sourcemap 提供 source map（v3）的解析与原始源码还原功能。","isRedistributable":true}
