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';
export default class Generator {
constructor ( parsed, source, name, names, visitors ) {
constructor ( parsed, source, name, names, visitors, options ) {
this.parsed = parsed;
this.source = source;
this.name = name;
this.names = names;
this.visitors = visitors;
this.options = options;
this.imports = [];
this.helpers = {};

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

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

Loading…
Cancel
Save