source: frontend/node_modules/workbox-build/src/lib/replace-and-update-source-map.ts

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 Copyright 2019 Google LLC
3
4 Use of this source code is governed by an MIT-style
5 license that can be found in the LICENSE file or at
6 https://opensource.org/licenses/MIT.
7*/
8
9import {RawSourceMap, SourceMapConsumer, SourceMapGenerator} from 'source-map';
10
11/**
12 * Adapted from https://github.com/nsams/sourcemap-aware-replace, with modern
13 * JavaScript updates, along with additional properties copied from originalMap.
14 *
15 * @param {Object} options
16 * @param {string} options.jsFilename The name for the file whose contents
17 * correspond to originalSource.
18 * @param {Object} options.originalMap The sourcemap for originalSource,
19 * prior to any replacements.
20 * @param {string} options.originalSource The source code, prior to any
21 * replacements.
22 * @param {string} options.replaceString A string to swap in for searchString.
23 * @param {string} options.searchString A string in originalSource to replace.
24 * Only the first occurrence will be replaced.
25 * @return {{source: string, map: string}} An object containing both
26 * originalSource with the replacement applied, and the modified originalMap.
27 *
28 * @private
29 */
30export async function replaceAndUpdateSourceMap({
31 jsFilename,
32 originalMap,
33 originalSource,
34 replaceString,
35 searchString,
36}: {
37 jsFilename: string;
38 originalMap: RawSourceMap;
39 originalSource: string;
40 replaceString: string;
41 searchString: string;
42}): Promise<{map: string; source: string}> {
43 const generator = new SourceMapGenerator({
44 file: jsFilename,
45 });
46
47 const consumer = await new SourceMapConsumer(originalMap);
48
49 let pos: number;
50 let src = originalSource;
51 const replacements: Array<{line: number; column: number}> = [];
52 let lineNum = 0;
53 let filePos = 0;
54
55 const lines = src.split('\n');
56 for (let line of lines) {
57 lineNum++;
58 let searchPos = 0;
59 while ((pos = line.indexOf(searchString, searchPos)) !== -1) {
60 src =
61 src.substring(0, filePos + pos) +
62 replaceString +
63 src.substring(filePos + pos + searchString.length);
64 line =
65 line.substring(0, pos) +
66 replaceString +
67 line.substring(pos + searchString.length);
68 replacements.push({line: lineNum, column: pos});
69 searchPos = pos + replaceString.length;
70 }
71 filePos += line.length + 1;
72 }
73
74 replacements.reverse();
75
76 consumer.eachMapping((mapping) => {
77 for (const replacement of replacements) {
78 if (
79 replacement.line === mapping.generatedLine &&
80 mapping.generatedColumn > replacement.column
81 ) {
82 const offset = searchString.length - replaceString.length;
83 mapping.generatedColumn -= offset;
84 }
85 }
86
87 if (mapping.source) {
88 const newMapping = {
89 generated: {
90 line: mapping.generatedLine,
91 column: mapping.generatedColumn,
92 },
93 original: {
94 line: mapping.originalLine,
95 column: mapping.originalColumn,
96 },
97 source: mapping.source,
98 };
99 return generator.addMapping(newMapping);
100 }
101
102 return mapping;
103 });
104
105 consumer.destroy();
106 // JSON.parse returns any.
107 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
108 const updatedSourceMap: RawSourceMap = Object.assign(
109 JSON.parse(generator.toString()),
110 {
111 names: originalMap.names,
112 sourceRoot: originalMap.sourceRoot,
113 sources: originalMap.sources,
114 sourcesContent: originalMap.sourcesContent,
115 },
116 );
117
118 return {
119 map: JSON.stringify(updatedSourceMap),
120 source: src,
121 };
122}
Note: See TracBrowser for help on using the repository browser.