Last active
December 24, 2019 09:18
-
-
Save wkei/ee0dce59c6468e0d441f98a227fc21e1 to your computer and use it in GitHub Desktop.
Svelte built bundle js, only 300 lines...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
export let count = 0 | |
$: doubled = count * 2 | |
</script> | |
<main> | |
<h1>doubled count: {doubled}!</h1> | |
<button on:click={() => count ++}>add</button> | |
</main> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import App from './App.svelte' | |
const app = new App({ | |
target: document.body, | |
props: { | |
count: 1, | |
}, | |
}) | |
export default app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = (function() { | |
"use strict"; | |
// ------------------------------------ js helper | |
function noop() {} | |
function run(fn) { | |
return fn(); | |
} | |
function blank_object() { | |
return Object.create(null); | |
} | |
function run_all(fns) { | |
fns.forEach(run); | |
} | |
function is_function(thing) { | |
return typeof thing === "function"; | |
} | |
function safe_not_equal(a, b) { | |
return a != a | |
? b == b | |
: a !== b || | |
(a && typeof a === "object") || typeof a === "function"; | |
} | |
// ------------------------------------ js helper | |
// ------------------------------------ dom helper | |
function append(target, node) { | |
target.appendChild(node); | |
} | |
function insert(target, node, anchor) { | |
target.insertBefore(node, anchor || null); | |
} | |
function detach(node) { | |
node.parentNode.removeChild(node); | |
} | |
function element(name) { | |
return document.createElement(name); | |
} | |
function text(data) { | |
return document.createTextNode(data); | |
} | |
function space() { | |
return text(" "); | |
} | |
// return unlisten | |
function listen(node, event, handler, options) { | |
node.addEventListener(event, handler, options); | |
return () => node.removeEventListener(event, handler, options); | |
} | |
function children(element) { | |
return Array.from(element.childNodes); | |
} | |
function set_data(text, data) { | |
data = "" + data; | |
if (text.data !== data) text.data = data; | |
} | |
// ------------------------------------ dom helper | |
// ------------------------------------ component helper | |
let current_component; | |
function set_current_component(component) { | |
current_component = component; | |
} | |
const dirty_components = []; | |
const binding_callbacks = []; | |
const render_callbacks = []; | |
const flush_callbacks = []; | |
const resolved_promise = Promise.resolve(); | |
let update_scheduled = false; | |
function schedule_update() { | |
if (!update_scheduled) { | |
update_scheduled = true; | |
resolved_promise.then(flush); | |
} | |
} | |
function add_render_callback(fn) { | |
render_callbacks.push(fn); | |
} | |
function flush() { | |
const seen_callbacks = new Set(); | |
do { | |
// first, call beforeUpdate functions | |
// and update components | |
while (dirty_components.length) { | |
const component = dirty_components.shift(); | |
set_current_component(component); | |
update(component.$$); | |
} | |
while (binding_callbacks.length) binding_callbacks.pop()(); | |
// then, once components are updated, call | |
// afterUpdate functions. This may cause | |
// subsequent updates... | |
for (let i = 0; i < render_callbacks.length; i += 1) { | |
const callback = render_callbacks[i]; | |
if (!seen_callbacks.has(callback)) { | |
callback(); | |
// ...so guard against infinite loops | |
seen_callbacks.add(callback); | |
} | |
} | |
render_callbacks.length = 0; | |
} while (dirty_components.length); | |
while (flush_callbacks.length) { | |
flush_callbacks.pop()(); | |
} | |
update_scheduled = false; | |
} | |
function update($$) { | |
if ($$.fragment !== null) { | |
$$.update(); | |
run_all($$.before_update); | |
const dirty = $$.dirty; | |
$$.dirty = [-1]; | |
$$.fragment && $$.fragment.p($$.ctx, dirty); | |
$$.after_update.forEach(add_render_callback); | |
} | |
} | |
const outroing = new Set(); | |
function transition_in(block, local) { | |
if (block && block.i) { | |
outroing.delete(block); | |
block.i(local); | |
} | |
} | |
function mount_component(component, target, anchor) { | |
const { fragment, on_mount, on_destroy, after_update } = component.$$; | |
fragment && fragment.m(target, anchor); | |
// onMount happens before the initial afterUpdate | |
add_render_callback(() => { | |
const new_on_destroy = on_mount.map(run).filter(is_function); | |
if (on_destroy) { | |
on_destroy.push(...new_on_destroy); | |
} else { | |
// Edge case - component was destroyed immediately, | |
// most likely as a result of a binding initialising | |
run_all(new_on_destroy); | |
} | |
component.$$.on_mount = []; | |
}); | |
after_update.forEach(add_render_callback); | |
} | |
function destroy_component(component, detaching) { | |
const $$ = component.$$; | |
if ($$.fragment !== null) { | |
run_all($$.on_destroy); | |
$$.fragment && $$.fragment.d(detaching); | |
// TODO null out other refs, including component.$$ (but need to | |
// preserve final state?) | |
$$.on_destroy = $$.fragment = null; | |
$$.ctx = []; | |
} | |
} | |
function make_dirty(component, i) { | |
if (component.$$.dirty[0] === -1) { | |
dirty_components.push(component); | |
schedule_update(); | |
component.$$.dirty.fill(0); | |
} | |
component.$$.dirty[(i / 31) | 0] |= 1 << i % 31; | |
} | |
function init( | |
component, | |
options, | |
instance, | |
create_fragment, | |
not_equal, | |
props, | |
dirty = [-1] | |
) { | |
const parent_component = current_component; | |
set_current_component(component); | |
const prop_values = options.props || {}; | |
const $$ = (component.$$ = { | |
fragment: null, | |
ctx: null, | |
// state | |
props, | |
update: noop, | |
not_equal, | |
bound: blank_object(), | |
// lifecycle | |
on_mount: [], | |
on_destroy: [], | |
before_update: [], | |
after_update: [], | |
context: new Map( | |
parent_component ? parent_component.$$.context : [] | |
), | |
// everything else | |
callbacks: blank_object(), | |
dirty | |
}); | |
let ready = false; | |
$$.ctx = instance | |
? instance(component, prop_values, (i, ret, value = ret) => { | |
if ($$.ctx && not_equal($$.ctx[i], ($$.ctx[i] = value))) { | |
if ($$.bound[i]) $$.bound[i](value); | |
if (ready) make_dirty(component, i); | |
} | |
return ret; | |
}) | |
: []; | |
$$.update(); | |
ready = true; | |
run_all($$.before_update); | |
// `false` as a special case of no DOM component | |
$$.fragment = create_fragment ? create_fragment($$.ctx) : false; | |
if (options.target) { | |
if (options.hydrate) { | |
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
$$.fragment && $$.fragment.l(children(options.target)); | |
} else { | |
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
$$.fragment && $$.fragment.c(); | |
} | |
if (options.intro) transition_in(component.$$.fragment); | |
mount_component(component, options.target, options.anchor); | |
flush(); | |
} | |
set_current_component(parent_component); | |
} | |
// component class | |
class SvelteComponent { | |
$destroy() { | |
// call fragment.d() | |
destroy_component(this, 1); | |
this.$destroy = noop; | |
} | |
$on(type, callback) { | |
const callbacks = | |
this.$$.callbacks[type] || (this.$$.callbacks[type] = []); | |
callbacks.push(callback); | |
return () => { | |
const index = callbacks.indexOf(callback); | |
if (index !== -1) callbacks.splice(index, 1); | |
}; | |
} | |
$set() { | |
// overridden by instance, if it has props | |
} | |
} | |
// ------------------------------------ component helper | |
/* src/App.svelte generated by Svelte v3.16.5 */ | |
// only about dom | |
function create_fragment(ctx) { | |
let main; | |
let h1; | |
let t0; | |
let t1; | |
let t2; | |
let t3; | |
let button; | |
let dispose; | |
return { | |
// create elment | |
c() { | |
main = element("main"); | |
h1 = element("h1"); | |
t0 = text("Doubled Count: "); | |
t1 = text(/*doubled*/ ctx[1]); | |
t2 = text("!"); | |
t3 = space(); | |
button = element("button"); | |
button.textContent = "add"; | |
dispose = listen(button, "click", /*click_handler*/ ctx[2]); | |
}, | |
// mount to dom | |
m(target, anchor) { | |
insert(target, main, anchor); | |
append(main, h1); | |
append(h1, t0); | |
append(h1, t1); | |
append(h1, t2); | |
append(main, t3); | |
append(main, button); | |
}, | |
// update the dom | |
p(ctx, [dirty]) { | |
// set textNode content by set_data | |
if (dirty & /*doubled*/ 2) set_data(t1, /*doubled*/ ctx[1]); | |
}, | |
i: noop, | |
o: noop, | |
d(detaching) { | |
// remove from dom | |
if (detaching) detach(main); | |
// unlisten | |
dispose(); | |
} | |
}; | |
} | |
/* | |
ctx generator | |
@$$self component | |
@$$props porps: passed from component constructor | |
$$invalidate mark component as dirty with ctx idx make_dirty -> -> schedule_update -> flush | |
*/ | |
function instance($$self, $$props, $$invalidate) { | |
let { count = 0 } = $$props; | |
const click_handler = () => $$invalidate(0, count++, count); | |
// when props is updated | |
$$self.$set = $$props => { | |
if ("count" in $$props) $$invalidate(0, (count = $$props.count)); | |
}; | |
let doubled; | |
// computed update | |
$$self.$$.update = () => { | |
if ($$self.$$.dirty & /*count*/ 1) { | |
$$invalidate(1, (doubled = count * 2)); | |
} | |
}; | |
return [count, doubled, click_handler]; | |
} | |
// new instant of App.svelte | |
class App extends SvelteComponent { | |
constructor(options) { | |
super(); | |
init(this, options, instance, create_fragment, safe_not_equal, { | |
count: 0 | |
}); | |
} | |
} | |
const app = new App({ | |
target: document.body // options | |
}); | |
return app; | |
})(); | |
//# sourceMappingURL=bundle.js.map |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment