@@ -16,6 +16,7 @@ import {
1616 TagInformation ,
1717} from '../../lib/documents' ;
1818import { importSvelte } from '../../importPackage' ;
19+ import { isNotNullOrUndefined } from '../../utils' ;
1920
2021export type SvelteCompileResult = ReturnType < typeof compile > ;
2122
@@ -93,41 +94,44 @@ export class SvelteDocument {
9394}
9495
9596export class TranspiledSvelteDocument implements Pick < DocumentMapper , 'getOriginalPosition' > {
96- static async create ( document : Document , preprocessors : PreprocessorGroup = { } ) {
97- const { transpiled, processedScript, processedStyle } = await transpile (
97+ static async create (
98+ document : Document ,
99+ preprocessors : PreprocessorGroup | PreprocessorGroup [ ] = [ ] ,
100+ ) {
101+ const { transpiled, processedScripts, processedStyles } = await transpile (
98102 document ,
99103 preprocessors ,
100104 ) ;
101105 const scriptMapper = await SvelteFragmentMapper . createScript (
102106 document ,
103107 transpiled ,
104- processedScript ,
108+ processedScripts ,
105109 ) ;
106110 const styleMapper = await SvelteFragmentMapper . createStyle (
107111 document ,
108112 transpiled ,
109- processedStyle ,
113+ processedStyles ,
110114 ) ;
111115
112116 return new TranspiledSvelteDocument ( document , transpiled , scriptMapper , styleMapper ) ;
113117 }
114118
115- private fragmentInfos = [ this . scriptMapper . fragmentInfo , this . styleMapper . fragmentInfo ] . sort (
116- ( i1 , i2 ) => i1 . end - i2 . end ,
117- ) ;
119+ private fragmentInfos = [ this . scriptMapper ? .fragmentInfo , this . styleMapper ? .fragmentInfo ]
120+ . filter ( isNotNullOrUndefined )
121+ . sort ( ( i1 , i2 ) => i1 . end - i2 . end ) ;
118122
119123 private constructor (
120124 private parent : Document ,
121125 private transpiled : string ,
122- public scriptMapper : SvelteFragmentMapper ,
123- public styleMapper : SvelteFragmentMapper ,
126+ public scriptMapper : SvelteFragmentMapper | null ,
127+ public styleMapper : SvelteFragmentMapper | null ,
124128 ) { }
125129
126130 getOriginalPosition ( generatedPosition : Position ) : Position {
127- if ( this . scriptMapper . isInTranspiledFragment ( generatedPosition ) ) {
131+ if ( this . scriptMapper ? .isInTranspiledFragment ( generatedPosition ) ) {
128132 return this . scriptMapper . getOriginalPosition ( generatedPosition ) ;
129133 }
130- if ( this . styleMapper . isInTranspiledFragment ( generatedPosition ) ) {
134+ if ( this . styleMapper ? .isInTranspiledFragment ( generatedPosition ) ) {
131135 return this . styleMapper . getOriginalPosition ( generatedPosition ) ;
132136 }
133137
@@ -154,13 +158,13 @@ export class TranspiledSvelteDocument implements Pick<DocumentMapper, 'getOrigin
154158 * Needs to be called before cleanup to prevent source map memory leaks.
155159 */
156160 destroy ( ) {
157- this . scriptMapper . destroy ( ) ;
158- this . styleMapper . destroy ( ) ;
161+ this . scriptMapper ? .destroy ( ) ;
162+ this . styleMapper ? .destroy ( ) ;
159163 }
160164}
161165
162166export class SvelteFragmentMapper {
163- static async createStyle ( originalDoc : Document , transpiled : string , processed ? : Processed ) {
167+ static async createStyle ( originalDoc : Document , transpiled : string , processed : Processed [ ] ) {
164168 return SvelteFragmentMapper . create (
165169 originalDoc ,
166170 transpiled ,
@@ -170,7 +174,7 @@ export class SvelteFragmentMapper {
170174 ) ;
171175 }
172176
173- static async createScript ( originalDoc : Document , transpiled : string , processed ? : Processed ) {
177+ static async createScript ( originalDoc : Document , transpiled : string , processed : Processed [ ] ) {
174178 return SvelteFragmentMapper . create (
175179 originalDoc ,
176180 transpiled ,
@@ -185,14 +189,12 @@ export class SvelteFragmentMapper {
185189 transpiled : string ,
186190 originalTagInfo : TagInformation | null ,
187191 transpiledTagInfo : TagInformation | null ,
188- processed ? : Processed ,
192+ processed : Processed [ ] ,
189193 ) {
190- const sourceMapper = processed ?. map
191- ? new SourceMapDocumentMapper (
192- await new SourceMapConsumer ( processed . map . toString ( ) ) ,
193- originalDoc . uri ,
194- )
195- : new IdentityMapper ( originalDoc . uri ) ;
194+ const sourceMapper =
195+ processed . length > 0
196+ ? await SvelteFragmentMapper . createSourceMapper ( processed , originalDoc )
197+ : new IdentityMapper ( originalDoc . uri ) ;
196198
197199 if ( originalTagInfo && transpiledTagInfo ) {
198200 const sourceLength = originalTagInfo . container . end - originalTagInfo . container . start ;
@@ -208,11 +210,20 @@ export class SvelteFragmentMapper {
208210 ) ;
209211 }
210212
211- return new SvelteFragmentMapper (
212- { end : - 1 , diff : 0 } ,
213- new IdentityMapper ( originalDoc . uri ) ,
214- new IdentityMapper ( originalDoc . uri ) ,
215- sourceMapper ,
213+ return null ;
214+ }
215+
216+ private static async createSourceMapper ( processed : Processed [ ] , originalDoc : Document ) {
217+ return processed . reduce (
218+ async ( parent , processedSingle ) =>
219+ processedSingle ?. map
220+ ? new SourceMapDocumentMapper (
221+ await new SourceMapConsumer ( processedSingle . map . toString ( ) ) ,
222+ originalDoc . uri ,
223+ await parent ,
224+ )
225+ : new IdentityMapper ( originalDoc . uri , await parent ) ,
226+ Promise . resolve < DocumentMapper > ( < any > undefined ) ,
216227 ) ;
217228 }
218229
@@ -262,49 +273,56 @@ export class SvelteFragmentMapper {
262273 }
263274}
264275
265- async function transpile ( document : Document , preprocessors : PreprocessorGroup = { } ) {
266- const preprocessor : PreprocessorGroup = { } ;
267- let processedScript : Processed | undefined ;
268- let processedStyle : Processed | undefined ;
269-
270- preprocessor . markup = preprocessors . markup ;
271-
272- if ( preprocessors . script ) {
273- preprocessor . script = async ( args : any ) => {
274- try {
275- const res = await preprocessors . script ! ( args ) ;
276- if ( res && res . map ) {
277- processedScript = res ;
276+ async function transpile (
277+ document : Document ,
278+ preprocessors : PreprocessorGroup | PreprocessorGroup [ ] = [ ] ,
279+ ) {
280+ preprocessors = Array . isArray ( preprocessors ) ? preprocessors : [ preprocessors ] ;
281+ const processedScripts : Processed [ ] = [ ] ;
282+ const processedStyles : Processed [ ] = [ ] ;
283+
284+ const wrappedPreprocessors = preprocessors . map ( ( preprocessor ) => {
285+ const wrappedPreprocessor : PreprocessorGroup = { markup : preprocessor . markup } ;
286+
287+ if ( preprocessor . script ) {
288+ wrappedPreprocessor . script = async ( args : any ) => {
289+ try {
290+ const res = await preprocessor . script ! ( args ) ;
291+ if ( res && res . map ) {
292+ processedScripts . push ( res ) ;
293+ }
294+ return res ;
295+ } catch ( e ) {
296+ e . __source = TranspileErrorSource . Script ;
297+ throw e ;
278298 }
279- return res ;
280- } catch ( e ) {
281- e . __source = TranspileErrorSource . Script ;
282- throw e ;
283- }
284- } ;
285- }
299+ } ;
300+ }
286301
287- if ( preprocessors . style ) {
288- preprocessor . style = async ( args : any ) => {
289- try {
290- const res = await preprocessors . style ! ( args ) ;
291- if ( res && res . map ) {
292- processedStyle = res ;
302+ if ( preprocessor . style ) {
303+ wrappedPreprocessor . style = async ( args : any ) => {
304+ try {
305+ const res = await preprocessor . style ! ( args ) ;
306+ if ( res && res . map ) {
307+ processedStyles . push ( res ) ;
308+ }
309+ return res ;
310+ } catch ( e ) {
311+ e . __source = TranspileErrorSource . Style ;
312+ throw e ;
293313 }
294- return res ;
295- } catch ( e ) {
296- e . __source = TranspileErrorSource . Style ;
297- throw e ;
298- }
299- } ;
300- }
314+ } ;
315+ }
316+
317+ return wrappedPreprocessor ;
318+ } ) ;
301319
302320 const svelte = importSvelte ( document . getFilePath ( ) || '' ) ;
303321 const transpiled = (
304- await svelte . preprocess ( document . getText ( ) , preprocessor , {
322+ await svelte . preprocess ( document . getText ( ) , wrappedPreprocessors , {
305323 filename : document . getFilePath ( ) || '' ,
306324 } )
307325 ) . toString ( ) ;
308326
309- return { transpiled, processedScript , processedStyle } ;
327+ return { transpiled, processedScripts , processedStyles } ;
310328}
0 commit comments