-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
add: import decoded sourcemaps from SourceMapGenerator #5732
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
Changes from 3 commits
9788885
38a3eba
19e42f7
6311a85
d8c553f
484a04b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,82 @@ async function replace_async( | |
return out.concat(final_content); | ||
} | ||
|
||
|
||
|
||
/** | ||
* import decoded sourcemap from mozilla/source-map/SourceMapGenerator | ||
* forked from source-map/lib/source-map-generator.js | ||
* from methods _serializeMappings and toJSON | ||
*/ | ||
function decodedSourcemapFromSourceMapGenerator(this: any) { | ||
Conduitry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
function areEqualMappings(a, b) { | ||
return ( | ||
a.generatedLine == b.generatedLine && | ||
a.generatedColumn == b.generatedColumn && | ||
a.source == b.source && | ||
a.originalLine == b.originalLine && | ||
a.originalColumn == b.originalColumn && | ||
a.name == b.name | ||
); | ||
} | ||
function convertMappings(this: any) { | ||
let previousGeneratedLine = 1; | ||
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. I think Conduitry was also asking if we could rename all the other variables and functions to use snake_case rather than camelCase 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. good fight, good night 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. I'm not quite sure how to interpret. I guess you are going to bed. Will you rename the variables later? 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. I've pushed a commit to rename these and to also do some other tidying up and inlining of functions that are just called once. I'm aiming to merge this today and take a look for anything else screaming to be merged and then cut a release. Although this is technically a new feature, I'm thinking of still releasing this as a patch release. |
||
const result = [[]]; | ||
let resultLine; | ||
let resultSegment; | ||
let mapping; | ||
|
||
const sourceIdx = this._sources.toArray().reduce((acc, val, idx) => (acc[val] = idx, acc), {}); | ||
const nameIdx = this._names.toArray().reduce((acc, val, idx) => (acc[val] = idx, acc), {}); | ||
|
||
const mappings = this._mappings.toArray(); | ||
resultLine = result[0]; | ||
|
||
for (let i = 0, len = mappings.length; i < len; i++) { | ||
mapping = mappings[i]; | ||
|
||
if (mapping.generatedLine > previousGeneratedLine) { | ||
while (mapping.generatedLine > previousGeneratedLine) { | ||
result.push([]); | ||
previousGeneratedLine++; | ||
} | ||
resultLine = result[mapping.generatedLine - 1]; // line is one-based | ||
} else if (i > 0) { | ||
if (areEqualMappings(mapping, mappings[i - 1])) { | ||
continue; | ||
} | ||
} | ||
resultLine.push([mapping.generatedColumn]); | ||
resultSegment = resultLine[resultLine.length - 1]; | ||
|
||
if (mapping.source != null) { | ||
resultSegment.push(...[ | ||
sourceIdx[mapping.source], | ||
mapping.originalLine - 1, // line is one-based | ||
mapping.originalColumn | ||
]); | ||
if (mapping.name != null) { | ||
resultSegment.push(nameIdx[mapping.name]); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
const map = { | ||
version: this._version, | ||
sources: this._sources.toArray(), | ||
names: this._names.toArray(), | ||
mappings: convertMappings.apply(this) | ||
}; | ||
if (this._file != null) { | ||
(map as any).file = this._file; | ||
} | ||
// not needed: map.sourcesContent and map.sourceRoot | ||
return map; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Convert a preprocessor output and its leading prefix and trailing suffix into StringWithSourceMap | ||
*/ | ||
|
@@ -109,6 +185,13 @@ function get_replacement( | |
if (typeof(decoded_map.mappings) === 'string') { | ||
decoded_map.mappings = decode_mappings(decoded_map.mappings); | ||
} | ||
if ( | ||
benmccann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(decoded_map as any)._mappings && | ||
decoded_map.constructor.name == 'SourceMapGenerator' | ||
) { | ||
// import decoded sourcemap from mozilla/source-map/SourceMapGenerator | ||
decoded_map = decodedSourcemapFromSourceMapGenerator.apply(decoded_map); | ||
} | ||
sourcemap_add_offset(decoded_map, get_location(offset + prefix.length)); | ||
} | ||
const processed_with_map = StringWithSourcemap.from_processed(processed.code, decoded_map); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import MagicString from 'magic-string'; | ||
import { SourceMapConsumer, SourceMapGenerator } from 'source-map'; | ||
|
||
export default { | ||
preprocess: { | ||
style: async ({ content, filename }) => { | ||
const src = new MagicString(content); | ||
const idx = content.indexOf('baritone'); | ||
src.overwrite(idx, idx+'baritone'.length, 'bar'); | ||
|
||
const map = SourceMapGenerator.fromSourceMap( | ||
await new SourceMapConsumer( | ||
// sourcemap must be encoded for SourceMapConsumer | ||
src.generateMap({ | ||
source: filename, | ||
hires: true, | ||
includeContent: false | ||
}) | ||
) | ||
); | ||
|
||
return { code: src.toString(), map }; | ||
} | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<h1>Testing Styles</h1> | ||
<h2>Testing Styles 2</h2> | ||
<script>export const b = 2;</script> | ||
<style> | ||
h1 { | ||
--baritone: red; | ||
} | ||
|
||
h2 { | ||
--baz: blue; | ||
} | ||
</style> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { test } from '../preprocessed-styles/test'; |
Uh oh!
There was an error while loading. Please reload this page.