Skip to content

Instantly share code, notes, and snippets.

@ispyinternet
Forked from Rich-Harris/.gitignore
Last active December 24, 2016 20:12
Show Gist options
  • Save ispyinternet/b2727478ab80b330b8e283185e274e6e to your computer and use it in GitHub Desktop.
Save ispyinternet/b2727478ab80b330b8e283185e274e6e to your computer and use it in GitHub Desktop.
.DS_Store
node_modules
npm install
npm run build
open index.html
{{#if showWidget1}}
<Widget>{{widget1Text}}</Widget>
{{/if}}
<Widget>WIDGET2 TEXT</Widget>
<script>
import Widget from './Widget.html';
export default {
data() {
return {
showWidget1:true,
widget1Text:'WIDGET1'
}
},
components: { Widget }
};
</script>
import App from './App.html';
new App({
target: document.querySelector( 'main' )
});
(function () {
'use strict';
function appendNode ( node, target ) {
target.appendChild( node );
}
function insertNode ( node, target, anchor ) {
target.insertBefore( node, anchor );
}
function detachNode ( node ) {
node.parentNode.removeChild( node );
}
function createElement ( name ) {
return document.createElement( name );
}
function createText ( data ) {
return document.createTextNode( data );
}
function createComment ( data ) {
return document.createComment( data );
}
function get ( key ) {
return key ? this._state[ key ] : this._state;
}
function fire ( eventName, data ) {
var handlers = eventName in this._handlers && this._handlers[ eventName ].slice();
if ( !handlers ) return;
for ( var i = 0; i < handlers.length; i += 1 ) {
handlers[i].call( this, data );
}
}
function observe ( key, callback, options ) {
var group = ( options && options.defer ) ? this._observers.pre : this._observers.post;
( group[ key ] || ( group[ key ] = [] ) ).push( callback );
if ( !options || options.init !== false ) {
callback.__calling = true;
callback.call( this, this._state[ key ] );
callback.__calling = false;
}
return {
cancel: function () {
var index = group[ key ].indexOf( callback );
if ( ~index ) group[ key ].splice( index, 1 );
}
};
}
function on ( eventName, handler ) {
var handlers = this._handlers[ eventName ] || ( this._handlers[ eventName ] = [] );
handlers.push( handler );
return {
cancel: function () {
var index = handlers.indexOf( handler );
if ( ~index ) handlers.splice( index, 1 );
}
};
}
function noop () {}
function dispatchObservers ( component, group, newState, oldState ) {
for ( var key in group ) {
if ( !( key in newState ) ) continue;
var newValue = newState[ key ];
var oldValue = oldState[ key ];
if ( newValue === oldValue && typeof newValue !== 'object' ) continue;
var callbacks = group[ key ];
if ( !callbacks ) continue;
for ( var i = 0; i < callbacks.length; i += 1 ) {
var callback = callbacks[i];
if ( callback.__calling ) continue;
callback.__calling = true;
callback.call( component, newValue, oldValue );
callback.__calling = false;
}
}
}
function renderMainFragment$1 ( root, component ) {
var div = createElement( 'div' );
div.className = "widget";
var yield_anchor = createComment( "yield" );
appendNode( yield_anchor, div );
return {
mount: function ( target, anchor ) {
insertNode( div, target, anchor );
component._yield && component._yield.mount( div, yield_anchor );
},
update: noop,
teardown: function ( detach ) {
component._yield && component._yield.teardown( detach );
if ( detach ) {
detachNode( div );
}
},
};
}
function Widget ( options ) {
options = options || {};
this._state = options.data || {};
this._observers = {
pre: Object.create( null ),
post: Object.create( null )
};
this._handlers = Object.create( null );
this._root = options._root;
this._yield = options._yield;
this._fragment = renderMainFragment$1( this._state, this );
if ( options.target ) this._fragment.mount( options.target, null );
}
Widget.prototype.get = get;
Widget.prototype.fire = fire;
Widget.prototype.observe = observe;
Widget.prototype.on = on;
Widget.prototype.set = function set ( newState ) {
var oldState = this._state;
this._state = Object.assign( {}, oldState, newState );
dispatchObservers( this, this._observers.pre, newState, oldState );
if ( this._fragment ) this._fragment.update( newState, this._state );
dispatchObservers( this, this._observers.post, newState, oldState );
};
Widget.prototype.teardown = function teardown ( detach ) {
this.fire( 'teardown' );
this._fragment.teardown( detach !== false );
this._fragment = null;
this._state = {};
};
var template = (function () {
return {
components: { Widget }
};
}());
function renderMainFragment ( root, component ) {
var widget_yieldFragment = renderwidgetYieldFragment( root, component );
var widget = new template.components.Widget({
target: null,
_root: component._root || component,
_yield: widget_yieldFragment
});
var text = createText( "\n" );
var widget1_yieldFragment = renderwidget1YieldFragment( root, component );
var widget1 = new template.components.Widget({
target: null,
_root: component._root || component,
_yield: widget1_yieldFragment
});
return {
mount: function ( target, anchor ) {
widget._fragment.mount( target, anchor );
insertNode( text, target, anchor );
widget1._fragment.mount( target, anchor );
},
update: function ( changed, root ) {
widget_yieldFragment.update( changed, root );
widget1_yieldFragment.update( changed, root );
},
teardown: function ( detach ) {
widget.teardown( detach );
widget1.teardown( detach );
if ( detach ) {
detachNode( text );
}
},
};
}
function renderwidget1YieldFragment ( root, component ) {
var text = createText( "bar" );
return {
mount: function ( target, anchor ) {
insertNode( text, target, anchor );
},
update: noop,
teardown: function ( detach ) {
if ( detach ) {
detachNode( text );
}
},
};
}
function renderwidgetYieldFragment ( root, component ) {
var text = createText( "foo" );
return {
mount: function ( target, anchor ) {
insertNode( text, target, anchor );
},
update: noop,
teardown: function ( detach ) {
if ( detach ) {
detachNode( text );
}
},
};
}
function App ( options ) {
options = options || {};
this._state = options.data || {};
this._observers = {
pre: Object.create( null ),
post: Object.create( null )
};
this._handlers = Object.create( null );
this._root = options._root;
this._yield = options._yield;
this._renderHooks = [];
this._fragment = renderMainFragment( this._state, this );
if ( options.target ) this._fragment.mount( options.target, null );
while ( this._renderHooks.length ) {
var hook = this._renderHooks.pop();
hook.fn.call( hook.context );
}
}
App.prototype.get = get;
App.prototype.fire = fire;
App.prototype.observe = observe;
App.prototype.on = on;
App.prototype.set = function set ( newState ) {
var oldState = this._state;
this._state = Object.assign( {}, oldState, newState );
dispatchObservers( this, this._observers.pre, newState, oldState );
if ( this._fragment ) this._fragment.update( newState, this._state );
dispatchObservers( this, this._observers.post, newState, oldState );
while ( this._renderHooks.length ) {
var hook = this._renderHooks.pop();
hook.fn.call( hook.context );
}
};
App.prototype.teardown = function teardown ( detach ) {
this.fire( 'teardown' );
this._fragment.teardown( detach !== false );
this._fragment = null;
this._state = {};
};
new App({
target: document.querySelector( 'main' )
});
}());
<body>
<main></main>
<script src='bundle.js'></script>
</body>
{
"devDependencies": {
"rollup": "^0.38.0",
"rollup-plugin-svelte": "^1.6.0"
},
"scripts": {
"build": "rollup -c"
}
}
import svelte from 'rollup-plugin-svelte';
export default {
entry: 'app.js',
dest: 'bundle.js',
format: 'iife',
plugins: [ svelte() ]
};
<div class='widget'>{{yield}}</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment