@@ -225,6 +225,9 @@ function getMarkoFile(code, fileOpts, markoOpts) {
225
225
parseMarko ( file ) ;
226
226
227
227
if ( isSource ) {
228
+ if ( markoOpts . stripTypes ) {
229
+ stripTypes ( file ) ;
230
+ }
228
231
return file ;
229
232
}
230
233
@@ -270,66 +273,15 @@ function getMarkoFile(code, fileOpts, markoOpts) {
270
273
}
271
274
}
272
275
276
+ if ( markoOpts . stripTypes ) {
277
+ stripTypes ( file ) ;
278
+ }
279
+
273
280
if ( isMigrate ) {
274
281
return file ;
275
282
}
276
283
277
284
file . ___compileStage = "transform" ;
278
- if ( markoOpts . stripTypes ) {
279
- const importScriptlets = new Map ( ) ;
280
- for ( const path of file . path . get ( "body" ) ) {
281
- if ( path . type === "MarkoScriptlet" && path . node . static ) {
282
- for ( const stmt of path . get ( "body" ) ) {
283
- if ( stmt . isImportDeclaration ( ) ) {
284
- // Hoist import declarations from scriptlets
285
- // temporarily so that they will be processed by
286
- // babel typescript transform.
287
- const importNode = stmt . node ;
288
- importScriptlets . set ( importNode , path . node ) ;
289
- stmt . remove ( ) ;
290
- path . insertBefore ( importNode ) ;
291
- }
292
- }
293
- }
294
- }
295
-
296
- traverseAll ( file , stripTypesVisitor ) ;
297
-
298
- for ( const path of file . path . get ( "body" ) ) {
299
- if ( path . type === "ExportNamedDeclaration" ) {
300
- if ( ! ( path . node . declaration || path . node . specifiers . length ) ) {
301
- // The babel typescript plugin will add an empty export declaration
302
- // if there are no other imports/exports in the file.
303
- // This is not needed for Marko file outputs since there is always
304
- // a default export.
305
- path . remove ( ) ;
306
- }
307
- } else if ( path . isImportDeclaration ( ) ) {
308
- const importNode = path . node ;
309
- const scriptlet = importScriptlets . get ( importNode ) ;
310
- if ( scriptlet ) {
311
- let hasTypes = false ;
312
- for ( const specifier of path . get ( "specifiers" ) ) {
313
- if (
314
- specifier . node . type === "ImportSpecifier" &&
315
- specifier . node . importKind === "type"
316
- ) {
317
- hasTypes = true ;
318
- specifier . remove ( ) ;
319
- }
320
- }
321
-
322
- path . remove ( ) ;
323
-
324
- // Add back imports from scriptlets that were
325
- // hoisted for the babel typescript transform.
326
- if ( ! hasTypes || importNode . specifiers . length ) {
327
- scriptlet . body . unshift ( importNode ) ;
328
- }
329
- }
330
- }
331
- }
332
- }
333
285
334
286
const rootTransformers = [ ] ;
335
287
for ( const id in taglibLookup . taglibsById ) {
@@ -455,6 +407,62 @@ function addPlugin(meta, arr, plugin) {
455
407
}
456
408
}
457
409
410
+ function stripTypes ( file ) {
411
+ const importScriptlets = new Map ( ) ;
412
+ for ( const path of file . path . get ( "body" ) ) {
413
+ if ( path . type === "MarkoScriptlet" && path . node . static ) {
414
+ for ( const stmt of path . get ( "body" ) ) {
415
+ if ( stmt . isImportDeclaration ( ) ) {
416
+ // Hoist import declarations from scriptlets
417
+ // temporarily so that they will be processed by
418
+ // babel typescript transform.
419
+ const importNode = stmt . node ;
420
+ importScriptlets . set ( importNode , path . node ) ;
421
+ stmt . remove ( ) ;
422
+ path . insertBefore ( importNode ) ;
423
+ }
424
+ }
425
+ }
426
+ }
427
+
428
+ traverseAll ( file , stripTypesVisitor ) ;
429
+
430
+ for ( const path of file . path . get ( "body" ) ) {
431
+ if ( path . type === "ExportNamedDeclaration" ) {
432
+ if ( ! ( path . node . declaration || path . node . specifiers . length ) ) {
433
+ // The babel typescript plugin will add an empty export declaration
434
+ // if there are no other imports/exports in the file.
435
+ // This is not needed for Marko file outputs since there is always
436
+ // a default export.
437
+ path . remove ( ) ;
438
+ }
439
+ } else if ( path . isImportDeclaration ( ) ) {
440
+ const importNode = path . node ;
441
+ const scriptlet = importScriptlets . get ( importNode ) ;
442
+ if ( scriptlet ) {
443
+ let hasTypes = false ;
444
+ for ( const specifier of path . get ( "specifiers" ) ) {
445
+ if (
446
+ specifier . node . type === "ImportSpecifier" &&
447
+ specifier . node . importKind === "type"
448
+ ) {
449
+ hasTypes = true ;
450
+ specifier . remove ( ) ;
451
+ }
452
+ }
453
+
454
+ path . remove ( ) ;
455
+
456
+ // Add back imports from scriptlets that were
457
+ // hoisted for the babel typescript transform.
458
+ if ( ! hasTypes || importNode . specifiers . length ) {
459
+ scriptlet . body . unshift ( importNode ) ;
460
+ }
461
+ }
462
+ }
463
+ }
464
+ }
465
+
458
466
function isMarkoOutput ( output ) {
459
467
return output === "source" || output === "migrate" ;
460
468
}
0 commit comments