@@ -7,13 +7,16 @@ import { stringToUuid } from './helpers/stringToUuid';
77import { RawSourceMap , RawSourceMapWithDebugId } from './models/RawSourceMap' ;
88import { Err , Ok , R , ResultPromise } from './models/Result' ;
99
10- export interface ProcessResult {
10+ export interface ProcessResultWithoutSourceMap {
1111 readonly debugId : string ;
1212 readonly source : string ;
13+ }
14+
15+ export interface ProcessResultWithSourceMaps extends ProcessResultWithoutSourceMap {
1316 readonly sourceMap : RawSourceMapWithDebugId ;
1417}
1518
16- export interface ProcessResultWithPaths extends ProcessResult {
19+ export interface ProcessResultWithPaths extends ProcessResultWithSourceMaps {
1720 readonly sourcePath : string ;
1821 readonly sourceMapPath : string ;
1922}
@@ -82,19 +85,64 @@ export class SourceProcessor {
8285 ) ;
8386 }
8487
88+ /**
89+ * Adds required snippets and comments to source
90+ * @param source Source content.
91+ * @param debugId Debug ID. If not provided, one will be generated from `source`.
92+ * @param force Force adding changes.
93+ * @returns Used debug ID, new source and new sourcemap.
94+ */
95+ public async processSource (
96+ source : string ,
97+ debugId ?: string ,
98+ force ?: boolean ,
99+ ) : Promise < ProcessResultWithoutSourceMap > {
100+ return await this . processSourceAndAvailableSourceMap ( source , undefined , debugId , force ) ;
101+ }
102+
85103 /**
86104 * Adds required snippets and comments to source, and modifies sourcemap to include debug ID.
87105 * @param source Source content.
88106 * @param sourceMap Sourcemap object or JSON.
89107 * @param debugId Debug ID. If not provided, one will be generated from `source`.
108+ * @param force Force adding changes.
90109 * @returns Used debug ID, new source and new sourcemap.
91110 */
92111 public async processSourceAndSourceMap (
93112 source : string ,
94113 sourceMap : RawSourceMap ,
95114 debugId ?: string ,
96115 force ?: boolean ,
97- ) : Promise < ProcessResult > {
116+ ) : Promise < ProcessResultWithSourceMaps > {
117+ return await this . processSourceAndAvailableSourceMap ( source , sourceMap , debugId , force ) ;
118+ }
119+
120+ /**
121+ * Adds required snippets and comments to source, and modifies sourcemap to include debug ID if available.
122+ * @param source Source content.
123+ * @param sourceMap Sourcemap object or JSON.
124+ * @param debugId Debug ID. If not provided, one will be generated from `source`.
125+ * @param force Force adding changes.
126+ * @returns Used debug ID, new source and new sourcemap.
127+ */
128+ private async processSourceAndAvailableSourceMap (
129+ source : string ,
130+ sourceMap : RawSourceMap ,
131+ debugId ?: string ,
132+ force ?: boolean ,
133+ ) : Promise < ProcessResultWithSourceMaps > ;
134+ private async processSourceAndAvailableSourceMap (
135+ source : string ,
136+ sourceMap ?: undefined ,
137+ debugId ?: string ,
138+ force ?: boolean ,
139+ ) : Promise < ProcessResultWithoutSourceMap > ;
140+ private async processSourceAndAvailableSourceMap (
141+ source : string ,
142+ sourceMap ?: RawSourceMap ,
143+ debugId ?: string ,
144+ force ?: boolean ,
145+ ) : Promise < ProcessResultWithSourceMaps | ProcessResultWithoutSourceMap > {
98146 const sourceDebugId = this . getSourceDebugId ( source ) ;
99147 if ( ! debugId ) {
100148 debugId = sourceDebugId ?? stringToUuid ( source ) ;
@@ -116,21 +164,26 @@ export class SourceProcessor {
116164 ? shebang + sourceSnippet + '\n' + source . substring ( shebang . length )
117165 : sourceSnippet + '\n' + source ;
118166
119- // We need to offset the source map by amount of lines that we're inserting to the source code
120- // Sourcemaps map code like this:
121- // original code X:Y => generated code A:B
122- // So if we add any code to generated code, mappings after that code will become invalid
123- // We need to offset the mapping lines by sourceSnippetNewlineCount:
124- // original code X:Y => generated code (A + sourceSnippetNewlineCount):B
125- const sourceSnippetNewlineCount = sourceSnippet . match ( / \n / g) ?. length ?? 0 ;
126- offsetSourceMap = await this . offsetSourceMap ( sourceMap , sourceSnippetNewlineCount + 1 ) ;
167+ if ( sourceMap ) {
168+ // We need to offset the source map by amount of lines that we're inserting to the source code
169+ // Sourcemaps map code like this:
170+ // original code X:Y => generated code A:B
171+ // So if we add any code to generated code, mappings after that code will become invalid
172+ // We need to offset the mapping lines by sourceSnippetNewlineCount:
173+ // original code X:Y => generated code (A + sourceSnippetNewlineCount):B
174+ const sourceSnippetNewlineCount = sourceSnippet . match ( / \n / g) ?. length ?? 0 ;
175+ offsetSourceMap = await this . offsetSourceMap ( sourceMap , sourceSnippetNewlineCount + 1 ) ;
176+ }
127177 }
128178
129179 if ( force || ! sourceDebugId || ! this . _debugIdGenerator . hasCommentSnippet ( source , debugId ) ) {
130180 const sourceComment = this . _debugIdGenerator . generateSourceComment ( debugId ) ;
131181 newSource = appendBeforeWhitespaces ( newSource , '\n' + sourceComment ) ;
132182 }
133183
184+ if ( ! sourceMap ) {
185+ return { debugId, source : newSource } as ProcessResultWithoutSourceMap ;
186+ }
134187 const newSourceMap = this . _debugIdGenerator . addSourceMapDebugId ( offsetSourceMap ?? sourceMap , debugId ) ;
135188 return { debugId, source : newSource , sourceMap : newSourceMap } ;
136189 }
0 commit comments