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

Multiple components #72

Merged
merged 12 commits into from
Apr 2, 2017
Prev Previous commit
Next Next commit
nested components work
Rich-Harris committed Apr 1, 2017
commit 1a5b15c94471b1f9a8b7329849bcc4266be31112
3 changes: 2 additions & 1 deletion examples/manifest.json
Original file line number Diff line number Diff line change
@@ -7,5 +7,6 @@
"line-chart",
"area-chart",
"bar-chart",
"self-references"
"self-references",
"nested-components"
]
12 changes: 12 additions & 0 deletions examples/nested-components/App.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<p>This is a top-level element.</p>
<Nested/>

<script>
import Nested from './Nested.html';

export default {
components: {
Nested
}
};
</script>
1 change: 1 addition & 0 deletions examples/nested-components/Nested.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>And this is a nested component.</p>
3 changes: 3 additions & 0 deletions examples/nested-components/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "Nested components"
}
2 changes: 2 additions & 0 deletions scripts/prep/build-examples.js
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@ const summary = [];
manifest.forEach( id => {
const example = require( `${root}/examples/${id}/example.json` );

example.data = example.data || {};

example.components = glob.sync( '**/*.html', { cwd: `${root}/examples/${id}` })
.map( file => {
return {
1 change: 1 addition & 0 deletions shared/routes/Repl/ModuleSelector.html
Original file line number Diff line number Diff line change
@@ -56,6 +56,7 @@
top: 0.55rem;
font-size: 0.8em;
color: #999;
pointer-events: none;
}

select {
45 changes: 37 additions & 8 deletions shared/routes/Repl/index.html
Original file line number Diff line number Diff line change
@@ -640,18 +640,16 @@ <h2 class='show-if-mobile'>Rendered component</h2>

let uid = 1;
const importMap = new Map();
const entry = './App.html';

this.bundlePromise = rollup.rollup({
entry: '\0App.html',
entry,
external: id => {
return id.charCodeAt( 0 ) > 0;
return id.charCodeAt( 0 ) > 0 && id[0] !== '.';
},
plugins: [{
resolveId ( importee, importer ) {
if ( !importer ) return importee;
if ( importee[0] === '.' ) {
return `\0${importee.slice( 2 )}`;
}
if ( importee[0] === '.' ) return `\0${importee.slice( 2 )}`;
},
load ( id ) {
return lookup[ id ];
@@ -733,6 +731,17 @@ <h2 class='show-if-mobile'>Rendered component</h2>
lastSelectedExample = id;
getJSON( `/examples/${id}.json` ).then( example => {
exampleComponents = JSON.stringify( example.components );

// compile non-selected components
if ( window.svelte ) {
example.components.slice( 1 ).forEach( component => {
component.compiled = svelte.compile( component.source, {
name: component.name,
filename: component.name + '.html'
});
});
}

this.set({
components: example.components,
selectedComponent: example.components[0],
@@ -773,6 +782,24 @@ <h2 class='show-if-mobile'>Rendered component</h2>

this.set({ loadedSvelte: true });

this.get( 'components' ).forEach( component => {
// skip the selected component, that'll come soon enough
if ( component === this.get( 'selectedComponent' ) ) return;

try {
component.compiled = svelte.compile( component.source || '', {
name: component.name,
filename: component.name + '.html'
});
} catch ( err ) {
console.error( err.stack );

this.set({
sourceError: { message: err.shortMessage || err.message, pos: err.pos }
});
}
});

this.observe( 'selectedComponent', selectedComponent => {
if ( !selectedComponent ) return;

@@ -782,7 +809,10 @@ <h2 class='show-if-mobile'>Rendered component</h2>
}

try {
selectedComponent.compiled = svelte.compile( selectedComponent.source || '' );
selectedComponent.compiled = svelte.compile( selectedComponent.source || '', {
name: selectedComponent.name,
filename: selectedComponent.name + '.html'
});
this.set({ selectedComponent, sourceError: null });

if ( this.get( 'loadedRollup' ) ) {
@@ -795,7 +825,6 @@ <h2 class='show-if-mobile'>Rendered component</h2>
sourceError: { message: err.shortMessage || err.message, pos: err.pos }
});
}
console.groupEnd()
});

this.updateUrl();