@@ -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}
@@ -112,19 +115,64 @@ export class SourceProcessor {
112115 return { debugId, source : newSource } ;
113116 }
114117
118+ /**
119+ * Adds required snippets and comments to source
120+ * @param source Source content.
121+ * @param debugId Debug ID. If not provided, one will be generated from `source`.
122+ * @param force Force adding changes.
123+ * @returns Used debug ID, new source and new sourcemap.
124+ */
125+ public async processSource (
126+ source : string ,
127+ debugId ?: string ,
128+ force ?: boolean ,
129+ ) : Promise < ProcessResultWithoutSourceMap > {
130+ return await this . processSourceAndAvailableSourceMap ( source , undefined , debugId , force ) ;
131+ }
132+
115133 /**
116134 * Adds required snippets and comments to source, and modifies sourcemap to include debug ID.
117135 * @param source Source content.
118136 * @param sourceMap Sourcemap object or JSON.
119137 * @param debugId Debug ID. If not provided, one will be generated from `source`.
138+ * @param force Force adding changes.
120139 * @returns Used debug ID, new source and new sourcemap.
121140 */
122141 public async processSourceAndSourceMap (
123142 source : string ,
124143 sourceMap : RawSourceMap ,
125144 debugId ?: string ,
126145 force ?: boolean ,
127- ) : Promise < ProcessResult > {
146+ ) : Promise < ProcessResultWithSourceMaps > {
147+ return await this . processSourceAndAvailableSourceMap ( source , sourceMap , debugId , force ) ;
148+ }
149+
150+ /**
151+ * Adds required snippets and comments to source, and modifies sourcemap to include debug ID if available.
152+ * @param source Source content.
153+ * @param sourceMap Sourcemap object or JSON.
154+ * @param debugId Debug ID. If not provided, one will be generated from `source`.
155+ * @param force Force adding changes.
156+ * @returns Used debug ID, new source and new sourcemap.
157+ */
158+ private async processSourceAndAvailableSourceMap (
159+ source : string ,
160+ sourceMap : RawSourceMap ,
161+ debugId ?: string ,
162+ force ?: boolean ,
163+ ) : Promise < ProcessResultWithSourceMaps > ;
164+ private async processSourceAndAvailableSourceMap (
165+ source : string ,
166+ sourceMap ?: undefined ,
167+ debugId ?: string ,
168+ force ?: boolean ,
169+ ) : Promise < ProcessResultWithoutSourceMap > ;
170+ private async processSourceAndAvailableSourceMap (
171+ source : string ,
172+ sourceMap ?: RawSourceMap ,
173+ debugId ?: string ,
174+ force ?: boolean ,
175+ ) : Promise < ProcessResultWithSourceMaps | ProcessResultWithoutSourceMap > {
128176 const sourceDebugId = this . getSourceDebugId ( source ) ;
129177 if ( ! debugId ) {
130178 debugId = sourceDebugId ?? stringToUuid ( source ) ;
@@ -146,21 +194,26 @@ export class SourceProcessor {
146194 ? shebang + sourceSnippet + '\n' + source . substring ( shebang . length )
147195 : sourceSnippet + '\n' + source ;
148196
149- // We need to offset the source map by amount of lines that we're inserting to the source code
150- // Sourcemaps map code like this:
151- // original code X:Y => generated code A:B
152- // So if we add any code to generated code, mappings after that code will become invalid
153- // We need to offset the mapping lines by sourceSnippetNewlineCount:
154- // original code X:Y => generated code (A + sourceSnippetNewlineCount):B
155- const sourceSnippetNewlineCount = sourceSnippet . match ( / \n / g) ?. length ?? 0 ;
156- offsetSourceMap = await this . offsetSourceMap ( sourceMap , sourceSnippetNewlineCount + 1 ) ;
197+ if ( sourceMap ) {
198+ // We need to offset the source map by amount of lines that we're inserting to the source code
199+ // Sourcemaps map code like this:
200+ // original code X:Y => generated code A:B
201+ // So if we add any code to generated code, mappings after that code will become invalid
202+ // We need to offset the mapping lines by sourceSnippetNewlineCount:
203+ // original code X:Y => generated code (A + sourceSnippetNewlineCount):B
204+ const sourceSnippetNewlineCount = sourceSnippet . match ( / \n / g) ?. length ?? 0 ;
205+ offsetSourceMap = await this . offsetSourceMap ( sourceMap , sourceSnippetNewlineCount + 1 ) ;
206+ }
157207 }
158208
159209 if ( force || ! sourceDebugId || ! this . _debugIdGenerator . hasCommentSnippet ( source , debugId ) ) {
160210 const sourceComment = this . _debugIdGenerator . generateSourceComment ( debugId ) ;
161211 newSource = appendBeforeWhitespaces ( newSource , '\n' + sourceComment ) ;
162212 }
163213
214+ if ( ! sourceMap ) {
215+ return { debugId, source : newSource } as ProcessResultWithoutSourceMap ;
216+ }
164217 const newSourceMap = this . _debugIdGenerator . addSourceMapDebugId ( offsetSourceMap ?? sourceMap , debugId ) ;
165218 return { debugId, source : newSource , sourceMap : newSourceMap } ;
166219 }
0 commit comments