-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JS Source Maps support #506
Merged
devongovett
merged 76 commits into
parcel-bundler:master
from
DeMoorJasper:feature/source-maps
Jan 22, 2018
Merged
Changes from 72 commits
Commits
Show all changes
76 commits
Select commit
Hold shift + click to select a range
fedbd52
Merge pull request #1 from parcel-bundler/master
3f31fc5
Merge branch 'master' of github.com:DeMoorJasper/parcel
DeMoorJasper b09d15f
Merge pull request #4 from parcel-bundler/master
80b3431
Merge branch 'master' of github.com:DeMoorJasper/parcel
DeMoorJasper 392bbc9
run prettier
DeMoorJasper e616cc2
Merge pull request #7 from parcel-bundler/master
1d90425
stuff
DeMoorJasper 51bfe22
Merge pull request #9 from parcel-bundler/master
2d393d2
Merge pull request #12 from parcel-bundler/master
85c7587
Merge branch 'master' of github.com:DeMoorJasper/parcel
DeMoorJasper 3c3cb77
Merge pull request #14 from parcel-bundler/master
9e7e46b
Merge branch 'master' of github.com:DeMoorJasper/parcel
DeMoorJasper 44f7a89
initial sourcemap
DeMoorJasper f24bdd7
improve sourcemap constructor
DeMoorJasper a5b5ce2
also support SourceMapConsumer as addMap input
DeMoorJasper 243d543
some cleanup and fixes
DeMoorJasper a2056df
Merge pull request #16 from parcel-bundler/master
c3f40bd
Merge branch 'feature/source-maps' of github.com:DeMoorJasper/parcel …
DeMoorJasper f6cadb5
add lineOffset and 1:1 sourcemaps
DeMoorJasper a6a0d9a
add lineOffset and 1:1 sourcemaps
DeMoorJasper cef8a8b
fix conflicts
DeMoorJasper 5157b02
slight sourcemaputil improvement
DeMoorJasper ca829e5
use babel-generator instead of double generating
DeMoorJasper 6d8f4da
add 1:1 sourcemap for untranspiled assets
DeMoorJasper d101c48
update offset
DeMoorJasper 7d9532b
fix source paths
DeMoorJasper 35a1b6f
fix conflicts
DeMoorJasper 156736f
Merge branch 'parcel-bundler-master' into feature/source-maps
DeMoorJasper 40fe5bc
Better offset solution
DeMoorJasper 4840a59
fix sourceContent not being set by babel-generator
DeMoorJasper 42c52b6
fix small offset bug
DeMoorJasper 607d5a0
add sourcemap option and offset when globals are added
DeMoorJasper 6e89578
add cli option to disable sourcemaps
DeMoorJasper bb74a14
ts & coffeescript support
DeMoorJasper 897b494
fix most tests
DeMoorJasper 401b12c
All tests (except hmr) fixed + bugfix for ts
DeMoorJasper b28c167
fix hmr tests
DeMoorJasper ec0f552
comment out error throwing in tests
DeMoorJasper 220bdf8
fix windows tests
DeMoorJasper d0498f9
add sourcemap tests
DeMoorJasper 603a12d
Merge branch 'feature/source-maps' of github.com:DeMoorJasper/parcel …
DeMoorJasper 0c97ebf
add comment to why throwing errors is commented out in tests
DeMoorJasper 7e29f10
Merge pull request #18 from parcel-bundler/master
be46eee
Merge branch 'feature/source-maps' of github.com:DeMoorJasper/parcel …
DeMoorJasper e09aaa2
update with circular fix, now throws on test failures
DeMoorJasper 032d475
fix generator for invalid maps
DeMoorJasper b7f6500
Merge branch 'master' into feature/source-maps
05741c0
Merge branch 'master' into feature/source-maps
126f522
Merge branch 'feature/source-maps' of github.com:DeMoorJasper/parcel …
DeMoorJasper 2bc0f2b
fix tests
DeMoorJasper dd5a018
Merge branch 'master' into feature/source-maps
46f2054
Merge branch 'feature/source-maps' of github.com:DeMoorJasper/parcel …
DeMoorJasper f942bfe
Merge branch 'master' into feature/source-maps
42598ff
Merge branch 'feature/source-maps' of github.com:DeMoorJasper/parcel …
DeMoorJasper 8b9ad40
fix tiny issues
DeMoorJasper cf4f426
small performance improvement
DeMoorJasper 11a5010
rewrite sourcemap handling, less generator constructing overhead and …
DeMoorJasper d3c86bd
extendSourceMap bugfix
DeMoorJasper 9568714
Use babel rawMappings to remove encoding step
DeMoorJasper 1222fb9
small performance fixes
DeMoorJasper c013456
improve linecounter
DeMoorJasper 06a48b9
Merge pull request #21 from parcel-bundler/master
56e6ea6
Merge branch 'feature/source-maps' of github.com:DeMoorJasper/parcel …
DeMoorJasper 5567ee5
small performance improvement and bugfix
DeMoorJasper c4eb40f
small improvement
DeMoorJasper 310d22f
change for source-map 0.7 compatibility, improves performance a lil
DeMoorJasper be42812
switch to official npm release
DeMoorJasper 0e57597
Merge pull request #23 from DeMoorJasper/source-map-0.7
2abb918
Merge branch 'feature/source-maps' of github.com:DeMoorJasper/parcel …
DeMoorJasper f39c21e
tiny improvement
DeMoorJasper 90b85d1
only install source-map 0.7 if possible
DeMoorJasper 8c81116
remove optional 0.7 dep
DeMoorJasper 0b95aae
Minor refactorings
devongovett 9908e9e
Store precomputed line count as part of source map
devongovett 01db40c
Clean up
devongovett 11b89c6
Last cleanup
devongovett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,192 @@ | ||
const sourceMap = require('source-map'); | ||
const textUtils = require('./utils/textUtils'); | ||
|
||
class SourceMap { | ||
constructor(file) { | ||
this.sources = {}; | ||
this.mappings = []; | ||
this.file = file; | ||
} | ||
|
||
copyConstructor(map) { | ||
let sourcemap = new SourceMap(); | ||
sourcemap.mappings = map.mappings; | ||
sourcemap.sources = map.sources; | ||
sourcemap.file = map.file; | ||
return sourcemap; | ||
} | ||
|
||
isConsumer(map) { | ||
return map && map.computeColumnSpans; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just do |
||
} | ||
|
||
async getConsumer(map) { | ||
if (this.isConsumer(map)) { | ||
return map; | ||
} | ||
return await new sourceMap.SourceMapConsumer(map); | ||
} | ||
|
||
async addMap(map, lineOffset = 0, columnOffset = 0) { | ||
if (!isSourceMapInstance(map) && 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 = this.copyConstructor(map); | ||
} | ||
if (lineOffset === 0 && columnOffset === 0) { | ||
this.concatMappings(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]; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
concatMappings(mappings) { | ||
this.mappings = this.mappings.concat(mappings); | ||
} | ||
|
||
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; | ||
let lines = textUtils.lineCounter(sourceContent); | ||
for (let line = 1; line < lines + 1; line++) { | ||
this.addMapping({ | ||
source: sourceName, | ||
original: { | ||
line: line, | ||
column: 0 | ||
}, | ||
generated: { | ||
line: line, | ||
column: 0 | ||
} | ||
}); | ||
} | ||
return this; | ||
} | ||
|
||
async extendSourceMap(original, extension) { | ||
if (!isSourceMapInstance(extension)) { | ||
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(); | ||
} | ||
} | ||
|
||
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; | ||
}); | ||
} | ||
|
||
stringify() { | ||
let generator = new sourceMap.SourceMapGenerator({ | ||
file: this.file | ||
}); | ||
this.eachMapping(mapping => generator.addMapping(mapping)); | ||
Object.keys(this.sources).forEach(sourceName => | ||
generator.setSourceContent(sourceName, this.sources[sourceName]) | ||
); | ||
return generator.toString(); | ||
} | ||
} | ||
|
||
function isSourceMapInstance(map) { | ||
return !map.sources.length; | ||
} | ||
|
||
module.exports = SourceMap; | ||
module.exports.isSourceMapInstance = isSourceMapInstance; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should we disable sourcemaps in production? Isn't that where they are most useful since code is minified? Let's get some feedback on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think there are usecases for disabling sourcemaps in production. how about enabling it by default and adding an option
--disable-sourcemaps
- so we can choose at build-time?