diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 9ae999d669..dfe1a8c0b8 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -2,7 +2,7 @@ import MagicString from 'magic-string'; import isReference from 'is-reference'; import { parseExpressionAt } from 'acorn'; import annotateWithScopes from '../../utils/annotateWithScopes'; -import { walk } from 'estree-walker'; +import { walk, childKeys } from 'estree-walker'; import deindent from '../../utils/deindent'; import { stringify, escape } from '../../utils/stringify'; import CodeBuilder from '../../utils/CodeBuilder'; @@ -136,7 +136,7 @@ export default function dom( let prototypeBase = `${name}.prototype`; const proto = sharedPath - ? `@proto` + ? `@Component.prototype` : deindent` { ${['destroy', 'get', 'fire', 'on', 'set', '_set', '_mount', '_unmount', '_differs'] @@ -174,7 +174,6 @@ export default function dom( ${options.dev && `this._debugName = '${debugName}';`} ${options.dev && !generator.customElement && `if (!options || (!options.target && !options.root)) throw new Error("'target' is a required option");`} - @init(this, options); ${templateProperties.store && `this.store = %store();`} ${generator.usesRefs && `this.refs = {};`} this._state = ${initialState.reduce((state, piece) => `@assign(${state}, ${piece})`)}; @@ -332,13 +331,16 @@ export default function dom( }); `); } else { + // TODO put methods in class body builder.addBlock(deindent` - function ${name}(options) { - ${constructorBody} + class ${name} extends @Component { + constructor(options) { + super(options); + ${constructorBody} + } } - @assign(${prototypeBase}, ${proto}); - ${templateProperties.methods && `@assign(${prototypeBase}, %methods);`} + ${templateProperties.methods && `@assign(${name}.prototype, %methods);`} `); } @@ -402,6 +404,10 @@ export default function dom( } else { let inlineHelpers = ''; + // super gross hack to ensure Component isn't declared before its superclass + usedHelpers.delete('Component'); + usedHelpers.add('Thing').add('Component'); + usedHelpers.forEach(key => { const str = shared[key]; const code = new MagicString(str); @@ -424,8 +430,9 @@ export default function dom( usedHelpers.add(dependency); const alias = generator.alias(dependency); - if (alias !== node.name) + if (alias !== node.name) { code.overwrite(node.start, node.end, alias); + } } } }, @@ -439,17 +446,17 @@ export default function dom( // special case const global = `_svelteTransitionManager`; - inlineHelpers += `\n\nvar ${generator.alias('transitionManager')} = window.${global} || (window.${global} = ${code});\n\n`; + inlineHelpers += `var ${generator.alias('transitionManager')} = window.${global} || (window.${global} = ${code});\n\n`; } else { const alias = generator.alias(expression.id.name); if (alias !== expression.id.name) code.overwrite(expression.id.start, expression.id.end, alias); - inlineHelpers += `\n\n${code}`; + inlineHelpers += `${code}\n\n`; } }); - result += inlineHelpers; + result = inlineHelpers + result; } const filename = options.filename && ( diff --git a/src/shared/index.js b/src/shared/index.js index 0d7cb43bdd..b93a754357 100644 --- a/src/shared/index.js +++ b/src/shared/index.js @@ -10,128 +10,139 @@ export function blankObject() { return Object.create(null); } -export function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} +// TODO need to think of a suitable name for this +export class Thing { + constructor() { + this._handlers = blankObject(); + } -export function destroyDev(detach) { - destroy.call(this, detach); - this.destroy = function() { - console.warn('Component was already destroyed'); - }; -} + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; -export function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; -export function _differsImmutable(a, b) { - return a != a ? b == b : a !== b; -} + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } + + get() { + return this._state; + } -export function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); } } -export function get() { - return this._state; -} +export class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -export function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } + + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; + } -export function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; } - }; -} -export function run(fn) { - fn(); -} + if (!dirty) return; -export function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); -export function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _recompute() {} - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + _unmount() { + if (this._fragment) this._fragment.u(); } } -export function setDev(newState) { - if (typeof newState !== 'object') { - throw new Error( - this._debugName + '.set was called without an object of data key-values to update.' - ); +export class ComponentDev extends Component { + destroy(detach) { + super.destroy(detach); + this.destroy = () => { + console.warn('Component was already destroyed'); + }; } - this._checkReadOnly(newState); - set.call(this, newState); + set(newState) { + if (typeof newState !== 'object') { + throw new Error(`${this._debugName}.set was called without an object of data key-values to update.`); + } + + this._checkReadOnly(newState); + super.set(newState); + } } -export function callAll(fns) { - while (fns && fns.length) fns.shift()(); +export function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -export function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); +export function run(fn) { + fn(); } -export function _unmount() { - if (this._fragment) this._fragment.u(); +export function callAll(fns) { + while (fns && fns.length) fns.shift()(); } export function isPromise(value) { @@ -144,30 +155,4 @@ export var FAILURE = {}; export function removeFromStore() { this.store._remove(this); -} - -export var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - -export var protoDev = { - destroy: destroyDev, - get, - fire, - on, - set: setDev, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +} \ No newline at end of file diff --git a/store.js b/store.js index 42f17c9bb8..59d40618ad 100644 --- a/store.js +++ b/store.js @@ -1,81 +1,24 @@ import { + Thing, assign, blankObject, - _differs, - _differsImmutable, - get, - on, - fire + _differsImmutable } from './shared.js'; -function Store(state, options) { - this._handlers = {}; - this._dependents = []; +class Store extends Thing { + constructor(state, options) { + super(); - this._computed = blankObject(); - this._sortedComputedProperties = []; + this._dependents = []; - this._state = assign({}, state); - this._differs = options && options.immutable ? _differsImmutable : _differs; -} - -assign(Store.prototype, { - _add: function(component, props) { - this._dependents.push({ - component: component, - props: props - }); - }, - - _init: function(props) { - var state = {}; - for (var i = 0; i < props.length; i += 1) { - var prop = props[i]; - state['$' + prop] = this._state[prop]; - } - return state; - }, - - _remove: function(component) { - var i = this._dependents.length; - while (i--) { - if (this._dependents[i].component === component) { - this._dependents.splice(i, 1); - return; - } - } - }, - - _sortComputedProperties: function() { - var computed = this._computed; - var sorted = this._sortedComputedProperties = []; - var cycles; - var visited = blankObject(); + this._computed = blankObject(); + this._sortedComputedProperties = []; - function visit(key) { - if (cycles[key]) { - throw new Error('Cyclical dependency detected'); - } - - if (visited[key]) return; - visited[key] = true; - - var c = computed[key]; - - if (c) { - cycles[key] = true; - c.deps.forEach(visit); - sorted.push(c); - } - } - - for (var key in this._computed) { - cycles = blankObject(); - visit(key); - } - }, + this._state = assign({}, state); + if (options && options.immutable) this._differs = _differsImmutable; + } - compute: function(key, deps, fn) { + compute(key, deps, fn) { var store = this; var value; @@ -102,15 +45,9 @@ assign(Store.prototype, { this._computed[key] = c; this._sortComputedProperties(); - }, - - fire: fire, - - get: get, - - on: on, + } - set: function(newState) { + set(newState) { var oldState = this._state, changed = this._changed = {}, dirty = false; @@ -156,6 +93,61 @@ assign(Store.prototype, { previous: oldState }); } -}); + + _add(component, props) { + this._dependents.push({ + component: component, + props: props + }); + } + + _init(props) { + var state = {}; + for (var i = 0; i < props.length; i += 1) { + var prop = props[i]; + state['$' + prop] = this._state[prop]; + } + return state; + } + + _remove(component) { + var i = this._dependents.length; + while (i--) { + if (this._dependents[i].component === component) { + this._dependents.splice(i, 1); + return; + } + } + } + + _sortComputedProperties() { + var computed = this._computed; + var sorted = this._sortedComputedProperties = []; + var cycles; + var visited = blankObject(); + + function visit(key) { + if (cycles[key]) { + throw new Error('Cyclical dependency detected'); + } + + if (visited[key]) return; + visited[key] = true; + + var c = computed[key]; + + if (c) { + cycles[key] = true; + c.deps.forEach(visit); + sorted.push(c); + } + } + + for (var key in this._computed) { + cycles = blankObject(); + visit(key); + } + } +} export { Store }; diff --git a/test/js/samples/action/expected-bundle.js b/test/js/samples/action/expected-bundle.js index 093fb642a7..4cca72e910 100644 --- a/test/js/samples/action/expected-bundle.js +++ b/test/js/samples/action/expected-bundle.js @@ -21,116 +21,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); + + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -174,24 +176,24 @@ function create_main_fragment(component, state) { detachNode(a); }, - d: function destroy$$1() { + d: function destroy() { if (typeof link_action.destroy === 'function') link_action.destroy.call(component); } }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/action/expected.js b/test/js/samples/action/expected.js index 5562b84529..e2776ce941 100644 --- a/test/js/samples/action/expected.js +++ b/test/js/samples/action/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, assign, createElement, detachNode, insertNode, noop } from "svelte/shared.js"; function link(node) { @@ -48,17 +48,17 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/collapses-text-around-comments/expected-bundle.js b/test/js/samples/collapses-text-around-comments/expected-bundle.js index 6786d1c2dc..5e04dd6817 100644 --- a/test/js/samples/collapses-text-around-comments/expected-bundle.js +++ b/test/js/samples/collapses-text-around-comments/expected-bundle.js @@ -29,116 +29,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -185,20 +187,20 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign(data(), options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign(data(), options.data); - if (!document.getElementById("svelte-1a7i8ec-style")) add_css(); + if (!document.getElementById("svelte-1a7i8ec-style")) add_css(); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/collapses-text-around-comments/expected.js b/test/js/samples/collapses-text-around-comments/expected.js index 2f50f85a68..90c02f30b1 100644 --- a/test/js/samples/collapses-text-around-comments/expected.js +++ b/test/js/samples/collapses-text-around-comments/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { appendNode, assign, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, appendNode, assign, createElement, createText, detachNode, insertNode, noop } from "svelte/shared.js"; function data() { return { foo: 42 } @@ -45,19 +45,19 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign(data(), options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign(data(), options.data); - if (!document.getElementById("svelte-1a7i8ec-style")) add_css(); + if (!document.getElementById("svelte-1a7i8ec-style")) add_css(); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/component-static-immutable/expected-bundle.js b/test/js/samples/component-static-immutable/expected-bundle.js index 235f57755d..027e6c7cd0 100644 --- a/test/js/samples/component-static-immutable/expected-bundle.js +++ b/test/js/samples/component-static-immutable/expected-bundle.js @@ -9,120 +9,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; -function _differsImmutable(a, b) { - return a != a ? b == b : a !== b; -} + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + get() { + return this._state; + } - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; } -} -function get() { - return this._state; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } + + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; + } + + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; } - }; -} -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + if (!dirty) return; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); - - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -151,38 +149,38 @@ function create_main_fragment(component, state) { nested._unmount(); }, - d: function destroy$$1() { + d: function destroy() { nested.destroy(false); } }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - this._aftercreate = []; - } + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); - this._lock = true; - callAll(this._beforecreate); - callAll(this._oncreate); - callAll(this._aftercreate); - this._lock = false; + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } } } -assign(SvelteComponent.prototype, proto); - SvelteComponent.prototype._differs = _differsImmutable; export default SvelteComponent; diff --git a/test/js/samples/component-static-immutable/expected.js b/test/js/samples/component-static-immutable/expected.js index ee5a447d64..7512269f6a 100644 --- a/test/js/samples/component-static-immutable/expected.js +++ b/test/js/samples/component-static-immutable/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { _differsImmutable, assign, callAll, init, noop, proto } from "svelte/shared.js"; +import { Component, _differsImmutable, assign, callAll, noop } from "svelte/shared.js"; var Nested = window.Nested; @@ -32,31 +32,31 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - this._aftercreate = []; - } + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); - this._lock = true; - callAll(this._beforecreate); - callAll(this._oncreate); - callAll(this._aftercreate); - this._lock = false; + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } } } -assign(SvelteComponent.prototype, proto); - SvelteComponent.prototype._differs = _differsImmutable; export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/component-static-immutable2/expected-bundle.js b/test/js/samples/component-static-immutable2/expected-bundle.js index 235f57755d..027e6c7cd0 100644 --- a/test/js/samples/component-static-immutable2/expected-bundle.js +++ b/test/js/samples/component-static-immutable2/expected-bundle.js @@ -9,120 +9,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; -function _differsImmutable(a, b) { - return a != a ? b == b : a !== b; -} + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + get() { + return this._state; + } - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; } -} -function get() { - return this._state; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } + + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; + } + + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; } - }; -} -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + if (!dirty) return; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); - - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -151,38 +149,38 @@ function create_main_fragment(component, state) { nested._unmount(); }, - d: function destroy$$1() { + d: function destroy() { nested.destroy(false); } }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - this._aftercreate = []; - } + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); - this._lock = true; - callAll(this._beforecreate); - callAll(this._oncreate); - callAll(this._aftercreate); - this._lock = false; + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } } } -assign(SvelteComponent.prototype, proto); - SvelteComponent.prototype._differs = _differsImmutable; export default SvelteComponent; diff --git a/test/js/samples/component-static-immutable2/expected.js b/test/js/samples/component-static-immutable2/expected.js index ee5a447d64..7512269f6a 100644 --- a/test/js/samples/component-static-immutable2/expected.js +++ b/test/js/samples/component-static-immutable2/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { _differsImmutable, assign, callAll, init, noop, proto } from "svelte/shared.js"; +import { Component, _differsImmutable, assign, callAll, noop } from "svelte/shared.js"; var Nested = window.Nested; @@ -32,31 +32,31 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - this._aftercreate = []; - } + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); - this._lock = true; - callAll(this._beforecreate); - callAll(this._oncreate); - callAll(this._aftercreate); - this._lock = false; + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } } } -assign(SvelteComponent.prototype, proto); - SvelteComponent.prototype._differs = _differsImmutable; export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/component-static/expected-bundle.js b/test/js/samples/component-static/expected-bundle.js index dc4f6dbf0d..8cd994bbc4 100644 --- a/test/js/samples/component-static/expected-bundle.js +++ b/test/js/samples/component-static/expected-bundle.js @@ -9,116 +9,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _recompute() {} + + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -147,36 +149,36 @@ function create_main_fragment(component, state) { nested._unmount(); }, - d: function destroy$$1() { + d: function destroy() { nested.destroy(false); } }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - this._aftercreate = []; - } + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); - this._lock = true; - callAll(this._beforecreate); - callAll(this._oncreate); - callAll(this._aftercreate); - this._lock = false; + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/component-static/expected.js b/test/js/samples/component-static/expected.js index 93584db49e..541ebae801 100644 --- a/test/js/samples/component-static/expected.js +++ b/test/js/samples/component-static/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, callAll, init, noop, proto } from "svelte/shared.js"; +import { Component, assign, callAll, noop } from "svelte/shared.js"; var Nested = window.Nested; @@ -32,29 +32,29 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - this._aftercreate = []; - } + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); - this._lock = true; - callAll(this._beforecreate); - callAll(this._oncreate); - callAll(this._aftercreate); - this._lock = false; + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/computed-collapsed-if/expected-bundle.js b/test/js/samples/computed-collapsed-if/expected-bundle.js index 8cd4bf0011..5183205365 100644 --- a/test/js/samples/computed-collapsed-if/expected-bundle.js +++ b/test/js/samples/computed-collapsed-if/expected-bundle.js @@ -9,116 +9,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); + + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _recompute() {} + + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -145,21 +147,21 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); - this._recompute({ x: 1 }, this._state); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); + this._recompute({ x: 1 }, this._state); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - SvelteComponent.prototype._recompute = function _recompute(changed, state) { if (changed.x) { if (this._differs(state.a, (state.a = a(state)))) changed.a = true; diff --git a/test/js/samples/computed-collapsed-if/expected.js b/test/js/samples/computed-collapsed-if/expected.js index f1228ca0b6..2527cd6c3d 100644 --- a/test/js/samples/computed-collapsed-if/expected.js +++ b/test/js/samples/computed-collapsed-if/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, init, noop, proto } from "svelte/shared.js"; +import { Component, assign, noop } from "svelte/shared.js"; function a({ x }) { return x * 2; @@ -24,21 +24,21 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); - this._recompute({ x: 1 }, this._state); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); + this._recompute({ x: 1 }, this._state); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - SvelteComponent.prototype._recompute = function _recompute(changed, state) { if (changed.x) { if (this._differs(state.a, (state.a = a(state)))) changed.a = true; diff --git a/test/js/samples/css-media-query/expected-bundle.js b/test/js/samples/css-media-query/expected-bundle.js index 6b1455f9c6..9a8fd8e81e 100644 --- a/test/js/samples/css-media-query/expected-bundle.js +++ b/test/js/samples/css-media-query/expected-bundle.js @@ -25,116 +25,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); + + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -172,20 +174,20 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - if (!document.getElementById("svelte-1slhpfn-style")) add_css(); + if (!document.getElementById("svelte-1slhpfn-style")) add_css(); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/css-media-query/expected.js b/test/js/samples/css-media-query/expected.js index 893af23035..a7d97adcc9 100644 --- a/test/js/samples/css-media-query/expected.js +++ b/test/js/samples/css-media-query/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { appendNode, assign, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, appendNode, assign, createElement, detachNode, insertNode, noop } from "svelte/shared.js"; function add_css() { var style = createElement("style"); @@ -35,19 +35,19 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - if (!document.getElementById("svelte-1slhpfn-style")) add_css(); + if (!document.getElementById("svelte-1slhpfn-style")) add_css(); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js b/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js index c2f35f7b98..cce0a24212 100644 --- a/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js +++ b/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js @@ -21,116 +21,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); + + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _recompute() {} + + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -161,7 +163,6 @@ function create_main_fragment(component, state) { class SvelteComponent extends HTMLElement { constructor(options = {}) { super(); - init(this, options); this._state = assign({}, options.data); this.attachShadow({ mode: 'open' }); @@ -185,7 +186,7 @@ class SvelteComponent extends HTMLElement { } customElements.define("custom-element", SvelteComponent); -assign(assign(SvelteComponent.prototype, proto), { +assign(assign(SvelteComponent.prototype, Component.prototype), { _mount(target, anchor) { target.insertBefore(this, anchor); }, diff --git a/test/js/samples/css-shadow-dom-keyframes/expected.js b/test/js/samples/css-shadow-dom-keyframes/expected.js index eeac231db1..2487f11d97 100644 --- a/test/js/samples/css-shadow-dom-keyframes/expected.js +++ b/test/js/samples/css-shadow-dom-keyframes/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, assign, createElement, detachNode, insertNode, noop } from "svelte/shared.js"; function create_main_fragment(component, state) { var div; @@ -28,7 +28,6 @@ function create_main_fragment(component, state) { class SvelteComponent extends HTMLElement { constructor(options = {}) { super(); - init(this, options); this._state = assign({}, options.data); this.attachShadow({ mode: 'open' }); @@ -52,7 +51,7 @@ class SvelteComponent extends HTMLElement { } customElements.define("custom-element", SvelteComponent); -assign(assign(SvelteComponent.prototype, proto), { +assign(assign(SvelteComponent.prototype, Component.prototype), { _mount(target, anchor) { target.insertBefore(this, anchor); }, diff --git a/test/js/samples/deconflict-builtins/expected-bundle.js b/test/js/samples/deconflict-builtins/expected-bundle.js index 2f9dd2db1d..284f281fd8 100644 --- a/test/js/samples/deconflict-builtins/expected-bundle.js +++ b/test/js/samples/deconflict-builtins/expected-bundle.js @@ -39,116 +39,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -220,7 +222,7 @@ function create_main_fragment(component, state) { detachNode(each_anchor); }, - d: function destroy$$1() { + d: function destroy() { destroyEach(each_blocks); } }; @@ -259,18 +261,18 @@ function create_each_block(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/deconflict-builtins/expected-v2.js b/test/js/samples/deconflict-builtins/expected-v2.js new file mode 100644 index 0000000000..f6861d2557 --- /dev/null +++ b/test/js/samples/deconflict-builtins/expected-v2.js @@ -0,0 +1,124 @@ +/* generated by Svelte vX.Y.Z */ +import { appendNode, assign, createComment, createElement, createText, destroyEach, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var each_anchor; + + var each_value = state.createElement; + + var each_blocks = []; + + for (var i = 0; i < each_value.length; i += 1) { + each_blocks[i] = create_each_block(component, assign(assign({}, state), { + each_value: each_value, + node: each_value[i], + node_index: i + })); + } + + return { + c: function create() { + for (var i = 0; i < each_blocks.length; i += 1) { + each_blocks[i].c(); + } + + each_anchor = createComment(); + }, + + m: function mount(target, anchor) { + for (var i = 0; i < each_blocks.length; i += 1) { + each_blocks[i].m(target, anchor); + } + + insertNode(each_anchor, target, anchor); + }, + + p: function update(changed, state) { + var each_value = state.createElement; + + if (changed.createElement) { + for (var i = 0; i < each_value.length; i += 1) { + var each_context = assign(assign({}, state), { + each_value: each_value, + node: each_value[i], + node_index: i + }); + + if (each_blocks[i]) { + each_blocks[i].p(changed, each_context); + } else { + each_blocks[i] = create_each_block(component, each_context); + each_blocks[i].c(); + each_blocks[i].m(each_anchor.parentNode, each_anchor); + } + } + + for (; i < each_blocks.length; i += 1) { + each_blocks[i].u(); + each_blocks[i].d(); + } + each_blocks.length = each_value.length; + } + }, + + u: function unmount() { + for (var i = 0; i < each_blocks.length; i += 1) { + each_blocks[i].u(); + } + + detachNode(each_anchor); + }, + + d: function destroy() { + destroyEach(each_blocks); + } + }; +} + +// (1:0) {#each createElement as node} +function create_each_block(component, state) { + var node = state.node, each_value = state.each_value, node_index = state.node_index; + var span, text_value = node, text; + + return { + c: function create() { + span = createElement("span"); + text = createText(text_value); + }, + + m: function mount(target, anchor) { + insertNode(span, target, anchor); + appendNode(text, span); + }, + + p: function update(changed, state) { + node = state.node; + each_value = state.each_value; + node_index = state.node_index; + if ((changed.createElement) && text_value !== (text_value = node)) { + text.data = text_value; + } + }, + + u: function unmount() { + detachNode(span); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/deconflict-builtins/expected.js b/test/js/samples/deconflict-builtins/expected.js index f6861d2557..da60ff688c 100644 --- a/test/js/samples/deconflict-builtins/expected.js +++ b/test/js/samples/deconflict-builtins/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { appendNode, assign, createComment, createElement, createText, destroyEach, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, appendNode, assign, createComment, createElement, createText, destroyEach, detachNode, insertNode, noop } from "svelte/shared.js"; function create_main_fragment(component, state) { var each_anchor; @@ -108,17 +108,17 @@ function create_each_block(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/deconflict-globals/expected-bundle.js b/test/js/samples/deconflict-globals/expected-bundle.js index 214eed9578..73a4e1c20b 100644 --- a/test/js/samples/deconflict-globals/expected-bundle.js +++ b/test/js/samples/deconflict-globals/expected-bundle.js @@ -9,116 +9,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } + + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _recompute() {} + + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -146,33 +148,33 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign(data_1(), options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign(data_1(), options.data); - var self = this; - var _oncreate = function() { - var changed = { }; - oncreate.call(self); - self.fire("update", { changed: changed, current: self._state }); - }; + var self = this; + var _oncreate = function() { + var changed = { }; + oncreate.call(self); + self.fire("update", { changed: changed, current: self._state }); + }; - if (!options.root) { - this._oncreate = []; - } + if (!options.root) { + this._oncreate = []; + } - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - this.root._oncreate.push(_oncreate); + this.root._oncreate.push(_oncreate); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); - callAll(this._oncreate); + callAll(this._oncreate); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/deconflict-globals/expected.js b/test/js/samples/deconflict-globals/expected.js index 91192f4cca..7922871df4 100644 --- a/test/js/samples/deconflict-globals/expected.js +++ b/test/js/samples/deconflict-globals/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, callAll, init, noop, proto } from "svelte/shared.js"; +import { Component, assign, callAll, noop } from "svelte/shared.js"; function data_1() { return { @@ -26,32 +26,32 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign(data_1(), options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign(data_1(), options.data); - var self = this; - var _oncreate = function() { - var changed = { }; - oncreate.call(self); - self.fire("update", { changed: changed, current: self._state }); - }; + var self = this; + var _oncreate = function() { + var changed = { }; + oncreate.call(self); + self.fire("update", { changed: changed, current: self._state }); + }; - if (!options.root) { - this._oncreate = []; - } + if (!options.root) { + this._oncreate = []; + } - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - this.root._oncreate.push(_oncreate); + this.root._oncreate.push(_oncreate); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); - callAll(this._oncreate); + callAll(this._oncreate); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/dev-warning-missing-data-computed/expected-bundle.js b/test/js/samples/dev-warning-missing-data-computed/expected-bundle.js index e0ff6a1171..8e0ccbcf02 100644 --- a/test/js/samples/dev-warning-missing-data-computed/expected-bundle.js +++ b/test/js/samples/dev-warning-missing-data-computed/expected-bundle.js @@ -29,135 +29,137 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function destroyDev(detach) { - destroy.call(this, detach); - this.destroy = function() { - console.warn('Component was already destroyed'); - }; -} + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + get() { + return this._state; + } - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; } -} -function get() { - return this._state; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } + + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; + } + + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; } - }; -} -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + if (!dirty) return; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } + } + + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _recompute() {} - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + _unmount() { + if (this._fragment) this._fragment.u(); } } -function setDev(newState) { - if (typeof newState !== 'object') { - throw new Error( - this._debugName + '.set was called without an object of data key-values to update.' - ); +class ComponentDev extends Component { + destroy(detach) { + super.destroy(detach); + this.destroy = () => { + console.warn('Component was already destroyed'); + }; } - this._checkReadOnly(newState); - set.call(this, newState); -} + set(newState) { + if (typeof newState !== 'object') { + throw new Error(`${this._debugName}.set was called without an object of data key-values to update.`); + } -function callAll(fns) { - while (fns && fns.length) fns.shift()(); + this._checkReadOnly(newState); + super.set(newState); + } } -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -function _unmount() { - if (this._fragment) this._fragment.u(); +function callAll(fns) { + while (fns && fns.length) fns.shift()(); } -var protoDev = { - destroy: destroyDev, - get, - fire, - on, - set: setDev, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function bar({ foo }) { @@ -200,25 +202,25 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - this._debugName = ''; - if (!options || (!options.target && !options.root)) throw new Error("'target' is a required option"); - init(this, options); - this._state = assign({ Math : Math }, options.data); - this._recompute({ foo: 1 }, this._state); - if (!('foo' in this._state)) console.warn(" was created without expected data property 'foo'"); +class SvelteComponent extends ComponentDev { + constructor(options) { + super(options); + this._debugName = ''; + if (!options || (!options.target && !options.root)) throw new Error("'target' is a required option"); + this._state = assign({ Math : Math }, options.data); + this._recompute({ foo: 1 }, this._state); + if (!('foo' in this._state)) console.warn(" was created without expected data property 'foo'"); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - if (options.hydrate) throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option"); - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + if (options.hydrate) throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option"); + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, protoDev); - SvelteComponent.prototype._checkReadOnly = function _checkReadOnly(newState) { if ('bar' in newState && !this._updatingReadonlyProperty) throw new Error(": Cannot set read-only property 'bar'"); }; diff --git a/test/js/samples/dev-warning-missing-data-computed/expected-v2.js b/test/js/samples/dev-warning-missing-data-computed/expected-v2.js new file mode 100644 index 0000000000..52dee37250 --- /dev/null +++ b/test/js/samples/dev-warning-missing-data-computed/expected-v2.js @@ -0,0 +1,72 @@ +/* generated by Svelte vX.Y.Z */ +import { appendNode, assign, createElement, createText, detachNode, init, insertNode, noop, protoDev } from "svelte/shared.js"; + +function bar({ foo }) { + return foo * 2; +} + +function create_main_fragment(component, state) { + var p, text_value = state.Math.max(0, state.foo), text, text_1, text_2; + + return { + c: function create() { + p = createElement("p"); + text = createText(text_value); + text_1 = createText("\n\t"); + text_2 = createText(state.bar); + }, + + m: function mount(target, anchor) { + insertNode(p, target, anchor); + appendNode(text, p); + appendNode(text_1, p); + appendNode(text_2, p); + }, + + p: function update(changed, state) { + if ((changed.Math || changed.foo) && text_value !== (text_value = state.Math.max(0, state.foo))) { + text.data = text_value; + } + + if (changed.bar) { + text_2.data = state.bar; + } + }, + + u: function unmount() { + detachNode(p); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + this._debugName = ''; + if (!options || (!options.target && !options.root)) throw new Error("'target' is a required option"); + init(this, options); + this._state = assign({ Math : Math }, options.data); + this._recompute({ foo: 1 }, this._state); + if (!('foo' in this._state)) console.warn(" was created without expected data property 'foo'"); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + if (options.hydrate) throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option"); + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, protoDev); + +SvelteComponent.prototype._checkReadOnly = function _checkReadOnly(newState) { + if ('bar' in newState && !this._updatingReadonlyProperty) throw new Error(": Cannot set read-only property 'bar'"); +}; + +SvelteComponent.prototype._recompute = function _recompute(changed, state) { + if (changed.foo) { + if (this._differs(state.bar, (state.bar = bar(state)))) changed.bar = true; + } +} +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/dev-warning-missing-data-computed/expected.js b/test/js/samples/dev-warning-missing-data-computed/expected.js index 52dee37250..f01bfb8547 100644 --- a/test/js/samples/dev-warning-missing-data-computed/expected.js +++ b/test/js/samples/dev-warning-missing-data-computed/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { appendNode, assign, createElement, createText, detachNode, init, insertNode, noop, protoDev } from "svelte/shared.js"; +import { ComponentDev, appendNode, assign, createElement, createText, detachNode, insertNode, noop } from "svelte/shared.js"; function bar({ foo }) { return foo * 2; @@ -41,25 +41,25 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - this._debugName = ''; - if (!options || (!options.target && !options.root)) throw new Error("'target' is a required option"); - init(this, options); - this._state = assign({ Math : Math }, options.data); - this._recompute({ foo: 1 }, this._state); - if (!('foo' in this._state)) console.warn(" was created without expected data property 'foo'"); +class SvelteComponent extends ComponentDev { + constructor(options) { + super(options); + this._debugName = ''; + if (!options || (!options.target && !options.root)) throw new Error("'target' is a required option"); + this._state = assign({ Math : Math }, options.data); + this._recompute({ foo: 1 }, this._state); + if (!('foo' in this._state)) console.warn(" was created without expected data property 'foo'"); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - if (options.hydrate) throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option"); - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + if (options.hydrate) throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option"); + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, protoDev); - SvelteComponent.prototype._checkReadOnly = function _checkReadOnly(newState) { if ('bar' in newState && !this._updatingReadonlyProperty) throw new Error(": Cannot set read-only property 'bar'"); }; diff --git a/test/js/samples/do-use-dataset/expected-bundle.js b/test/js/samples/do-use-dataset/expected-bundle.js index 32af7494c3..2d544b5c15 100644 --- a/test/js/samples/do-use-dataset/expected-bundle.js +++ b/test/js/samples/do-use-dataset/expected-bundle.js @@ -25,116 +25,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -176,18 +178,18 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/do-use-dataset/expected-v2.js b/test/js/samples/do-use-dataset/expected-v2.js new file mode 100644 index 0000000000..b88636a606 --- /dev/null +++ b/test/js/samples/do-use-dataset/expected-v2.js @@ -0,0 +1,55 @@ +/* generated by Svelte vX.Y.Z */ +import { assign, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var div, text, div_1; + + return { + c: function create() { + div = createElement("div"); + text = createText("\n"); + div_1 = createElement("div"); + this.h(); + }, + + h: function hydrate() { + div.dataset.foo = "bar"; + div_1.dataset.foo = state.bar; + }, + + m: function mount(target, anchor) { + insertNode(div, target, anchor); + insertNode(text, target, anchor); + insertNode(div_1, target, anchor); + }, + + p: function update(changed, state) { + if (changed.bar) { + div_1.dataset.foo = state.bar; + } + }, + + u: function unmount() { + detachNode(div); + detachNode(text); + detachNode(div_1); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/do-use-dataset/expected.js b/test/js/samples/do-use-dataset/expected.js index b88636a606..accd7a47e8 100644 --- a/test/js/samples/do-use-dataset/expected.js +++ b/test/js/samples/do-use-dataset/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, assign, createElement, createText, detachNode, insertNode, noop } from "svelte/shared.js"; function create_main_fragment(component, state) { var div, text, div_1; @@ -39,17 +39,17 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/dont-use-dataset-in-legacy/expected-bundle.js b/test/js/samples/dont-use-dataset-in-legacy/expected-bundle.js index 5e9def1913..7a661b32a2 100644 --- a/test/js/samples/dont-use-dataset-in-legacy/expected-bundle.js +++ b/test/js/samples/dont-use-dataset-in-legacy/expected-bundle.js @@ -29,116 +29,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -180,18 +182,18 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/dont-use-dataset-in-legacy/expected-v2.js b/test/js/samples/dont-use-dataset-in-legacy/expected-v2.js new file mode 100644 index 0000000000..699da1270b --- /dev/null +++ b/test/js/samples/dont-use-dataset-in-legacy/expected-v2.js @@ -0,0 +1,55 @@ +/* generated by Svelte vX.Y.Z */ +import { assign, createElement, createText, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var div, text, div_1; + + return { + c: function create() { + div = createElement("div"); + text = createText("\n"); + div_1 = createElement("div"); + this.h(); + }, + + h: function hydrate() { + setAttribute(div, "data-foo", "bar"); + setAttribute(div_1, "data-foo", state.bar); + }, + + m: function mount(target, anchor) { + insertNode(div, target, anchor); + insertNode(text, target, anchor); + insertNode(div_1, target, anchor); + }, + + p: function update(changed, state) { + if (changed.bar) { + setAttribute(div_1, "data-foo", state.bar); + } + }, + + u: function unmount() { + detachNode(div); + detachNode(text); + detachNode(div_1); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/dont-use-dataset-in-legacy/expected.js b/test/js/samples/dont-use-dataset-in-legacy/expected.js index 699da1270b..d4fa1c0fed 100644 --- a/test/js/samples/dont-use-dataset-in-legacy/expected.js +++ b/test/js/samples/dont-use-dataset-in-legacy/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, createElement, createText, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js"; +import { Component, assign, createElement, createText, detachNode, insertNode, noop, setAttribute } from "svelte/shared.js"; function create_main_fragment(component, state) { var div, text, div_1; @@ -39,17 +39,17 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/dont-use-dataset-in-svg/expected-bundle.js b/test/js/samples/dont-use-dataset-in-svg/expected-bundle.js index 54b8382a2d..b2b7814ea6 100644 --- a/test/js/samples/dont-use-dataset-in-svg/expected-bundle.js +++ b/test/js/samples/dont-use-dataset-in-svg/expected-bundle.js @@ -29,116 +29,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -178,18 +180,18 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/dont-use-dataset-in-svg/expected-v2.js b/test/js/samples/dont-use-dataset-in-svg/expected-v2.js new file mode 100644 index 0000000000..4269367254 --- /dev/null +++ b/test/js/samples/dont-use-dataset-in-svg/expected-v2.js @@ -0,0 +1,53 @@ +/* generated by Svelte vX.Y.Z */ +import { appendNode, assign, createSvgElement, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var svg, g, g_1; + + return { + c: function create() { + svg = createSvgElement("svg"); + g = createSvgElement("g"); + g_1 = createSvgElement("g"); + this.h(); + }, + + h: function hydrate() { + setAttribute(g, "data-foo", "bar"); + setAttribute(g_1, "data-foo", state.bar); + }, + + m: function mount(target, anchor) { + insertNode(svg, target, anchor); + appendNode(g, svg); + appendNode(g_1, svg); + }, + + p: function update(changed, state) { + if (changed.bar) { + setAttribute(g_1, "data-foo", state.bar); + } + }, + + u: function unmount() { + detachNode(svg); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/dont-use-dataset-in-svg/expected.js b/test/js/samples/dont-use-dataset-in-svg/expected.js index 4269367254..beb4c48d27 100644 --- a/test/js/samples/dont-use-dataset-in-svg/expected.js +++ b/test/js/samples/dont-use-dataset-in-svg/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { appendNode, assign, createSvgElement, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js"; +import { Component, appendNode, assign, createSvgElement, detachNode, insertNode, noop, setAttribute } from "svelte/shared.js"; function create_main_fragment(component, state) { var svg, g, g_1; @@ -37,17 +37,17 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/each-block-changed-check/expected-bundle.js b/test/js/samples/each-block-changed-check/expected-bundle.js index 89c3cab243..d77c249371 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -41,116 +41,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -231,7 +233,7 @@ function create_main_fragment(component, state) { detachNode(p); }, - d: function destroy$$1() { + d: function destroy() { destroyEach(each_blocks); } }; @@ -306,18 +308,18 @@ function create_each_block(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/each-block-changed-check/expected-v2.js b/test/js/samples/each-block-changed-check/expected-v2.js new file mode 100644 index 0000000000..e7e0ccca30 --- /dev/null +++ b/test/js/samples/each-block-changed-check/expected-v2.js @@ -0,0 +1,169 @@ +/* generated by Svelte vX.Y.Z */ +import { appendNode, assign, createElement, createText, destroyEach, detachAfter, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var text, p, text_1; + + var each_value = state.comments; + + var each_blocks = []; + + for (var i = 0; i < each_value.length; i += 1) { + each_blocks[i] = create_each_block(component, assign(assign({}, state), { + each_value: each_value, + comment: each_value[i], + i: i + })); + } + + return { + c: function create() { + for (var i = 0; i < each_blocks.length; i += 1) { + each_blocks[i].c(); + } + + text = createText("\n\n"); + p = createElement("p"); + text_1 = createText(state.foo); + }, + + m: function mount(target, anchor) { + for (var i = 0; i < each_blocks.length; i += 1) { + each_blocks[i].m(target, anchor); + } + + insertNode(text, target, anchor); + insertNode(p, target, anchor); + appendNode(text_1, p); + }, + + p: function update(changed, state) { + var each_value = state.comments; + + if (changed.comments || changed.elapsed || changed.time) { + for (var i = 0; i < each_value.length; i += 1) { + var each_context = assign(assign({}, state), { + each_value: each_value, + comment: each_value[i], + i: i + }); + + if (each_blocks[i]) { + each_blocks[i].p(changed, each_context); + } else { + each_blocks[i] = create_each_block(component, each_context); + each_blocks[i].c(); + each_blocks[i].m(text.parentNode, text); + } + } + + for (; i < each_blocks.length; i += 1) { + each_blocks[i].u(); + each_blocks[i].d(); + } + each_blocks.length = each_value.length; + } + + if (changed.foo) { + text_1.data = state.foo; + } + }, + + u: function unmount() { + for (var i = 0; i < each_blocks.length; i += 1) { + each_blocks[i].u(); + } + + detachNode(text); + detachNode(p); + }, + + d: function destroy() { + destroyEach(each_blocks); + } + }; +} + +// (1:0) {#each comments as comment, i} +function create_each_block(component, state) { + var comment = state.comment, each_value = state.each_value, i = state.i; + var div, strong, text, text_1, span, text_2_value = comment.author, text_2, text_3, text_4_value = state.elapsed(comment.time, state.time), text_4, text_5, text_6, raw_value = comment.html, raw_before; + + return { + c: function create() { + div = createElement("div"); + strong = createElement("strong"); + text = createText(i); + text_1 = createText("\n\n\t\t"); + span = createElement("span"); + text_2 = createText(text_2_value); + text_3 = createText(" wrote "); + text_4 = createText(text_4_value); + text_5 = createText(" ago:"); + text_6 = createText("\n\n\t\t"); + raw_before = createElement('noscript'); + this.h(); + }, + + h: function hydrate() { + span.className = "meta"; + div.className = "comment"; + }, + + m: function mount(target, anchor) { + insertNode(div, target, anchor); + appendNode(strong, div); + appendNode(text, strong); + appendNode(text_1, div); + appendNode(span, div); + appendNode(text_2, span); + appendNode(text_3, span); + appendNode(text_4, span); + appendNode(text_5, span); + appendNode(text_6, div); + appendNode(raw_before, div); + raw_before.insertAdjacentHTML("afterend", raw_value); + }, + + p: function update(changed, state) { + comment = state.comment; + each_value = state.each_value; + i = state.i; + if ((changed.comments) && text_2_value !== (text_2_value = comment.author)) { + text_2.data = text_2_value; + } + + if ((changed.elapsed || changed.comments || changed.time) && text_4_value !== (text_4_value = state.elapsed(comment.time, state.time))) { + text_4.data = text_4_value; + } + + if ((changed.comments) && raw_value !== (raw_value = comment.html)) { + detachAfter(raw_before); + raw_before.insertAdjacentHTML("afterend", raw_value); + } + }, + + u: function unmount() { + detachAfter(raw_before); + + detachNode(div); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js index e7e0ccca30..a677d72d27 100644 --- a/test/js/samples/each-block-changed-check/expected.js +++ b/test/js/samples/each-block-changed-check/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { appendNode, assign, createElement, createText, destroyEach, detachAfter, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, appendNode, assign, createElement, createText, destroyEach, detachAfter, detachNode, insertNode, noop } from "svelte/shared.js"; function create_main_fragment(component, state) { var text, p, text_1; @@ -153,17 +153,17 @@ function create_each_block(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/event-handlers-custom/expected-bundle.js b/test/js/samples/event-handlers-custom/expected-bundle.js index 5d85e177e1..10e888c5f9 100644 --- a/test/js/samples/event-handlers-custom/expected-bundle.js +++ b/test/js/samples/event-handlers-custom/expected-bundle.js @@ -21,116 +21,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); + + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -170,25 +172,26 @@ function create_main_fragment(component, state) { detachNode(button); }, - d: function destroy$$1() { + d: function destroy() { foo_handler.destroy(); } }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); assign(SvelteComponent.prototype, methods); export default SvelteComponent; diff --git a/test/js/samples/event-handlers-custom/expected.js b/test/js/samples/event-handlers-custom/expected.js index 3975641534..978532df69 100644 --- a/test/js/samples/event-handlers-custom/expected.js +++ b/test/js/samples/event-handlers-custom/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, assign, createElement, detachNode, insertNode, noop } from "svelte/shared.js"; function foo( node, callback ) { // code goes here @@ -44,18 +44,19 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); assign(SvelteComponent.prototype, methods); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/head-no-whitespace/expected-bundle.js b/test/js/samples/head-no-whitespace/expected-bundle.js index 7f88574b70..585e44bd3b 100644 --- a/test/js/samples/head-no-whitespace/expected-bundle.js +++ b/test/js/samples/head-no-whitespace/expected-bundle.js @@ -21,116 +21,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); + + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -167,18 +169,18 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/head-no-whitespace/expected-v2.js b/test/js/samples/head-no-whitespace/expected-v2.js new file mode 100644 index 0000000000..9818f42116 --- /dev/null +++ b/test/js/samples/head-no-whitespace/expected-v2.js @@ -0,0 +1,50 @@ +/* generated by Svelte vX.Y.Z */ +import { appendNode, assign, createElement, detachNode, init, noop, proto } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var meta, meta_1; + + return { + c: function create() { + meta = createElement("meta"); + meta_1 = createElement("meta"); + this.h(); + }, + + h: function hydrate() { + meta.name = "twitter:creator"; + meta.content = "@sveltejs"; + meta_1.name = "twitter:title"; + meta_1.content = "Svelte"; + }, + + m: function mount(target, anchor) { + appendNode(meta, document.head); + appendNode(meta_1, document.head); + }, + + p: noop, + + u: function unmount() { + detachNode(meta); + detachNode(meta_1); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/head-no-whitespace/expected.js b/test/js/samples/head-no-whitespace/expected.js index 9818f42116..a2dbece70c 100644 --- a/test/js/samples/head-no-whitespace/expected.js +++ b/test/js/samples/head-no-whitespace/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { appendNode, assign, createElement, detachNode, init, noop, proto } from "svelte/shared.js"; +import { Component, appendNode, assign, createElement, detachNode, noop } from "svelte/shared.js"; function create_main_fragment(component, state) { var meta, meta_1; @@ -34,17 +34,17 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/if-block-no-update/expected-bundle.js b/test/js/samples/if-block-no-update/expected-bundle.js index 1760a2ca60..4eb0bf8bcb 100644 --- a/test/js/samples/if-block-no-update/expected-bundle.js +++ b/test/js/samples/if-block-no-update/expected-bundle.js @@ -25,116 +25,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -175,7 +177,7 @@ function create_main_fragment(component, state) { detachNode(if_block_anchor); }, - d: function destroy$$1() { + d: function destroy() { if_block.d(); } }; @@ -225,18 +227,18 @@ function create_if_block_1(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/if-block-no-update/expected-v2.js b/test/js/samples/if-block-no-update/expected-v2.js new file mode 100644 index 0000000000..c06ff6ee67 --- /dev/null +++ b/test/js/samples/if-block-no-update/expected-v2.js @@ -0,0 +1,104 @@ +/* generated by Svelte vX.Y.Z */ +import { assign, createComment, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var if_block_anchor; + + function select_block_type(state) { + if (state.foo) return create_if_block; + return create_if_block_1; + } + + var current_block_type = select_block_type(state); + var if_block = current_block_type(component, state); + + return { + c: function create() { + if_block.c(); + if_block_anchor = createComment(); + }, + + m: function mount(target, anchor) { + if_block.m(target, anchor); + insertNode(if_block_anchor, target, anchor); + }, + + p: function update(changed, state) { + if (current_block_type !== (current_block_type = select_block_type(state))) { + if_block.u(); + if_block.d(); + if_block = current_block_type(component, state); + if_block.c(); + if_block.m(if_block_anchor.parentNode, if_block_anchor); + } + }, + + u: function unmount() { + if_block.u(); + detachNode(if_block_anchor); + }, + + d: function destroy() { + if_block.d(); + } + }; +} + +// (1:0) {#if foo} +function create_if_block(component, state) { + var p; + + return { + c: function create() { + p = createElement("p"); + p.textContent = "foo!"; + }, + + m: function mount(target, anchor) { + insertNode(p, target, anchor); + }, + + u: function unmount() { + detachNode(p); + }, + + d: noop + }; +} + +// (3:0) {:else} +function create_if_block_1(component, state) { + var p; + + return { + c: function create() { + p = createElement("p"); + p.textContent = "not foo!"; + }, + + m: function mount(target, anchor) { + insertNode(p, target, anchor); + }, + + u: function unmount() { + detachNode(p); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/if-block-no-update/expected.js b/test/js/samples/if-block-no-update/expected.js index c06ff6ee67..9c3faef253 100644 --- a/test/js/samples/if-block-no-update/expected.js +++ b/test/js/samples/if-block-no-update/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, createComment, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, assign, createComment, createElement, detachNode, insertNode, noop } from "svelte/shared.js"; function create_main_fragment(component, state) { var if_block_anchor; @@ -88,17 +88,17 @@ function create_if_block_1(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/if-block-simple/expected-bundle.js b/test/js/samples/if-block-simple/expected-bundle.js index f71aafff94..85e13f2375 100644 --- a/test/js/samples/if-block-simple/expected-bundle.js +++ b/test/js/samples/if-block-simple/expected-bundle.js @@ -25,116 +25,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -173,7 +175,7 @@ function create_main_fragment(component, state) { detachNode(if_block_anchor); }, - d: function destroy$$1() { + d: function destroy() { if (if_block) if_block.d(); } }; @@ -201,18 +203,18 @@ function create_if_block(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/if-block-simple/expected-v2.js b/test/js/samples/if-block-simple/expected-v2.js new file mode 100644 index 0000000000..b3447471d3 --- /dev/null +++ b/test/js/samples/if-block-simple/expected-v2.js @@ -0,0 +1,80 @@ +/* generated by Svelte vX.Y.Z */ +import { assign, createComment, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var if_block_anchor; + + var if_block = (state.foo) && create_if_block(component, state); + + return { + c: function create() { + if (if_block) if_block.c(); + if_block_anchor = createComment(); + }, + + m: function mount(target, anchor) { + if (if_block) if_block.m(target, anchor); + insertNode(if_block_anchor, target, anchor); + }, + + p: function update(changed, state) { + if (state.foo) { + if (!if_block) { + if_block = create_if_block(component, state); + if_block.c(); + if_block.m(if_block_anchor.parentNode, if_block_anchor); + } + } else if (if_block) { + if_block.u(); + if_block.d(); + if_block = null; + } + }, + + u: function unmount() { + if (if_block) if_block.u(); + detachNode(if_block_anchor); + }, + + d: function destroy() { + if (if_block) if_block.d(); + } + }; +} + +// (1:0) {#if foo} +function create_if_block(component, state) { + var p; + + return { + c: function create() { + p = createElement("p"); + p.textContent = "foo!"; + }, + + m: function mount(target, anchor) { + insertNode(p, target, anchor); + }, + + u: function unmount() { + detachNode(p); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/if-block-simple/expected.js b/test/js/samples/if-block-simple/expected.js index b3447471d3..e5e83f7366 100644 --- a/test/js/samples/if-block-simple/expected.js +++ b/test/js/samples/if-block-simple/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, createComment, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, assign, createComment, createElement, detachNode, insertNode, noop } from "svelte/shared.js"; function create_main_fragment(component, state) { var if_block_anchor; @@ -64,17 +64,17 @@ function create_if_block(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js index bd3bacfac2..580c6472e2 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js @@ -25,116 +25,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -174,18 +176,18 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/inline-style-optimized-multiple/expected-v2.js b/test/js/samples/inline-style-optimized-multiple/expected-v2.js new file mode 100644 index 0000000000..7a94993b69 --- /dev/null +++ b/test/js/samples/inline-style-optimized-multiple/expected-v2.js @@ -0,0 +1,53 @@ +/* generated by Svelte vX.Y.Z */ +import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var div; + + return { + c: function create() { + div = createElement("div"); + this.h(); + }, + + h: function hydrate() { + setStyle(div, "color", state.color); + setStyle(div, "transform", "translate(" + state.x + "px," + state.y + "px)"); + }, + + m: function mount(target, anchor) { + insertNode(div, target, anchor); + }, + + p: function update(changed, state) { + if (changed.color) { + setStyle(div, "color", state.color); + } + + if (changed.x || changed.y) { + setStyle(div, "transform", "translate(" + state.x + "px," + state.y + "px)"); + } + }, + + u: function unmount() { + detachNode(div); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized-multiple/expected.js b/test/js/samples/inline-style-optimized-multiple/expected.js index 7a94993b69..9a7ec51e84 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected.js +++ b/test/js/samples/inline-style-optimized-multiple/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; +import { Component, assign, createElement, detachNode, insertNode, noop, setStyle } from "svelte/shared.js"; function create_main_fragment(component, state) { var div; @@ -37,17 +37,17 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized-url/expected-bundle.js b/test/js/samples/inline-style-optimized-url/expected-bundle.js index 4119905682..51a04d5de4 100644 --- a/test/js/samples/inline-style-optimized-url/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-url/expected-bundle.js @@ -25,116 +25,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -169,18 +171,18 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/inline-style-optimized-url/expected-v2.js b/test/js/samples/inline-style-optimized-url/expected-v2.js new file mode 100644 index 0000000000..5a27ecfa49 --- /dev/null +++ b/test/js/samples/inline-style-optimized-url/expected-v2.js @@ -0,0 +1,48 @@ +/* generated by Svelte vX.Y.Z */ +import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var div; + + return { + c: function create() { + div = createElement("div"); + this.h(); + }, + + h: function hydrate() { + setStyle(div, "background", "url(data:image/png;base64," + state.data + ")"); + }, + + m: function mount(target, anchor) { + insertNode(div, target, anchor); + }, + + p: function update(changed, state) { + if (changed.data) { + setStyle(div, "background", "url(data:image/png;base64," + state.data + ")"); + } + }, + + u: function unmount() { + detachNode(div); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized-url/expected.js b/test/js/samples/inline-style-optimized-url/expected.js index 5a27ecfa49..c8bdab4078 100644 --- a/test/js/samples/inline-style-optimized-url/expected.js +++ b/test/js/samples/inline-style-optimized-url/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; +import { Component, assign, createElement, detachNode, insertNode, noop, setStyle } from "svelte/shared.js"; function create_main_fragment(component, state) { var div; @@ -32,17 +32,17 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized/expected-bundle.js b/test/js/samples/inline-style-optimized/expected-bundle.js index 7643ee066d..1acfac36ec 100644 --- a/test/js/samples/inline-style-optimized/expected-bundle.js +++ b/test/js/samples/inline-style-optimized/expected-bundle.js @@ -25,116 +25,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -169,18 +171,18 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/inline-style-optimized/expected-v2.js b/test/js/samples/inline-style-optimized/expected-v2.js new file mode 100644 index 0000000000..86dabe8d21 --- /dev/null +++ b/test/js/samples/inline-style-optimized/expected-v2.js @@ -0,0 +1,48 @@ +/* generated by Svelte vX.Y.Z */ +import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var div; + + return { + c: function create() { + div = createElement("div"); + this.h(); + }, + + h: function hydrate() { + setStyle(div, "color", state.color); + }, + + m: function mount(target, anchor) { + insertNode(div, target, anchor); + }, + + p: function update(changed, state) { + if (changed.color) { + setStyle(div, "color", state.color); + } + }, + + u: function unmount() { + detachNode(div); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized/expected.js b/test/js/samples/inline-style-optimized/expected.js index 86dabe8d21..444f3307d8 100644 --- a/test/js/samples/inline-style-optimized/expected.js +++ b/test/js/samples/inline-style-optimized/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; +import { Component, assign, createElement, detachNode, insertNode, noop, setStyle } from "svelte/shared.js"; function create_main_fragment(component, state) { var div; @@ -32,17 +32,17 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-unoptimized/expected-bundle.js b/test/js/samples/inline-style-unoptimized/expected-bundle.js index 67b2734743..6e7d53f055 100644 --- a/test/js/samples/inline-style-unoptimized/expected-bundle.js +++ b/test/js/samples/inline-style-unoptimized/expected-bundle.js @@ -25,116 +25,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -180,18 +182,18 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/inline-style-unoptimized/expected-v2.js b/test/js/samples/inline-style-unoptimized/expected-v2.js new file mode 100644 index 0000000000..7d7e933c12 --- /dev/null +++ b/test/js/samples/inline-style-unoptimized/expected-v2.js @@ -0,0 +1,59 @@ +/* generated by Svelte vX.Y.Z */ +import { assign, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var div, text, div_1, div_1_style_value; + + return { + c: function create() { + div = createElement("div"); + text = createText("\n"); + div_1 = createElement("div"); + this.h(); + }, + + h: function hydrate() { + div.style.cssText = state.style; + div_1.style.cssText = div_1_style_value = "" + state.key + ": " + state.value; + }, + + m: function mount(target, anchor) { + insertNode(div, target, anchor); + insertNode(text, target, anchor); + insertNode(div_1, target, anchor); + }, + + p: function update(changed, state) { + if (changed.style) { + div.style.cssText = state.style; + } + + if ((changed.key || changed.value) && div_1_style_value !== (div_1_style_value = "" + state.key + ": " + state.value)) { + div_1.style.cssText = div_1_style_value; + } + }, + + u: function unmount() { + detachNode(div); + detachNode(text); + detachNode(div_1); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-unoptimized/expected.js b/test/js/samples/inline-style-unoptimized/expected.js index 7d7e933c12..325d4e1e6c 100644 --- a/test/js/samples/inline-style-unoptimized/expected.js +++ b/test/js/samples/inline-style-unoptimized/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, assign, createElement, createText, detachNode, insertNode, noop } from "svelte/shared.js"; function create_main_fragment(component, state) { var div, text, div_1, div_1_style_value; @@ -43,17 +43,17 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/input-without-blowback-guard/expected-bundle.js b/test/js/samples/input-without-blowback-guard/expected-bundle.js index e881aa6de4..ecc959165d 100644 --- a/test/js/samples/input-without-blowback-guard/expected-bundle.js +++ b/test/js/samples/input-without-blowback-guard/expected-bundle.js @@ -33,116 +33,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); + + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -178,24 +180,24 @@ function create_main_fragment(component, state) { detachNode(input); }, - d: function destroy$$1() { + d: function destroy() { removeListener(input, "change", input_change_handler); } }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/input-without-blowback-guard/expected.js b/test/js/samples/input-without-blowback-guard/expected.js index ba99db4541..304614a689 100644 --- a/test/js/samples/input-without-blowback-guard/expected.js +++ b/test/js/samples/input-without-blowback-guard/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { addListener, assign, createElement, detachNode, init, insertNode, proto, removeListener, setAttribute } from "svelte/shared.js"; +import { Component, addListener, assign, createElement, detachNode, insertNode, removeListener, setAttribute } from "svelte/shared.js"; function create_main_fragment(component, state) { var input; @@ -39,17 +39,17 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/legacy-input-type/expected-bundle.js b/test/js/samples/legacy-input-type/expected-bundle.js index 1119454b3d..c0c9992088 100644 --- a/test/js/samples/legacy-input-type/expected-bundle.js +++ b/test/js/samples/legacy-input-type/expected-bundle.js @@ -27,116 +27,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); + + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -167,18 +169,18 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/legacy-input-type/expected.js b/test/js/samples/legacy-input-type/expected.js index 2426c06481..2b4bcb75c4 100644 --- a/test/js/samples/legacy-input-type/expected.js +++ b/test/js/samples/legacy-input-type/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, createElement, detachNode, init, insertNode, noop, proto, setInputType } from "svelte/shared.js"; +import { Component, assign, createElement, detachNode, insertNode, noop, setInputType } from "svelte/shared.js"; function create_main_fragment(component, state) { var input; @@ -28,17 +28,17 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/media-bindings/expected-bundle.js b/test/js/samples/media-bindings/expected-bundle.js index abcdd40986..18ee72e67c 100644 --- a/test/js/samples/media-bindings/expected-bundle.js +++ b/test/js/samples/media-bindings/expected-bundle.js @@ -37,116 +37,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _recompute() {} + + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -221,7 +223,7 @@ function create_main_fragment(component, state) { detachNode(audio); }, - d: function destroy$$1() { + d: function destroy() { removeListener(audio, "timeupdate", audio_timeupdate_handler); removeListener(audio, "durationchange", audio_durationchange_handler); removeListener(audio, "play", audio_play_pause_handler); @@ -233,25 +235,25 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - } + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + } - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); - callAll(this._beforecreate); + callAll(this._beforecreate); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/media-bindings/expected.js b/test/js/samples/media-bindings/expected.js index a395dd4385..9e424a5c65 100644 --- a/test/js/samples/media-bindings/expected.js +++ b/test/js/samples/media-bindings/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { addListener, assign, callAll, createElement, detachNode, init, insertNode, proto, removeListener, timeRangesToArray } from "svelte/shared.js"; +import { Component, addListener, assign, callAll, createElement, detachNode, insertNode, removeListener, timeRangesToArray } from "svelte/shared.js"; function create_main_fragment(component, state) { var audio, audio_is_paused = true, audio_updating = false, audio_animationframe; @@ -84,24 +84,24 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - } + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + } - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); - callAll(this._beforecreate); + callAll(this._beforecreate); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/non-imported-component/expected-bundle.js b/test/js/samples/non-imported-component/expected-bundle.js index b4adec303a..475268755d 100644 --- a/test/js/samples/non-imported-component/expected-bundle.js +++ b/test/js/samples/non-imported-component/expected-bundle.js @@ -23,116 +23,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _recompute() {} + + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -170,37 +172,37 @@ function create_main_fragment(component, state) { nonimported._unmount(); }, - d: function destroy$$1() { + d: function destroy() { imported.destroy(false); nonimported.destroy(false); } }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - this._aftercreate = []; - } + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); - this._lock = true; - callAll(this._beforecreate); - callAll(this._oncreate); - callAll(this._aftercreate); - this._lock = false; + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/non-imported-component/expected.js b/test/js/samples/non-imported-component/expected.js index c1637b7cc4..830b989a72 100644 --- a/test/js/samples/non-imported-component/expected.js +++ b/test/js/samples/non-imported-component/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, callAll, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, assign, callAll, createText, detachNode, insertNode, noop } from "svelte/shared.js"; import Imported from 'Imported.html'; @@ -43,29 +43,29 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - this._aftercreate = []; - } + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); - this._lock = true; - callAll(this._beforecreate); - callAll(this._oncreate); - callAll(this._aftercreate); - this._lock = false; + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/setup-method/expected-bundle.js b/test/js/samples/setup-method/expected-bundle.js index e1aaa9ae75..272fe733b4 100644 --- a/test/js/samples/setup-method/expected-bundle.js +++ b/test/js/samples/setup-method/expected-bundle.js @@ -9,116 +9,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); + + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -128,14 +130,14 @@ var methods = { } }; -function setup(Component) { - Component.SOME_CONSTANT = 42; - Component.factory = function (target) { - return new Component({ +function setup(Component$$1) { + Component$$1.SOME_CONSTANT = 42; + Component$$1.factory = function (target) { + return new Component$$1({ target: target }); }; - Component.prototype.foo( 'baz' ); + Component$$1.prototype.foo( 'baz' ); } function create_main_fragment(component, state) { @@ -153,19 +155,20 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); assign(SvelteComponent.prototype, methods); setup(SvelteComponent); diff --git a/test/js/samples/setup-method/expected.js b/test/js/samples/setup-method/expected.js index d39c4288e3..ed4ac240be 100644 --- a/test/js/samples/setup-method/expected.js +++ b/test/js/samples/setup-method/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, init, noop, proto } from "svelte/shared.js"; +import { Component, assign, noop } from "svelte/shared.js"; var methods = { foo ( bar ) { @@ -32,19 +32,20 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); assign(SvelteComponent.prototype, methods); setup(SvelteComponent); diff --git a/test/js/samples/svg-title/expected-bundle.js b/test/js/samples/svg-title/expected-bundle.js index 477d97c839..94d1d1587d 100644 --- a/test/js/samples/svg-title/expected-bundle.js +++ b/test/js/samples/svg-title/expected-bundle.js @@ -29,116 +29,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); + + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -168,18 +170,18 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/svg-title/expected.js b/test/js/samples/svg-title/expected.js index 3f7ea0b8e9..592aa300b1 100644 --- a/test/js/samples/svg-title/expected.js +++ b/test/js/samples/svg-title/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { appendNode, assign, createSvgElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, appendNode, assign, createSvgElement, createText, detachNode, insertNode, noop } from "svelte/shared.js"; function create_main_fragment(component, state) { var svg, title, text; @@ -27,17 +27,17 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/title/expected-bundle.js b/test/js/samples/title/expected-bundle.js index 27cae18b3d..2d58cecb32 100644 --- a/test/js/samples/title/expected-bundle.js +++ b/test/js/samples/title/expected-bundle.js @@ -9,116 +9,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -144,18 +146,18 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/title/expected-v2.js b/test/js/samples/title/expected-v2.js new file mode 100644 index 0000000000..0e059bbb4c --- /dev/null +++ b/test/js/samples/title/expected-v2.js @@ -0,0 +1,39 @@ +/* generated by Svelte vX.Y.Z */ +import { assign, init, noop, proto } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var title_value; + + document.title = title_value = "a " + state.custom + " title"; + + return { + c: noop, + + m: noop, + + p: function update(changed, state) { + if ((changed.custom) && title_value !== (title_value = "a " + state.custom + " title")) { + document.title = title_value; + } + }, + + u: noop, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/title/expected.js b/test/js/samples/title/expected.js index 0e059bbb4c..db4821063a 100644 --- a/test/js/samples/title/expected.js +++ b/test/js/samples/title/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, init, noop, proto } from "svelte/shared.js"; +import { Component, assign, noop } from "svelte/shared.js"; function create_main_fragment(component, state) { var title_value; @@ -23,17 +23,17 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/use-elements-as-anchors/expected-bundle.js b/test/js/samples/use-elements-as-anchors/expected-bundle.js index 1fda063de1..634f34e04a 100644 --- a/test/js/samples/use-elements-as-anchors/expected-bundle.js +++ b/test/js/samples/use-elements-as-anchors/expected-bundle.js @@ -33,116 +33,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -271,7 +273,7 @@ function create_main_fragment(component, state) { detachNode(if_block_4_anchor); }, - d: function destroy$$1() { + d: function destroy() { if (if_block) if_block.d(); if (if_block_1) if_block_1.d(); if (if_block_2) if_block_2.d(); @@ -391,18 +393,18 @@ function create_if_block_4(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/use-elements-as-anchors/expected-v2.js b/test/js/samples/use-elements-as-anchors/expected-v2.js new file mode 100644 index 0000000000..d9cff08d3f --- /dev/null +++ b/test/js/samples/use-elements-as-anchors/expected-v2.js @@ -0,0 +1,262 @@ +/* generated by Svelte vX.Y.Z */ +import { appendNode, assign, createComment, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var div, text, p, text_2, text_3, text_4, p_1, text_6, text_8, if_block_4_anchor; + + var if_block = (state.a) && create_if_block(component, state); + + var if_block_1 = (state.b) && create_if_block_1(component, state); + + var if_block_2 = (state.c) && create_if_block_2(component, state); + + var if_block_3 = (state.d) && create_if_block_3(component, state); + + var if_block_4 = (state.e) && create_if_block_4(component, state); + + return { + c: function create() { + div = createElement("div"); + if (if_block) if_block.c(); + text = createText("\n\n\t"); + p = createElement("p"); + p.textContent = "this can be used as an anchor"; + text_2 = createText("\n\n\t"); + if (if_block_1) if_block_1.c(); + text_3 = createText("\n\n\t"); + if (if_block_2) if_block_2.c(); + text_4 = createText("\n\n\t"); + p_1 = createElement("p"); + p_1.textContent = "so can this"; + text_6 = createText("\n\n\t"); + if (if_block_3) if_block_3.c(); + text_8 = createText("\n\n"); + if (if_block_4) if_block_4.c(); + if_block_4_anchor = createComment(); + }, + + m: function mount(target, anchor) { + insertNode(div, target, anchor); + if (if_block) if_block.m(div, null); + appendNode(text, div); + appendNode(p, div); + appendNode(text_2, div); + if (if_block_1) if_block_1.m(div, null); + appendNode(text_3, div); + if (if_block_2) if_block_2.m(div, null); + appendNode(text_4, div); + appendNode(p_1, div); + appendNode(text_6, div); + if (if_block_3) if_block_3.m(div, null); + insertNode(text_8, target, anchor); + if (if_block_4) if_block_4.m(target, anchor); + insertNode(if_block_4_anchor, target, anchor); + }, + + p: function update(changed, state) { + if (state.a) { + if (!if_block) { + if_block = create_if_block(component, state); + if_block.c(); + if_block.m(div, text); + } + } else if (if_block) { + if_block.u(); + if_block.d(); + if_block = null; + } + + if (state.b) { + if (!if_block_1) { + if_block_1 = create_if_block_1(component, state); + if_block_1.c(); + if_block_1.m(div, text_3); + } + } else if (if_block_1) { + if_block_1.u(); + if_block_1.d(); + if_block_1 = null; + } + + if (state.c) { + if (!if_block_2) { + if_block_2 = create_if_block_2(component, state); + if_block_2.c(); + if_block_2.m(div, text_4); + } + } else if (if_block_2) { + if_block_2.u(); + if_block_2.d(); + if_block_2 = null; + } + + if (state.d) { + if (!if_block_3) { + if_block_3 = create_if_block_3(component, state); + if_block_3.c(); + if_block_3.m(div, null); + } + } else if (if_block_3) { + if_block_3.u(); + if_block_3.d(); + if_block_3 = null; + } + + if (state.e) { + if (!if_block_4) { + if_block_4 = create_if_block_4(component, state); + if_block_4.c(); + if_block_4.m(if_block_4_anchor.parentNode, if_block_4_anchor); + } + } else if (if_block_4) { + if_block_4.u(); + if_block_4.d(); + if_block_4 = null; + } + }, + + u: function unmount() { + detachNode(div); + if (if_block) if_block.u(); + if (if_block_1) if_block_1.u(); + if (if_block_2) if_block_2.u(); + if (if_block_3) if_block_3.u(); + detachNode(text_8); + if (if_block_4) if_block_4.u(); + detachNode(if_block_4_anchor); + }, + + d: function destroy() { + if (if_block) if_block.d(); + if (if_block_1) if_block_1.d(); + if (if_block_2) if_block_2.d(); + if (if_block_3) if_block_3.d(); + if (if_block_4) if_block_4.d(); + } + }; +} + +// (2:1) {#if a} +function create_if_block(component, state) { + var p; + + return { + c: function create() { + p = createElement("p"); + p.textContent = "a"; + }, + + m: function mount(target, anchor) { + insertNode(p, target, anchor); + }, + + u: function unmount() { + detachNode(p); + }, + + d: noop + }; +} + +// (8:1) {#if b} +function create_if_block_1(component, state) { + var p; + + return { + c: function create() { + p = createElement("p"); + p.textContent = "b"; + }, + + m: function mount(target, anchor) { + insertNode(p, target, anchor); + }, + + u: function unmount() { + detachNode(p); + }, + + d: noop + }; +} + +// (12:1) {#if c} +function create_if_block_2(component, state) { + var p; + + return { + c: function create() { + p = createElement("p"); + p.textContent = "c"; + }, + + m: function mount(target, anchor) { + insertNode(p, target, anchor); + }, + + u: function unmount() { + detachNode(p); + }, + + d: noop + }; +} + +// (18:1) {#if d} +function create_if_block_3(component, state) { + var p; + + return { + c: function create() { + p = createElement("p"); + p.textContent = "d"; + }, + + m: function mount(target, anchor) { + insertNode(p, target, anchor); + }, + + u: function unmount() { + detachNode(p); + }, + + d: noop + }; +} + +// (25:0) {#if e} +function create_if_block_4(component, state) { + var p; + + return { + c: function create() { + p = createElement("p"); + p.textContent = "e"; + }, + + m: function mount(target, anchor) { + insertNode(p, target, anchor); + }, + + u: function unmount() { + detachNode(p); + }, + + d: noop + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/use-elements-as-anchors/expected.js b/test/js/samples/use-elements-as-anchors/expected.js index d9cff08d3f..08c16e2a1e 100644 --- a/test/js/samples/use-elements-as-anchors/expected.js +++ b/test/js/samples/use-elements-as-anchors/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { appendNode, assign, createComment, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import { Component, appendNode, assign, createComment, createElement, createText, detachNode, insertNode, noop } from "svelte/shared.js"; function create_main_fragment(component, state) { var div, text, p, text_2, text_3, text_4, p_1, text_6, text_8, if_block_4_anchor; @@ -246,17 +246,17 @@ function create_if_block_4(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/window-binding-scroll/expected-bundle.js b/test/js/samples/window-binding-scroll/expected-bundle.js index df4fc6b2c9..e39ce13692 100644 --- a/test/js/samples/window-binding-scroll/expected-bundle.js +++ b/test/js/samples/window-binding-scroll/expected-bundle.js @@ -29,116 +29,118 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; - - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} - -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} +// TODO need to think of a suitable name for this +class Thing { + constructor() { + this._handlers = blankObject(); + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } -} -function get() { - return this._state; -} + get() { + return this._state; + } + + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } } -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); +class Component extends Thing { + constructor(options) { + super(); + this._bind = options._bind; - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + } -function set(newState) { - this._set(assign({}, newState)); - if (this.root._lock) return; - this.root._lock = true; - callAll(this.root._beforecreate); - callAll(this.root._oncreate); - callAll(this.root._aftercreate); - this.root._lock = false; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } + + if (!dirty) return; + + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); - if (this._fragment) { - this.fire("state", { changed: changed, current: this._state, previous: oldState }); - this._fragment.p(changed, this._state); - this.fire("update", { changed: changed, current: this._state, previous: oldState }); + if (this._fragment) { + this.fire("state", { changed, current: this._state, previous }); + this._fragment.p(changed, this._state); + this.fire("update", { changed, current: this._state, previous }); + } } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -function _unmount() { - if (this._fragment) this._fragment.u(); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} /* generated by Svelte vX.Y.Z */ @@ -188,25 +190,25 @@ function create_main_fragment(component, state) { detachNode(p); }, - d: function destroy$$1() { + d: function destroy() { window.removeEventListener("scroll", onwindowscroll); } }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); - this._state.y = window.pageYOffset; +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); + this._state.y = window.pageYOffset; - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } -assign(SvelteComponent.prototype, proto); - export default SvelteComponent; diff --git a/test/js/samples/window-binding-scroll/expected-v2.js b/test/js/samples/window-binding-scroll/expected-v2.js new file mode 100644 index 0000000000..59009aeb9b --- /dev/null +++ b/test/js/samples/window-binding-scroll/expected-v2.js @@ -0,0 +1,68 @@ +/* generated by Svelte vX.Y.Z */ +import { appendNode, assign, createElement, createText, detachNode, init, insertNode, proto } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + var window_updating = false, clear_window_updating = function() { window_updating = false; }, window_updating_timeout, p, text, text_1; + + function onwindowscroll(event) { + if (window_updating) return; + window_updating = true; + + component.set({ + y: this.pageYOffset + }); + window_updating = false; + } + window.addEventListener("scroll", onwindowscroll); + + component.observe("y", function(y) { + window_updating = true; + clearTimeout(window_updating_timeout); + window.scrollTo(window.pageXOffset, y); + window_updating_timeout = setTimeout(clear_window_updating, 100); + }); + + return { + c: function create() { + p = createElement("p"); + text = createText("scrolled to "); + text_1 = createText(state.y); + }, + + m: function mount(target, anchor) { + insertNode(p, target, anchor); + appendNode(text, p); + appendNode(text_1, p); + }, + + p: function update(changed, state) { + if (changed.y) { + text_1.data = state.y; + } + }, + + u: function unmount() { + detachNode(p); + }, + + d: function destroy() { + window.removeEventListener("scroll", onwindowscroll); + } + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + this._state.y = window.pageYOffset; + + this._fragment = create_main_fragment(this, this._state); + + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } +} + +assign(SvelteComponent.prototype, proto); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/window-binding-scroll/expected.js b/test/js/samples/window-binding-scroll/expected.js index d18b35687e..0dd9f98cc5 100644 --- a/test/js/samples/window-binding-scroll/expected.js +++ b/test/js/samples/window-binding-scroll/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { appendNode, assign, createElement, createText, detachNode, init, insertNode, proto } from "svelte/shared.js"; +import { Component, appendNode, assign, createElement, createText, detachNode, insertNode } from "svelte/shared.js"; function create_main_fragment(component, state) { var window_updating = false, clear_window_updating = function() { window_updating = false; }, window_updating_timeout, p, text, text_1; @@ -53,18 +53,18 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); - this._state.y = window.pageYOffset; +class SvelteComponent extends Component { + constructor(options) { + super(options); + this._state = assign({}, options.data); + this._state.y = window.pageYOffset; - this._fragment = create_main_fragment(this, this._state); + this._fragment = create_main_fragment(this, this._state); - if (options.target) { - this._fragment.c(); - this._mount(options.target, options.anchor); + if (options.target) { + this._fragment.c(); + this._mount(options.target, options.anchor); + } } } - -assign(SvelteComponent.prototype, proto); export default SvelteComponent; \ No newline at end of file