@ampproject/remapping

中文文档
2.3.0发布于 2024/03/01Apache-2.0TypeScriptESM + CJS近 30 天下载 1.2亿

通过变换重映射顺序源图以指向原始源代码

中文使用文档
文档目录5 节
  1. 安装
  2. 使用量
  3. 文件的多重转换
  4. 对加载图的高级控制
  5. 选项

@ ampproject/ 重新绘图

通过变换重映射顺序源图以指向原始源代码

重映射允许您使用通过转换代码和“再映射”生成的源图 他们到原始来源位置。 想想"我的精密代码, 改变与bail和捆绑 使用webpack),所有指向您原始源代码中的正确位置.

在重映射下,你的任何源代码转换 不需要意识到输入的源图, 它们只需要生成输出源图。 这大大简化了建筑习惯 转换(认为是找到和替换).

安装

npm install @ampproject/remapping

使用量

function remapping(
  map: SourceMap | SourceMap[],
  loader: (file: string, ctx: LoaderContext) => (SourceMap | null | undefined),
  options?: { excludeContent: boolean, decodedMappings: boolean }
): SourceMap;

// LoaderContext gives the loader the importing sourcemap, tree depth, the ability to override the
// "source" location (where child sources are resolved relative to, or the location of original
// source), and the ability to override the "content" of an original source for inclusion in the
// output sourcemap.
type LoaderContext = {
 readonly importer: string;
 readonly depth: number;
 source: string;
 content: string | null | undefined;
}

remapping 取最后输出源图,并 a loader 函数。 每个源文件指针 在源图中 loader 将用一条坚定的道路召唤。 如果路径本身代表 转换文件(它有一个与它相关的酸液图),然后是 loader 应该还回去 源图. 如果没有,该路径将被视为原始的,未转换的源代码.

// Babel transformed "helloworld.js" into "transformed.js"
const transformedMap = JSON.stringify({
  file: 'transformed.js',
  // 1st column of 2nd line of output file translates into the 1st source
  // file, line 3, column 2
  mappings: ';CAEE',
  sources: ['helloworld.js'],
  version: 3,
});

// Uglify minified "transformed.js" into "transformed.min.js"
const minifiedTransformedMap = JSON.stringify({
  file: 'transformed.min.js',
  // 0th column of 1st line of output file translates into the 1st source
  // file, line 2, column 1.
  mappings: 'AACC',
  names: [],
  sources: ['transformed.js'],
  version: 3,
});

const remapped = remapping(
  minifiedTransformedMap,
  (file, ctx) => {

    // The "transformed.js" file is an transformed file.
    if (file === 'transformed.js') {
      // The root importer is empty.
      console.assert(ctx.importer === '');
      // The depth in the sourcemap tree we're currently loading.
      // The root `minifiedTransformedMap` is depth 0, and its source children are depth 1, etc.
      console.assert(ctx.depth === 1);

      return transformedMap;
    }

    // Loader will be called to load transformedMap's source file pointers as well.
    console.assert(file === 'helloworld.js');
    // `transformed.js`'s sourcemap points into `helloworld.js`.
    console.assert(ctx.importer === 'transformed.js');
    // This is a source child of `transformed`, which is a source child of `minifiedTransformedMap`.
    console.assert(ctx.depth === 2);
    return null;
  }
);

console.log(remapped);
// {
//   file: 'transpiled.min.js',
//   mappings: 'AAEE',
//   sources: ['helloworld.js'],
//   version: 3,
// };

在这个例子中, loader 将两次呼叫:

  1. "transformed.js"中的第一个源文件指针 minifiedTransformedMap我们返回 与之相关的源图( 毕竟是一个已转换的文件) , 这样源图位置就可以 被追踪到它所代表的源文件.
  2. "helloworld.js"我们的原始,未修改的源代码。 此文件没有源代码图, 所以 我们回来了 null。 。 。 .

这个 remapped 源图现在从 transformed.min.js 进入 helloworld.js如果 你要读 mappings,它表示"一行输出行的第0栏指向一行 栏 helloworld.js" ..

文件的多重转换

作为方便,如果你对一个文件有多个单源转换,你可以通过一个 数组源图文件,顺序为最近期的转换源图。 注意这一点: 更改 importer 财务报告和审定财务报表 depth 每一个呼叫我们装载器。 所以我们上面的例子可能是 写为:

const remapped = remapping(
  [minifiedTransformedMap, transformedMap],
  () => null
);

console.log(remapped);
// {
//   file: 'transpiled.min.js',
//   mappings: 'AAEE',
//   sources: ['helloworld.js'],
//   version: 3,
// };

对加载图的高级控制

source

这个 source 属性可以覆盖到任何值来改变当前负载的位置。 比如说, 对于原始源文件,它允许我们将位置更改为原始源,无论 来源地图条目所言。 对于转换的文件, 它允许我们改变 相对解析加载源图的儿童源位置.

const remapped = remapping(
  minifiedTransformedMap,
  (file, ctx) => {

    if (file === 'transformed.js') {
      // We pretend the transformed.js file actually exists in the 'src/' directory. When the nested
      // source files are loaded, they will now be relative to `src/`.
      ctx.source = 'src/transformed.js';
      return transformedMap;
    }

    console.assert(file === 'src/helloworld.js');
    // We could futher change the source of this original file, eg, to be inside a nested directory
    // itself. This will be reflected in the remapped sourcemap.
    ctx.source = 'src/nested/transformed.js';
    return null;
  }
);

console.log(remapped);
// {
//   …,
//   sources: ['src/nested/helloworld.js'],
// };

content

这个 content 当我们遇到原始源文件时,属性会被覆盖。 Eg 允许 以手动方式提供原始文件的出处内容,无论 sourcesContent 字段存在于父源图中。 也可以设定为 null 删除 源内容.

const remapped = remapping(
  minifiedTransformedMap,
  (file, ctx) => {

    if (file === 'transformed.js') {
      // transformedMap does not include a `sourcesContent` field, so usually the remapped sourcemap
      // would not include any `sourcesContent` values.
      return transformedMap;
    }

    console.assert(file === 'helloworld.js');
    // We can read the file to provide the source content.
    ctx.content = fs.readFileSync(file, 'utf8');
    return null;
  }
);

console.log(remapped);
// {
//   …,
//   sourcesContent: [
//     'console.log("Hello world!")',
//   ],
// };

选项

不适用

默认, excludeContent 实值 false。通过 { excludeContent: true } 将排除 sourcesContent 字段从返回的源图。 这主要在您想要减少时有用 大小从源图.

解码绘图

默认, decodedMappings 实值 false。通过 { decodedMappings: true } 将离开 mappings 字段内 解码状态 改为 编码为 VLQ 字符串 .

版本与发布记录

@ampproject/remapping 共发布 19 个版本,首次发布于 2019/08/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.3.0latest2024/03/012 年前
2.2.12023/04/073 年前
2.2.02022/04/274 年前
2.1.22022/02/164 年前
2.1.12022/02/114 年前
2.1.02022/02/074 年前

关于 @ampproject/remapping 的常见问题

@ampproject/remapping 是什么?

通过变换重映射顺序源图以指向原始源代码

如何安装 @ampproject/remapping?

在项目目录运行 npm install @ampproject/remapping,或使用 pnpm add / yarn add / bun add。

@ampproject/remapping 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ampproject/remapping 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ampproject/remapping 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ampproject/remapping 可以商用吗?

@ampproject/remapping 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@ampproject/remapping 的下载量有多少?

根据 npm 官方下载统计,@ampproject/remapping 在 2026/10/06 – 2026/11/04 这 30 天内被下载 123,717,432 次,日均约 4,123,914 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ampproject/remapping 还在维护吗?

最新版本 2.3.0 发布于 2024/03/01(2 年前);至今共发布 19 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/08/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。