-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: add API for interacting with source maps
PR-URL: #31132 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
Showing
8 changed files
with
252 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
'use strict'; | ||
|
||
module.exports = require('internal/modules/cjs/loader').Module; | ||
const { findSourceMap } = require('internal/source_map/source_map_cache'); | ||
const { Module } = require('internal/modules/cjs/loader'); | ||
const { SourceMap } = require('internal/source_map/source_map'); | ||
|
||
Module.findSourceMap = findSourceMap; | ||
Module.SourceMap = SourceMap; | ||
module.exports = Module; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Flags: --enable-source-maps | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const { findSourceMap, SourceMap } = require('module'); | ||
const { readFileSync } = require('fs'); | ||
|
||
// findSourceMap() can lookup source-maps based on URIs, in the | ||
// non-exceptional case. | ||
{ | ||
require('../fixtures/source-map/disk-relative-path.js'); | ||
const sourceMap = findSourceMap( | ||
require.resolve('../fixtures/source-map/disk-relative-path.js') | ||
); | ||
const { | ||
originalLine, | ||
originalColumn, | ||
originalSource | ||
} = sourceMap.findEntry(0, 29); | ||
assert.strictEqual(originalLine, 2); | ||
assert.strictEqual(originalColumn, 4); | ||
assert(originalSource.endsWith('disk.js')); | ||
} | ||
|
||
// findSourceMap() can be used in Error.prepareStackTrace() to lookup | ||
// source-map attached to error. | ||
{ | ||
let callSite; | ||
let sourceMap; | ||
Error.prepareStackTrace = (error, trace) => { | ||
const throwingRequireCallSite = trace[0]; | ||
if (throwingRequireCallSite.getFileName().endsWith('typescript-throw.js')) { | ||
sourceMap = findSourceMap(throwingRequireCallSite.getFileName(), error); | ||
callSite = throwingRequireCallSite; | ||
} | ||
}; | ||
try { | ||
// Require a file that throws an exception, and has a source map. | ||
require('../fixtures/source-map/typescript-throw.js'); | ||
} catch (err) { | ||
err.stack; // Force prepareStackTrace() to be called. | ||
} | ||
assert(callSite); | ||
assert(sourceMap); | ||
const { | ||
generatedLine, | ||
generatedColumn, | ||
originalLine, | ||
originalColumn, | ||
originalSource | ||
} = sourceMap.findEntry( | ||
callSite.getLineNumber() - 1, | ||
callSite.getColumnNumber() - 1 | ||
); | ||
|
||
assert.strictEqual(generatedLine, 19); | ||
assert.strictEqual(generatedColumn, 14); | ||
|
||
assert.strictEqual(originalLine, 17); | ||
assert.strictEqual(originalColumn, 10); | ||
assert(originalSource.endsWith('typescript-throw.ts')); | ||
} | ||
|
||
// SourceMap can be instantiated with Source Map V3 object as payload. | ||
{ | ||
const payload = JSON.parse(readFileSync( | ||
require.resolve('../fixtures/source-map/disk.map'), 'utf8' | ||
)); | ||
const sourceMap = new SourceMap(payload); | ||
const { | ||
originalLine, | ||
originalColumn, | ||
originalSource | ||
} = sourceMap.findEntry(0, 29); | ||
assert.strictEqual(originalLine, 2); | ||
assert.strictEqual(originalColumn, 4); | ||
assert(originalSource.endsWith('disk.js')); | ||
// The stored payload should be a clone: | ||
assert.strictEqual(payload.mappings, sourceMap.payload.mappings); | ||
assert.notStrictEqual(payload, sourceMap.payload); | ||
assert.strictEqual(payload.sources[0], sourceMap.payload.sources[0]); | ||
assert.notStrictEqual(payload.sources, sourceMap.payload.sources); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters