initial support for dev versions of shared helpers

pull/378/head
Conduitry 8 years ago
parent 7525d6417d
commit de042daed3

@ -9,12 +9,13 @@ import getOutro from './shared/utils/getOutro.js';
import annotateWithScopes from './annotateWithScopes.js'; import annotateWithScopes from './annotateWithScopes.js';
export default class Generator { export default class Generator {
constructor ( parsed, source, name, names, visitors ) { constructor ( parsed, source, name, names, visitors, options ) {
this.parsed = parsed; this.parsed = parsed;
this.source = source; this.source = source;
this.name = name; this.name = name;
this.names = names; this.names = names;
this.visitors = visitors; this.visitors = visitors;
this.options = options;
this.imports = []; this.imports = [];
this.helpers = {}; this.helpers = {};

@ -6,10 +6,11 @@ import processCss from '../shared/processCss.js';
import visitors from './visitors/index.js'; import visitors from './visitors/index.js';
import Generator from '../Generator.js'; import Generator from '../Generator.js';
import * as shared from '../../shared/index.js'; import * as shared from '../../shared/index.js';
import devHelperLookup from './utils/devHelperLookup.js';
class DomGenerator extends Generator { class DomGenerator extends Generator {
constructor ( parsed, source, name, names, visitors ) { constructor ( parsed, source, name, names, visitors, options ) {
super( parsed, source, name, names, visitors ); super( parsed, source, name, names, visitors, options );
this.renderers = []; this.renderers = [];
this.uses = {}; this.uses = {};
@ -132,6 +133,9 @@ class DomGenerator extends Generator {
} }
helper ( name ) { helper ( name ) {
if ( this.options.dev && devHelperLookup[ name ] ) {
name = devHelperLookup[ name ];
}
this.uses[ name ] = true; this.uses[ name ] = true;
if ( !( name in this.aliases ) ) { if ( !( name in this.aliases ) ) {
@ -152,7 +156,7 @@ export default function dom ( parsed, source, options, names ) {
const format = options.format || 'es'; const format = options.format || 'es';
const name = options.name || 'SvelteComponent'; const name = options.name || 'SvelteComponent';
const generator = new DomGenerator( parsed, source, name, names, visitors ); const generator = new DomGenerator( parsed, source, name, names, visitors, options );
const { computations, templateProperties } = generator.parseJs(); const { computations, templateProperties } = generator.parseJs();
@ -362,28 +366,14 @@ export default function dom ( parsed, source, options, names ) {
const sharedPath = options.shared === true ? 'svelte/shared.js' : options.shared; const sharedPath = options.shared === true ? 'svelte/shared.js' : options.shared;
builders.main.addBlock( sharedPath ? [ 'get', 'fire', 'observe', 'on', 'set', '_flush' ].forEach( methodName => {
deindent` if ( sharedPath ) {
${name}.prototype.get = ${generator.helper( 'get' )}; builders.main.addLine( `${name}.prototype.${methodName} = ${generator.helper( methodName )};` );
${name}.prototype.fire = ${generator.helper( 'fire' )}; } else {
${name}.prototype.observe = ${generator.helper( 'observe' )}; const fn = shared[ options.dev && devHelperLookup[ methodName ] ? devHelperLookup[ methodName ] : methodName ]; // eslint-disable-line import/namespace
${name}.prototype.on = ${generator.helper( 'on' )}; builders.main.addLine( `${name}.prototype.${methodName} = ${fn};` );
${name}.prototype.set = ${generator.helper( 'set' )}; }
${name}.prototype._flush = ${generator.helper( '_flush' )}; });
` :
deindent`
${name}.prototype.get = ${shared.get};
${name}.prototype.fire = ${shared.fire};
${name}.prototype.observe = ${shared.observe};
${name}.prototype.on = ${shared.on};
${name}.prototype.set = ${shared.set};
${name}.prototype._flush = ${shared._flush};
` );
// TODO deprecate component.teardown() // TODO deprecate component.teardown()
builders.main.addBlock( deindent` builders.main.addBlock( deindent`
@ -417,11 +407,7 @@ export default function dom ( parsed, source, options, names ) {
} else { } else {
Object.keys( generator.uses ).forEach( key => { Object.keys( generator.uses ).forEach( key => {
const fn = shared[ key ]; // eslint-disable-line import/namespace const fn = shared[ key ]; // eslint-disable-line import/namespace
if ( key !== generator.aliases[ key ] ) { builders.main.addBlock( fn.toString().replace( /^function [^(]*/, 'function ' + generator.aliases[ key ] ) );
builders.main.addBlock( `var ${generator.aliases[ key ]} = ${fn.toString()}}` );
} else {
builders.main.addBlock( fn.toString() );
}
}); });
} }

@ -5,8 +5,8 @@ import visitors from './visitors/index.js';
import Generator from '../Generator.js'; import Generator from '../Generator.js';
class SsrGenerator extends Generator { class SsrGenerator extends Generator {
constructor ( parsed, source, name, names, visitors ) { constructor ( parsed, source, name, names, visitors, options ) {
super( parsed, source, name, names, visitors ); super( parsed, source, name, names, visitors, options );
this.bindings = []; this.bindings = [];
this.renderCode = ''; this.renderCode = '';
} }
@ -36,7 +36,7 @@ export default function ssr ( parsed, source, options, names ) {
const format = options.format || 'cjs'; const format = options.format || 'cjs';
const name = options.name || 'SvelteComponent'; const name = options.name || 'SvelteComponent';
const generator = new SsrGenerator( parsed, source, name, names, visitors ); const generator = new SsrGenerator( parsed, source, name, names, visitors, options );
const { computations, templateProperties } = generator.parseJs(); const { computations, templateProperties } = generator.parseJs();

Loading…
Cancel
Save