finalize preprocess

pull/8569/head
Simon Holthausen 3 years ago
parent 7cb3c1b641
commit 7c85039471

@ -1,4 +1,5 @@
import { decode as decode_mappings } from '@jridgewell/sourcemap-codec'; import { decode as decode_mappings } from '@jridgewell/sourcemap-codec';
/** /**
* 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
@ -67,7 +68,7 @@ function decoded_sourcemap_from_generator(generator) {
} }
/** /**
* @param {Processed} processed * @param {import('./public.js').Processed} processed
*/ */
export function decode_map(processed) { export function decode_map(processed) {
let decoded_map = typeof processed.map === 'string' ? JSON.parse(processed.map) : processed.map; let decoded_map = typeof processed.map === 'string' ? JSON.parse(processed.map) : processed.map;

@ -1,5 +1,6 @@
import { DecodedSourceMap } from '@ampproject/remapping'; import { DecodedSourceMap } from '@ampproject/remapping';
import { Location } from 'locate-character'; import { Location } from 'locate-character';
import { MappedCode } from '../utils/mapped_code.js';
export interface Source { export interface Source {
source: string; source: string;
@ -13,3 +14,9 @@ export interface SourceUpdate {
map?: DecodedSourceMap; map?: DecodedSourceMap;
dependencies?: string[]; dependencies?: string[];
} }
export interface Replacement {
offset: number;
length: number;
replacement: MappedCode;
}

@ -1,10 +1,10 @@
import { MappedCode } from '../utils/mapped_code'; import { MappedCode } from '../utils/mapped_code.js';
/** /**
* @param {string} code_slice * @param {string} code_slice
* @param {number} offset * @param {number} offset
* @param {Source} * @param {import('./private.js').Source} opts
* @returns {Source} * @returns {import('./private.js').Source}
*/ */
export function slice_source(code_slice, offset, { file_basename, filename, get_location }) { export function slice_source(code_slice, offset, { file_basename, filename, get_location }) {
return { return {
@ -22,7 +22,7 @@ export function slice_source(code_slice, offset, { file_basename, filename, get_
*/ */
function calculate_replacements(re, get_replacement, source) { function calculate_replacements(re, get_replacement, source) {
/** /**
* @type {Array<Promise<Replacement>>} * @type {Array<Promise<import('./private.js').Replacement>>}
*/ */
const replacements = []; const replacements = [];
source.replace(re, (...match) => { source.replace(re, (...match) => {
@ -39,8 +39,8 @@ function calculate_replacements(re, get_replacement, source) {
} }
/** /**
* @param {Replacement[]} replacements * @param {import('./private.js').Replacement[]} replacements
* @param {Source} source * @param {import('./private.js').Source} source
* @returns {MappedCode} * @returns {MappedCode}
*/ */
function perform_replacements(replacements, source) { function perform_replacements(replacements, source) {
@ -62,16 +62,10 @@ function perform_replacements(replacements, source) {
/** /**
* @param {RegExp} regex * @param {RegExp} regex
* @param {(...match: any[]) => Promise<MappedCode>} get_replacement * @param {(...match: any[]) => Promise<MappedCode>} get_replacement
* @param {Source} location * @param {import('./private.js').Source} location
* @returns {Promise<MappedCode>} * @returns {Promise<MappedCode>}
*/ */
export async function replace_in_code(regex, get_replacement, location) { export async function replace_in_code(regex, get_replacement, location) {
const replacements = await calculate_replacements(regex, get_replacement, location.source); const replacements = await calculate_replacements(regex, get_replacement, location.source);
return perform_replacements(replacements, location); return perform_replacements(replacements, location);
} }
/** @typedef {Object} Replacement
* @property {number} offset
* @property {number} length
* @property {MappedCode} replacement
*/

Loading…
Cancel
Save