Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow custom path for helpers #219

Merged
merged 2 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"files": [
"compiler",
"ssr",
"shared.js",
"README.md"
],
"scripts": {
Expand Down
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' ) );
});
});
});