From 3c711cd56de6c9735f92e41e457353005c2c0d1c Mon Sep 17 00:00:00 2001 From: Benedikt Meurer Date: Sat, 4 Feb 2023 15:08:14 +0100 Subject: [PATCH] feat(x_google_ignoreList): initial support for ignore lists This patch introduces support for the `x_google_ignoreList` field to the `SourceMap` class. This extension was added to the Source Map Revision 3 Proposal[^1] to allow build tools to provide hints to debuggers (e.g. browser DevTools) about which files contain library or framework, and should thus be ignored by default for the purpose of stepping, break on exceptions, and the like[^2]. With this change it's possible for consumers of magic-string's `SourceMap` class to thread through and serialize the newly added `x_google_ignoreList` field (for example this is needed for rollup to support `x_google_ignoreList`). In a follow up change, we will also add support to the `Bundle` class to mark certain sources as ignore listed. [^1]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k [^2]: https://developer.chrome.com/blog/devtools-better-angular-debugging/ --- index.d.ts | 2 ++ src/SourceMap.js | 3 +++ test/MagicString.SourceMap.js | 36 +++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 test/MagicString.SourceMap.js diff --git a/index.d.ts b/index.d.ts index 2ffe0e9..2ec2bba 100644 --- a/index.d.ts +++ b/index.d.ts @@ -38,6 +38,7 @@ export interface DecodedSourceMap { sourcesContent: (string | null)[]; names: string[]; mappings: SourceMapSegment[][]; + x_google_ignoreList?: number[]; } export class SourceMap { @@ -49,6 +50,7 @@ export class SourceMap { sourcesContent: (string | null)[]; names: string[]; mappings: string; + x_google_ignoreList?: number[]; /** * Returns the equivalent of `JSON.stringify(map)` diff --git a/src/SourceMap.js b/src/SourceMap.js index feb1e02..9389315 100644 --- a/src/SourceMap.js +++ b/src/SourceMap.js @@ -22,6 +22,9 @@ export default class SourceMap { this.sourcesContent = properties.sourcesContent; this.names = properties.names; this.mappings = encode(properties.mappings); + if (typeof properties.x_google_ignoreList !== 'undefined') { + this.x_google_ignoreList = properties.x_google_ignoreList; + } } toString() { diff --git a/test/MagicString.SourceMap.js b/test/MagicString.SourceMap.js new file mode 100644 index 0000000..257b010 --- /dev/null +++ b/test/MagicString.SourceMap.js @@ -0,0 +1,36 @@ +const assert = require('assert'); +const MagicString = require('../'); + +require('source-map-support').install(); + +describe('MagicString.SourceMap', () => { + describe('options', () => { + it('preserves ignore list information', () => { + const map = new MagicString.SourceMap({ + file: 'foo.min.js', + sources: ['foo.js'], + sourcesContent: ['42'], + names: [], + mappings: [[0, 0]], + x_google_ignoreList: [0] + }); + + assert.deepEqual(map.x_google_ignoreList, [0]); + }); + }); + + describe('toString', () => { + it('serializes ignore list information', () => { + const map = new MagicString.SourceMap({ + file: 'foo.min.js', + sources: ['foo.js'], + sourcesContent: ['42'], + names: [], + mappings: [[0, 0]], + x_google_ignoreList: [0] + }); + + assert.equal(map.toString(), '{"version":3,"file":"foo.min.js","sources":["foo.js"],"sourcesContent":["42"],"names":[],"mappings":"AAAAA,AAAAA","x_google_ignoreList":[0]}'); + }); + }); +});