@@ -266,10 +266,11 @@ export default class Generator {
266
266
267
267
const imports = this . imports ;
268
268
const computations = [ ] ;
269
- let defaultExport = null ;
270
- let namespace = null ;
271
269
const templateProperties = { } ;
272
270
271
+ let namespace = null ;
272
+ let hasJs = ! ! js ;
273
+
273
274
if ( js ) {
274
275
this . addSourcemapLocations ( js . content ) ;
275
276
const body = js . content . body . slice ( ) ; // slice, because we're going to be mutating the original
@@ -287,38 +288,14 @@ export default class Generator {
287
288
}
288
289
}
289
290
290
- defaultExport = body . find ( node => node . type === 'ExportDefaultDeclaration' ) ;
291
+ const defaultExport = body . find ( node => node . type === 'ExportDefaultDeclaration' ) ;
291
292
292
293
if ( defaultExport ) {
293
- const finalNode = body [ body . length - 1 ] ;
294
- if ( defaultExport === finalNode ) {
295
- // export is last property, we can just return it
296
- this . code . overwrite ( defaultExport . start , defaultExport . declaration . start , `return ` ) ;
297
- } else {
298
- const { declarations } = annotateWithScopes ( js ) ;
299
- let template = 'template' ;
300
- for ( let i = 1 ; declarations . has ( template ) ; template = `template_${ i ++ } ` ) ;
301
-
302
- this . code . overwrite ( defaultExport . start , defaultExport . declaration . start , `var ${ template } = ` ) ;
303
-
304
- let i = defaultExport . start ;
305
- while ( / \s / . test ( source [ i - 1 ] ) ) i -- ;
306
-
307
- const indentation = source . slice ( i , defaultExport . start ) ;
308
- this . code . appendLeft ( finalNode . end , `\n\n${ indentation } return ${ template } ;` ) ;
309
- }
310
-
311
294
defaultExport . declaration . properties . forEach ( prop => {
312
295
templateProperties [ prop . key . name ] = prop ;
313
296
} ) ;
314
-
315
- this . code . prependRight ( js . content . start , `var ${ this . alias ( 'template' ) } = (function () {` ) ;
316
- } else {
317
- this . code . prependRight ( js . content . start , '(function () {' ) ;
318
297
}
319
298
320
- this . code . appendLeft ( js . content . end , '}());' ) ;
321
-
322
299
[ 'helpers' , 'events' , 'components' ] . forEach ( key => {
323
300
if ( templateProperties [ key ] ) {
324
301
templateProperties [ key ] . value . properties . forEach ( prop => {
@@ -383,10 +360,51 @@ export default class Generator {
383
360
removeObjectKey ( this . code , defaultExport . declaration , 'components' ) ;
384
361
}
385
362
}
363
+
364
+ // now that we've analysed the default export, we can determine whether or not we need to keep it
365
+ let hasDefaultExport = ! ! defaultExport ;
366
+ if ( defaultExport && defaultExport . declaration . properties . length === 0 ) {
367
+ hasDefaultExport = false ;
368
+ removeNode ( this . code , js . content , defaultExport ) ;
369
+ }
370
+
371
+ // if we do need to keep it, then we need to generate a return statement
372
+ if ( hasDefaultExport ) {
373
+ const finalNode = body [ body . length - 1 ] ;
374
+ if ( defaultExport === finalNode ) {
375
+ // export is last property, we can just return it
376
+ this . code . overwrite ( defaultExport . start , defaultExport . declaration . start , `return ` ) ;
377
+ } else {
378
+ const { declarations } = annotateWithScopes ( js ) ;
379
+ let template = 'template' ;
380
+ for ( let i = 1 ; declarations . has ( template ) ; template = `template_${ i ++ } ` ) ;
381
+
382
+ this . code . overwrite ( defaultExport . start , defaultExport . declaration . start , `var ${ template } = ` ) ;
383
+
384
+ let i = defaultExport . start ;
385
+ while ( / \s / . test ( source [ i - 1 ] ) ) i -- ;
386
+
387
+ const indentation = source . slice ( i , defaultExport . start ) ;
388
+ this . code . appendLeft ( finalNode . end , `\n\n${ indentation } return ${ template } ;` ) ;
389
+ }
390
+ }
391
+
392
+ // user code gets wrapped in an IIFE
393
+ if ( js . content . body . length ) {
394
+ const prefix = hasDefaultExport ? `var ${ this . alias ( 'template' ) } = (function () {` : `(function () {` ;
395
+ this . code . prependRight ( js . content . start , prefix ) . appendLeft ( js . content . end , '}());' ) ;
396
+ }
397
+
398
+ // if there's no need to include user code, remove it altogether
399
+ else {
400
+ this . code . remove ( js . content . start , js . content . end ) ;
401
+ hasJs = false ;
402
+ }
386
403
}
387
404
388
405
return {
389
406
computations,
407
+ hasJs,
390
408
namespace,
391
409
templateProperties
392
410
} ;
0 commit comments