Skip to content

Commit

Permalink
allow custom path for helpers (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 20, 2016
1 parent 946adf9 commit 3905683
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
36 changes: 18 additions & 18 deletions src/generators/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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`
Expand All @@ -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 } );
Expand Down
14 changes: 7 additions & 7 deletions test/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 );
Expand All @@ -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' );
Expand Down Expand Up @@ -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' ) );
});
});
});

0 comments on commit 3905683

Please sign in to comment.