Skip to content

Commit d2db41f

Browse files
committed
remove unnecessary template declarations - fixes #440
1 parent 4b5754b commit d2db41f

File tree

3 files changed

+49
-31
lines changed

3 files changed

+49
-31
lines changed

src/generators/Generator.js

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,11 @@ export default class Generator {
266266

267267
const imports = this.imports;
268268
const computations = [];
269-
let defaultExport = null;
270-
let namespace = null;
271269
const templateProperties = {};
272270

271+
let namespace = null;
272+
let hasJs = !!js;
273+
273274
if ( js ) {
274275
this.addSourcemapLocations( js.content );
275276
const body = js.content.body.slice(); // slice, because we're going to be mutating the original
@@ -287,38 +288,14 @@ export default class Generator {
287288
}
288289
}
289290

290-
defaultExport = body.find( node => node.type === 'ExportDefaultDeclaration' );
291+
const defaultExport = body.find( node => node.type === 'ExportDefaultDeclaration' );
291292

292293
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-
311294
defaultExport.declaration.properties.forEach( prop => {
312295
templateProperties[ prop.key.name ] = prop;
313296
});
314-
315-
this.code.prependRight( js.content.start, `var ${this.alias( 'template' )} = (function () {` );
316-
} else {
317-
this.code.prependRight( js.content.start, '(function () {' );
318297
}
319298

320-
this.code.appendLeft( js.content.end, '}());' );
321-
322299
[ 'helpers', 'events', 'components' ].forEach( key => {
323300
if ( templateProperties[ key ] ) {
324301
templateProperties[ key ].value.properties.forEach( prop => {
@@ -383,10 +360,51 @@ export default class Generator {
383360
removeObjectKey( this.code, defaultExport.declaration, 'components' );
384361
}
385362
}
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+
}
386403
}
387404

388405
return {
389406
computations,
407+
hasJs,
390408
namespace,
391409
templateProperties
392410
};

src/generators/dom/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export default function dom ( parsed, source, options ) {
151151

152152
const generator = new DomGenerator( parsed, source, name, visitors, options );
153153

154-
const { computations, templateProperties, namespace } = generator.parseJs();
154+
const { computations, hasJs, templateProperties, namespace } = generator.parseJs();
155155

156156
// Remove these after version 2
157157
if ( templateProperties.onrender ) {
@@ -239,7 +239,7 @@ export default function dom ( parsed, source, options ) {
239239
${generator.helper( 'dispatchObservers' )}( this, this._observers.post, newState, oldState );
240240
` );
241241

242-
if ( parsed.js ) {
242+
if ( hasJs ) {
243243
builders.main.addBlock( `[✂${parsed.js.content.start}-${parsed.js.content.end}✂]` );
244244
}
245245

src/generators/server-side-rendering/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default function ssr ( parsed, source, options ) {
4040

4141
const generator = new SsrGenerator( parsed, source, name, visitors, options );
4242

43-
const { computations, templateProperties } = generator.parseJs();
43+
const { computations, hasJs, templateProperties } = generator.parseJs();
4444

4545
const builders = {
4646
main: new CodeBuilder(),
@@ -131,7 +131,7 @@ export default function ssr ( parsed, source, options ) {
131131
};
132132
` );
133133

134-
if ( parsed.js ) {
134+
if ( hasJs ) {
135135
builders.main.addBlock( `[✂${parsed.js.content.start}-${parsed.js.content.end}✂]` );
136136
}
137137

0 commit comments

Comments
 (0)