From 3905683e647b9541aa53b5fd73f2072558d55997 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 20 Dec 2016 18:23:05 -0500 Subject: [PATCH] allow custom path for helpers (#218) --- src/generators/dom/index.js | 36 ++++++++++++++++++------------------ test/generate.js | 14 +++++++------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/generators/dom/index.js b/src/generators/dom/index.js index e64c0c424995..4700e5618ace 100644 --- a/src/generators/dom/index.js +++ b/src/generators/dom/index.js @@ -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 } ); diff --git a/test/generate.js b/test/generate.js index 387944dfc4d2..3db6ea0d4cdf 100644 --- a/test/generate.js +++ b/test/generate.js @@ -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' ) ); }); }); });