pull/5732/head
Conduitry 5 years ago
parent d8c553f51f
commit 484a04bfd9

@ -83,78 +83,71 @@ async function replace_async(
return out.concat(final_content); return out.concat(final_content);
} }
/** /**
* import decoded sourcemap from mozilla/source-map/SourceMapGenerator * Import decoded sourcemap from mozilla/source-map/SourceMapGenerator
* forked from source-map/lib/source-map-generator.js * Forked from source-map/lib/source-map-generator.js
* from methods _serializeMappings and toJSON * from methods _serializeMappings and toJSON.
* we cannot use source-map.d.ts types, cos we access hidden properties * We cannot use source-map.d.ts types, because we access hidden properties.
*/ */
function decoded_sourcemap_from_generator(generator: any) { function decoded_sourcemap_from_generator(generator: any) {
function are_equal_mappings(a, b) { let previous_generated_line = 1;
return ( const converted_mappings = [[]];
// sorted by selectivity let result_line;
a.generatedColumn == b.generatedColumn && let result_segment;
a.originalColumn == b.originalColumn && let mapping;
a.name == b.name &&
a.generatedLine == b.generatedLine && const source_idx = generator._sources.toArray()
a.originalLine == b.originalLine && .reduce((acc, val, idx) => (acc[val] = idx, acc), {});
a.source == b.source
); const name_idx = generator._names.toArray()
} .reduce((acc, val, idx) => (acc[val] = idx, acc), {});
function convert_mappings() {
// use global variable: generator const mappings = generator._mappings.toArray();
let previousGeneratedLine = 1; result_line = converted_mappings[0];
const result = [[]];
let resultLine; for (let i = 0, len = mappings.length; i < len; i++) {
let resultSegment; mapping = mappings[i];
let mapping;
if (mapping.generatedLine > previous_generated_line) {
const sourceIdx = generator._sources.toArray() while (mapping.generatedLine > previous_generated_line) {
.reduce((acc, val, idx) => (acc[val] = idx, acc), {}); converted_mappings.push([]);
previous_generated_line++;
const nameIdx = generator._names.toArray()
.reduce((acc, val, idx) => (acc[val] = idx, acc), {});
const mappings = generator._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 (are_equal_mappings(mapping, mappings[i - 1])) {
continue;
}
} }
resultLine.push([mapping.generatedColumn]); result_line = converted_mappings[mapping.generatedLine - 1]; // line is one-based
resultSegment = resultLine[resultLine.length - 1]; } else if (i > 0) {
const previous_mapping = mappings[i - 1];
if (mapping.source != null) { if (
resultSegment.push(...[ // sorted by selectivity
sourceIdx[mapping.source], mapping.generatedColumn === previous_mapping.generatedColumn &&
mapping.originalLine - 1, // line is one-based mapping.originalColumn === previous_mapping.originalColumn &&
mapping.originalColumn mapping.name === previous_mapping.name &&
]); mapping.generatedLine === previous_mapping.generatedLine &&
if (mapping.name != null) { mapping.originalLine === previous_mapping.originalLine &&
resultSegment.push(nameIdx[mapping.name]); mapping.source === previous_mapping.source
} ) {
continue;
}
}
result_line.push([mapping.generatedColumn]);
result_segment = result_line[result_line.length - 1];
if (mapping.source != null) {
result_segment.push(...[
source_idx[mapping.source],
mapping.originalLine - 1, // line is one-based
mapping.originalColumn
]);
if (mapping.name != null) {
result_segment.push(name_idx[mapping.name]);
} }
} }
return result;
} }
const map = { const map = {
version: generator._version, version: generator._version,
sources: generator._sources.toArray(), sources: generator._sources.toArray(),
names: generator._names.toArray(), names: generator._names.toArray(),
mappings: convert_mappings() mappings: converted_mappings
}; };
if (generator._file != null) { if (generator._file != null) {
(map as any).file = generator._file; (map as any).file = generator._file;
@ -163,10 +156,8 @@ function decoded_sourcemap_from_generator(generator: any) {
return map; return map;
} }
/**
* Convert a preprocessor output and its leading prefix and trailing suffix into StringWithSourceMap
/**
* Convert a preprocessor output and its leading prefix and trailing suffix into StringWithSourceMap
*/ */
function get_replacement( function get_replacement(
filename: string, filename: string,
@ -191,7 +182,7 @@ function get_replacement(
if (typeof(decoded_map.mappings) === 'string') { if (typeof(decoded_map.mappings) === 'string') {
decoded_map.mappings = decode_mappings(decoded_map.mappings); decoded_map.mappings = decode_mappings(decoded_map.mappings);
} }
if ((decoded_map as any)._mappings && decoded_map.constructor.name == 'SourceMapGenerator') { if ((decoded_map as any)._mappings && decoded_map.constructor.name === 'SourceMapGenerator') {
// import decoded sourcemap from mozilla/source-map/SourceMapGenerator // import decoded sourcemap from mozilla/source-map/SourceMapGenerator
decoded_map = decoded_sourcemap_from_generator(decoded_map); decoded_map = decoded_sourcemap_from_generator(decoded_map);
} }
@ -250,7 +241,7 @@ export default async function preprocess(
async function preprocess_tag_content(tag_name: 'style' | 'script', preprocessor: Preprocessor) { async function preprocess_tag_content(tag_name: 'style' | 'script', preprocessor: Preprocessor) {
const get_location = getLocator(source); const get_location = getLocator(source);
const tag_regex = tag_name == 'style' const tag_regex = tag_name === 'style'
? /<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi ? /<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi
: /<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi; : /<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi;

Loading…
Cancel
Save