allow custom path for helpers (#218)

pull/219/head
Rich-Harris 8 years ago
parent 946adf99ee
commit 3905683e64

@ -302,9 +302,15 @@ export default function dom ( parsed, source, options, names ) {
builders.main.addBlock( `${name}.prototype = template.methods;` );
}
const standalone = options.standalone !== false;
const sharedPath = options.shared === true ? 'svelte/shared.js' : options.shared;
builders.main.addBlock( standalone ?
builders.main.addBlock( sharedPath ?
deindent`
${name}.prototype.get = get;
${name}.prototype.fire = fire;
${name}.prototype.observe = observe;
${name}.prototype.on = on;
` :
deindent`
${name}.prototype.get = ${shared.get};
@ -313,12 +319,6 @@ export default function dom ( parsed, source, options, names ) {
${name}.prototype.observe = ${shared.observe};
${name}.prototype.on = ${shared.on};
` :
deindent`
${name}.prototype.get = get;
${name}.prototype.fire = fire;
${name}.prototype.observe = observe;
${name}.prototype.on = on;
` );
builders.main.addBlock( deindent`
@ -336,22 +336,22 @@ export default function dom ( parsed, source, options, names ) {
};
` );
if ( standalone ) {
builders.main.addBlock( shared.dispatchObservers.toString() );
Object.keys( generator.uses ).forEach( key => {
const fn = shared[ key ]; // eslint-disable-line import/namespace
builders.main.addBlock( fn.toString() );
});
} else {
if ( sharedPath ) {
if ( format !== 'es' ) {
throw new Error( `Non-standalone components must be compiled to ES2015 modules (format: 'es')` );
throw new Error( `Components with shared helpers must be compiled to ES2015 modules (format: 'es')` );
}
const names = [ 'get', 'fire', 'observe', 'on', 'dispatchObservers' ].concat( Object.keys( generator.uses ) );
builders.main.addLineAtStart(
`import { ${names.join( ', ' )} } from 'svelte/shared.js'`
`import { ${names.join( ', ' )} } from '${sharedPath}'`
);
} else {
builders.main.addBlock( shared.dispatchObservers.toString() );
Object.keys( generator.uses ).forEach( key => {
const fn = shared[ key ]; // eslint-disable-line import/namespace
builders.main.addBlock( fn.toString() );
});
}
return generator.generate( builders.main.toString(), options, { name, format } );

@ -15,7 +15,7 @@ require.extensions[ '.html' ] = function ( module, filename ) {
if ( showCompiledCode ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console
return module._compile( code.replace( 'svelte/shared.js', path.resolve( 'shared.js' ) ), filename );
return module._compile( code, filename );
};
function addLineNumbers ( code ) {
@ -44,7 +44,7 @@ function loadConfig ( dir ) {
describe( 'generate', () => {
before( setupHtmlEqual );
function runTest ( dir, standalone ) {
function runTest ( dir, shared ) {
if ( dir[0] === '.' ) return;
const config = loadConfig( dir );
@ -54,7 +54,7 @@ describe( 'generate', () => {
showCompiledCode = config.show;
compileOptions = config.compileOptions || {};
compileOptions.standalone = standalone;
compileOptions.shared = shared;
try {
const source = fs.readFileSync( `test/generator/${dir}/main.html`, 'utf-8' );
@ -123,15 +123,15 @@ describe( 'generate', () => {
});
}
describe( 'standalone', () => {
describe( 'inline helpers', () => {
fs.readdirSync( 'test/generator' ).forEach( dir => {
runTest( dir, true );
runTest( dir, null );
});
});
describe( 'non-standalone', () => {
describe( 'shared helpers', () => {
fs.readdirSync( 'test/generator' ).forEach( dir => {
runTest( dir, false );
runTest( dir, path.resolve( 'shared.js' ) );
});
});
});

Loading…
Cancel
Save