-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* run prettier * initial sourcemap * improve sourcemap constructor * also support SourceMapConsumer as addMap input * some cleanup and fixes * add lineOffset and 1:1 sourcemaps * add lineOffset and 1:1 sourcemaps * slight sourcemaputil improvement * use babel-generator instead of double generating * add 1:1 sourcemap for untranspiled assets * update offset * fix source paths * Better offset solution * fix sourceContent not being set by babel-generator * fix small offset bug * add sourcemap option and offset when globals are added * add cli option to disable sourcemaps * ts & coffeescript support * fix most tests * All tests (except hmr) fixed + bugfix for ts * fix hmr tests * comment out error throwing in tests * fix windows tests * add sourcemap tests * add comment to why throwing errors is commented out in tests * update with circular fix, now throws on test failures * fix generator for invalid maps * fix tests * fix tiny issues * small performance improvement * rewrite sourcemap handling, less generator constructing overhead and some more improvements * extendSourceMap bugfix * Use babel rawMappings to remove encoding step * small performance fixes * improve linecounter * small performance improvement and bugfix * small improvement * change for source-map 0.7 compatibility, improves performance a lil * switch to official npm release * tiny improvement * only install source-map 0.7 if possible * remove optional 0.7 dep * Minor refactorings * Store precomputed line count as part of source map * Clean up * Last cleanup
- Loading branch information
1 parent
dba3d49
commit 5c5d5f8
Showing
37 changed files
with
1,005 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ lib | |
!test/**/node_modules | ||
.vscode/ | ||
.idea/ | ||
*.min.js | ||
*.min.js |
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 |
---|---|---|
@@ -0,0 +1,190 @@ | ||
const {SourceMapConsumer, SourceMapGenerator} = require('source-map'); | ||
const lineCounter = require('./utils/lineCounter'); | ||
|
||
class SourceMap { | ||
constructor(mappings, sources) { | ||
this.mappings = mappings || []; | ||
this.sources = sources || {}; | ||
this.lineCount = null; | ||
} | ||
|
||
async getConsumer(map) { | ||
if (map instanceof SourceMapConsumer) { | ||
return map; | ||
} | ||
|
||
return await new SourceMapConsumer(map); | ||
} | ||
|
||
async addMap(map, lineOffset = 0, columnOffset = 0) { | ||
if (!(map instanceof SourceMap) && map.version) { | ||
let consumer = await this.getConsumer(map); | ||
|
||
consumer.eachMapping(mapping => { | ||
this.addConsumerMapping(mapping, lineOffset, columnOffset); | ||
if (!this.sources[mapping.source]) { | ||
this.sources[mapping.source] = consumer.sourceContentFor( | ||
mapping.source, | ||
true | ||
); | ||
} | ||
}); | ||
|
||
if (consumer.destroy) { | ||
// Only needs to happen in source-map 0.7 | ||
consumer.destroy(); | ||
} | ||
} else { | ||
if (!map.eachMapping) { | ||
map = new SourceMap(map.mappings, map.sources); | ||
} | ||
|
||
if (lineOffset === 0 && columnOffset === 0) { | ||
this.mappings = this.mappings.concat(map.mappings); | ||
} else { | ||
map.eachMapping(mapping => { | ||
this.addMapping(mapping, lineOffset, columnOffset); | ||
}); | ||
} | ||
|
||
Object.keys(map.sources).forEach(sourceName => { | ||
if (!this.sources[sourceName]) { | ||
this.sources[sourceName] = map.sources[sourceName]; | ||
} | ||
}); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
addMapping(mapping, lineOffset = 0, columnOffset = 0) { | ||
mapping.generated = { | ||
line: mapping.generated.line + lineOffset, | ||
column: mapping.generated.column + columnOffset | ||
}; | ||
|
||
this.mappings.push(mapping); | ||
} | ||
|
||
addConsumerMapping(mapping, lineOffset = 0, columnOffset = 0) { | ||
if ( | ||
!mapping.source || | ||
!mapping.originalLine || | ||
(!mapping.originalColumn && mapping.originalColumn !== 0) | ||
) { | ||
return; | ||
} | ||
|
||
this.mappings.push({ | ||
source: mapping.source, | ||
original: { | ||
line: mapping.originalLine, | ||
column: mapping.originalColumn | ||
}, | ||
generated: { | ||
line: mapping.generatedLine + lineOffset, | ||
column: mapping.generatedColumn + columnOffset | ||
}, | ||
name: mapping.name | ||
}); | ||
} | ||
|
||
eachMapping(callback) { | ||
this.mappings.forEach(callback); | ||
} | ||
|
||
generateEmptyMap(sourceName, sourceContent) { | ||
this.sources[sourceName] = sourceContent; | ||
|
||
this.lineCount = lineCounter(sourceContent); | ||
for (let line = 1; line < this.lineCount + 1; line++) { | ||
this.addMapping({ | ||
source: sourceName, | ||
original: { | ||
line: line, | ||
column: 0 | ||
}, | ||
generated: { | ||
line: line, | ||
column: 0 | ||
} | ||
}); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
async extendSourceMap(original, extension) { | ||
if (!(extension instanceof SourceMap)) { | ||
throw new Error( | ||
'[SOURCEMAP] Type of extension should be a SourceMap instance!' | ||
); | ||
} | ||
|
||
original = await this.getConsumer(original); | ||
extension.eachMapping(mapping => { | ||
let originalMapping = original.originalPositionFor({ | ||
line: mapping.original.line, | ||
column: mapping.original.column | ||
}); | ||
|
||
if (!originalMapping.line) { | ||
return false; | ||
} | ||
|
||
this.addMapping({ | ||
source: originalMapping.source, | ||
name: originalMapping.name, | ||
original: { | ||
line: originalMapping.line, | ||
column: originalMapping.column | ||
}, | ||
generated: { | ||
line: mapping.generated.line, | ||
column: mapping.generated.column | ||
} | ||
}); | ||
|
||
if (!this.sources[originalMapping.source]) { | ||
this.sources[originalMapping.source] = original.sourceContentFor( | ||
originalMapping.source, | ||
true | ||
); | ||
} | ||
}); | ||
|
||
if (original.destroy) { | ||
// Only needs to happen in source-map 0.7 | ||
original.destroy(); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
offset(lineOffset = 0, columnOffset = 0) { | ||
this.mappings.map(mapping => { | ||
mapping.generated.line = mapping.generated.line + lineOffset; | ||
mapping.generated.column = mapping.generated.column + columnOffset; | ||
return mapping; | ||
}); | ||
|
||
if (this.lineCount != null) { | ||
this.lineCount += lineOffset; | ||
} | ||
} | ||
|
||
stringify(file) { | ||
let generator = new SourceMapGenerator({ | ||
file: file | ||
}); | ||
|
||
this.eachMapping(mapping => generator.addMapping(mapping)); | ||
Object.keys(this.sources).forEach(sourceName => | ||
generator.setSourceContent(sourceName, this.sources[sourceName]) | ||
); | ||
|
||
return generator.toString(); | ||
} | ||
} | ||
|
||
module.exports = SourceMap; |
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
Oops, something went wrong.