@@ -7,13 +7,16 @@ import { stringToUuid } from './helpers/stringToUuid';
7
7
import { RawSourceMap , RawSourceMapWithDebugId } from './models/RawSourceMap' ;
8
8
import { Err , Ok , R , ResultPromise } from './models/Result' ;
9
9
10
- export interface ProcessResult {
10
+ export interface ProcessResultWithoutSourceMap {
11
11
readonly debugId : string ;
12
12
readonly source : string ;
13
+ }
14
+
15
+ export interface ProcessResultWithSourceMaps extends ProcessResultWithoutSourceMap {
13
16
readonly sourceMap : RawSourceMapWithDebugId ;
14
17
}
15
18
16
- export interface ProcessResultWithPaths extends ProcessResult {
19
+ export interface ProcessResultWithPaths extends ProcessResultWithSourceMaps {
17
20
readonly sourcePath : string ;
18
21
readonly sourceMapPath : string ;
19
22
}
@@ -82,19 +85,64 @@ export class SourceProcessor {
82
85
) ;
83
86
}
84
87
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
+
85
103
/**
86
104
* Adds required snippets and comments to source, and modifies sourcemap to include debug ID.
87
105
* @param source Source content.
88
106
* @param sourceMap Sourcemap object or JSON.
89
107
* @param debugId Debug ID. If not provided, one will be generated from `source`.
108
+ * @param force Force adding changes.
90
109
* @returns Used debug ID, new source and new sourcemap.
91
110
*/
92
111
public async processSourceAndSourceMap (
93
112
source : string ,
94
113
sourceMap : RawSourceMap ,
95
114
debugId ?: string ,
96
115
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 > {
98
146
const sourceDebugId = this . getSourceDebugId ( source ) ;
99
147
if ( ! debugId ) {
100
148
debugId = sourceDebugId ?? stringToUuid ( source ) ;
@@ -116,21 +164,26 @@ export class SourceProcessor {
116
164
? shebang + sourceSnippet + '\n' + source . substring ( shebang . length )
117
165
: sourceSnippet + '\n' + source ;
118
166
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
+ }
127
177
}
128
178
129
179
if ( force || ! sourceDebugId || ! this . _debugIdGenerator . hasCommentSnippet ( source , debugId ) ) {
130
180
const sourceComment = this . _debugIdGenerator . generateSourceComment ( debugId ) ;
131
181
newSource = appendBeforeWhitespaces ( newSource , '\n' + sourceComment ) ;
132
182
}
133
183
184
+ if ( ! sourceMap ) {
185
+ return { debugId, source : newSource } as ProcessResultWithoutSourceMap ;
186
+ }
134
187
const newSourceMap = this . _debugIdGenerator . addSourceMapDebugId ( offsetSourceMap ?? sourceMap , debugId ) ;
135
188
return { debugId, source : newSource , sourceMap : newSourceMap } ;
136
189
}
0 commit comments