Skip to content

Non-standalone components #215

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

Merged
merged 4 commits into from
Dec 20, 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 .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
src/shared
shared.js
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
node_modules
compiler
ssr
shared.js
scratch
!test/compiler
!test/ssr
.nyc_output
coverage
coverage.lcov
test/sourcemaps/*/output.js
test/sourcemaps/*/output.js.map
scratch
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
"codecov": "codecov",
"precodecov": "npm run coverage",
"lint": "eslint src test/*.js",
"build": "npm run build:main && npm run build:ssr",
"build:main": "rollup -c rollup.config.main.js",
"build:ssr": "rollup -c rollup.config.ssr.js",
"build": "npm run build:main && npm run build:shared && npm run build:ssr",
"build:main": "rollup -c rollup/rollup.config.main.js",
"build:shared": "rollup -c rollup/rollup.config.shared.js",
"build:ssr": "rollup -c rollup/rollup.config.ssr.js",
"pretest": "npm run build",
"prepublish": "npm run lint && npm run build"
},
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions rollup/rollup.config.shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
entry: 'src/shared/index.js',
dest: 'shared.js',
format: 'es'
};
File renamed without changes.
42 changes: 32 additions & 10 deletions src/generators/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,26 @@ export default function dom ( parsed, source, options, names ) {
builders.main.addBlock( `${name}.prototype = template.methods;` );
}

builders.main.addBlock( deindent`
${name}.prototype.get = ${shared.get};
const standalone = options.standalone !== false;

builders.main.addBlock( standalone ?
deindent`
${name}.prototype.get = ${shared.get};

${name}.prototype.fire = ${shared.fire};
${name}.prototype.fire = ${shared.fire};

${name}.prototype.observe = ${shared.observe};
${name}.prototype.observe = ${shared.observe};

${name}.prototype.on = ${shared.on};
${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`
${name}.prototype.set = function set ( newState ) {
${builders.set}
};
Expand All @@ -325,12 +336,23 @@ export default function dom ( parsed, source, options, names ) {
};
` );

builders.main.addBlock( shared.dispatchObservers.toString() );
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() );
});
Object.keys( generator.uses ).forEach( key => {
const fn = shared[ key ]; // eslint-disable-line import/namespace
builders.main.addBlock( fn.toString() );
});
} else {
if ( format !== 'es' ) {
throw new Error( `Non-standalone components 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'`
);
}

return generator.generate( builders.main.toString(), options, { name, format } );
}
30 changes: 23 additions & 7 deletions test/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import * as acorn from 'acorn';

import { svelte, env, setupHtmlEqual } from './helpers.js';

const cache = {};

let showCompiledCode = false;
let compileOptions = null;

require.extensions[ '.html' ] = function ( module, filename ) {
const options = Object.assign({ filename }, compileOptions );
const code = cache[ filename ] || ( cache[ filename ] = svelte.compile( fs.readFileSync( filename, 'utf-8' ), options ).code );
const { code } = svelte.compile( fs.readFileSync( filename, 'utf-8' ), options );

if ( showCompiledCode ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console

return module._compile( code, filename );
return module._compile( code.replace( 'svelte/shared.js', path.resolve( 'shared.js' ) ), filename );
};

function addLineNumbers ( code ) {
Expand All @@ -30,7 +29,9 @@ function addLineNumbers ( code ) {

function loadConfig ( dir ) {
try {
return require( `./generator/${dir}/_config.js` ).default;
const resolved = require.resolve( `./generator/${dir}/_config.js` );
delete require.cache[ resolved ];
return require( resolved ).default;
} catch ( err ) {
if ( err.code === 'E_NOT_FOUND' ) {
return {};
Expand All @@ -43,7 +44,7 @@ function loadConfig ( dir ) {
describe( 'generate', () => {
before( setupHtmlEqual );

fs.readdirSync( 'test/generator' ).forEach( dir => {
function runTest ( dir, standalone ) {
if ( dir[0] === '.' ) return;

const config = loadConfig( dir );
Expand All @@ -53,6 +54,7 @@ describe( 'generate', () => {

showCompiledCode = config.show;
compileOptions = config.compileOptions || {};
compileOptions.standalone = standalone;

try {
const source = fs.readFileSync( `test/generator/${dir}/main.html`, 'utf-8' );
Expand All @@ -78,7 +80,9 @@ describe( 'generate', () => {
throw err;
}

cache[ path.resolve( `test/generator/${dir}/main.html` ) ] = code;
Object.keys( require.cache ).filter( x => x.endsWith( '.html' ) ).forEach( file => {
delete require.cache[ file ];
});

let SvelteComponent;

Expand Down Expand Up @@ -117,5 +121,17 @@ describe( 'generate', () => {
throw err;
});
});
}

describe( 'standalone', () => {
fs.readdirSync( 'test/generator' ).forEach( dir => {
runTest( dir, true );
});
});

describe( 'non-standalone', () => {
fs.readdirSync( 'test/generator' ).forEach( dir => {
runTest( dir, false );
});
});
});
8 changes: 8 additions & 0 deletions test/generator/component-ref/Widget.html
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
<p>i am a widget</p>

<script>
export default {
onrender () {
this.isWidget = true;
}
};
</script>
4 changes: 1 addition & 3 deletions test/generator/component-ref/_config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import Widget from './Widget.html';

export default {
html: '<div><p>i am a widget</p></div>',
test ( assert, component ) {
const widget = component.refs.widget;
assert.ok( widget instanceof Widget );
assert.ok( widget.isWidget );
}
};