diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index 0b9b2eee9a67..a84ec430fd4e 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -174,7 +174,7 @@ export default class Generator { this.templateProperties = {}; this.walkJs(dom); - this.name = this.alias(name); + this.name = this.getUniqueName(name); if (options.customElement === true) { this.customElement = { @@ -610,8 +610,10 @@ export default class Generator { } if (templateProperties.methods && dom) { - addDeclaration('methods', templateProperties.methods.value); - + const indentationLevel = getIndentationLevel(source, templateProperties.methods.start); + if (indentationLevel) { + removeIndentation(code, templateProperties.methods.start, templateProperties.methods.end, indentationLevel, indentExclusionRanges); + } templateProperties.methods.value.properties.forEach(prop => { this.methods.add(prop.key.name); }); diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 9ae999d6695d..cc5a91bbb1fd 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'; @@ -133,17 +133,6 @@ export default function dom( ? 'svelte/shared.js' : options.shared || ''; - let prototypeBase = `${name}.prototype`; - - const proto = sharedPath - ? `@proto` - : deindent` - { - ${['destroy', 'get', 'fire', 'on', 'set', '_set', '_mount', '_unmount', '_differs'] - .map(n => `${n}: @${n}`) - .join(',\n')} - }`; - const debugName = `<${generator.customElement ? generator.tag : name}>`; // generate initial state object @@ -172,11 +161,7 @@ export default function dom( const constructorBody = deindent` ${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})`)}; ${storeProps.length > 0 && `this.store._add(this, [${storeProps.map(prop => `"${prop.slice(1)}"`)}]);`} ${generator.metaBindings} @@ -207,72 +192,101 @@ export default function dom( }];` )} - ${generator.slots.size && `this._slotted = options.slots || {};`} - - ${generator.customElement ? - deindent` - this.attachShadow({ mode: 'open' }); - ${css.code && `this.shadowRoot.innerHTML = \`\`;`} + ${generator.customElement ? deindent` + ${css.code && `this.shadowRoot.innerHTML = \`\`;`} ` : (generator.stylesheet.hasStyles && options.css !== false && - `if (!document.getElementById("${generator.stylesheet.id}-style")) @add_css();`) - } + `if (!document.getElementById("${generator.stylesheet.id}-style")) @add_css();`)} + + this._fragment = @create_main_fragment(this, this._state); ${hasInitHooks && deindent` - var self = this; - var _oncreate = function() { - var changed = { ${expectedProperties.map(p => `${p}: 1`).join(', ')} }; - ${templateProperties.onstate && `%onstate.call(self, { changed: changed, current: self._state });`} - ${templateProperties.oncreate && `%oncreate.call(self);`} - self.fire("update", { changed: changed, current: self._state }); - }; - `} + this.root._oncreate.push(() => { + const changed = { ${expectedProperties.map(p => `${p}: 1`).join(', ')} }; + ${templateProperties.onstate && `%onstate.call(this, { changed, current: this._state });`} + ${templateProperties.oncreate && `%oncreate.call(this);`} + this.fire("update", { changed, current: this._state }); + });`} - ${(hasInitHooks || generator.hasComponents || generator.hasComplexBindings || generator.hasIntroTransitions) && deindent` - if (!options.root) { - this._oncreate = []; - ${(generator.hasComponents || generator.hasComplexBindings) && `this._beforecreate = [];`} - ${(generator.hasComponents || generator.hasIntroTransitions) && `this._aftercreate = [];`} - } - `} + ${generator.customElement ? + deindent` + this._fragment.c(); + this._fragment.${block.hasIntroMethod ? 'i' : 'm'}(this.shadowRoot, null); - ${generator.slots.size && `this.slots = {};`} + if (options.target) this._mount(options.target, options.anchor); + ` : + deindent` + if (options.target) { + ${generator.hydratable + ? deindent` + var nodes = @children(options.target); + options.hydrate ? this._fragment.l(nodes) : this._fragment.c(); + nodes.forEach(@detachNode); + ` : + deindent` + ${options.dev && `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); + + ${(generator.hasComponents || generator.hasComplexBindings || hasInitHooks || generator.hasIntroTransitions) && deindent` + ${generator.hasComponents && `this._lock = true;`} + ${(generator.hasComponents || generator.hasComplexBindings) && `@callAll(this._beforecreate);`} + ${(generator.hasComponents || hasInitHooks) && `@callAll(this._oncreate);`} + ${(generator.hasComponents || generator.hasIntroTransitions) && `@callAll(this._aftercreate);`} + ${generator.hasComponents && `this._lock = false;`} + `} + }`} + `; - this._fragment = @create_main_fragment(this, this._state); + const classMethods = []; + const extraMethods = []; - ${hasInitHooks && deindent` - this.root._oncreate.push(_oncreate); - `} + if (templateProperties.methods) { + templateProperties.methods.value.properties.forEach(prop => { + if (/FunctionExpression/.test(prop.value.type)) { + const { type, params, body, async, generator: isGenerator, start, end } = prop.value; - ${generator.customElement ? deindent` - this._fragment.c(); - this._fragment.${block.hasIntroMethod ? 'i' : 'm'}(this.shadowRoot, null); - - if (options.target) this._mount(options.target, options.anchor); - ` : deindent` - if (options.target) { - ${generator.hydratable - ? deindent` - var nodes = @children(options.target); - options.hydrate ? this._fragment.l(nodes) : this._fragment.c(); - nodes.forEach(@detachNode); - ` : - deindent` - ${options.dev && `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); + const prefix = async ? `async ` : isGenerator ? `*` : ``; + const key = `${prefix}[✂${prop.key.start}-${prop.key.end}✂]`; - ${(generator.hasComponents || generator.hasComplexBindings || hasInitHooks || generator.hasIntroTransitions) && deindent` - ${generator.hasComponents && `this._lock = true;`} - ${(generator.hasComponents || generator.hasComplexBindings) && `@callAll(this._beforecreate);`} - ${(generator.hasComponents || hasInitHooks) && `@callAll(this._oncreate);`} - ${(generator.hasComponents || generator.hasIntroTransitions) && `@callAll(this._aftercreate);`} - ${generator.hasComponents && `this._lock = false;`} - `} + if (type === 'ArrowFunctionExpression') { + const paramsSnippet = params.length > 0 + ? `[✂${params[0].start}-${params[params.length - 1].end}✂]` + : ''; + + let bodySnippet = `[✂${body.start}-${body.end}✂]`; + if (body.type !== 'BlockStatement') bodySnippet = `{ return ${bodySnippet}; }`; + + classMethods.push(`${key}(${paramsSnippet}) ${bodySnippet}`) + } else if (type === 'FunctionExpression') { + let c = start; + while (generator.source[c] !== '(') c += 1; + classMethods.push(`${key}[✂${c}-${end}✂]`); + } + } else { + extraMethods.push(`[✂${prop.start}-${prop.end}✂]`) } - `} - `; + }); + } + + if (computations.length) { + classMethods.push(deindent` + _recompute(changed, state) { + ${computationBuilder} + }`); + } + + if (options.dev) { + const readonly = Array.from(generator.readonly); + if (readonly.length > 0) { + classMethods.push(deindent` + _checkReadOnly(newState) { + if (this._updatingReadonlyProperty) return; + ${readonly.map(prop => `if ('${prop}' in newState) throw new Error("${debugName}: Cannot set read-only property '${prop}'");`)} + }`); + } + } if (generator.customElement) { const props = generator.props || Array.from(generator.expectedProperties); @@ -281,6 +295,9 @@ export default function dom( class ${name} extends HTMLElement { constructor(options = {}) { super(); + this._handlers = {}; + this._init.call(this, options); + this.attachShadow({ mode: 'open' }); ${constructorBody} } @@ -289,39 +306,45 @@ export default function dom( } ${props.map(prop => deindent` - get ${prop}() { - return this.get().${prop}; - } + get ${prop}() { + return this.get().${prop}; + } - set ${prop}(value) { - this.set({ ${prop}: value }); - } - `).join('\n\n')} + set ${prop}(value) { + this.set({ ${prop}: value }); + }`).join('\n\n')} - ${generator.slots.size && deindent` - connectedCallback() { - Object.keys(this._slotted).forEach(key => { - this.appendChild(this._slotted[key]); - }); - }`} + ${classMethods.length && classMethods.join('\n\n')} attributeChangedCallback(attr, oldValue, newValue) { this.set({ [attr]: newValue }); } - ${(generator.hasComponents || generator.hasComplexBindings || templateProperties.oncreate || generator.hasIntroTransitions) && deindent` - connectedCallback() { - ${generator.hasComponents && `this._lock = true;`} - ${(generator.hasComponents || generator.hasComplexBindings) && `@callAll(this._beforecreate);`} - ${(generator.hasComponents || templateProperties.oncreate) && `@callAll(this._oncreate);`} - ${(generator.hasComponents || generator.hasIntroTransitions) && `@callAll(this._aftercreate);`} - ${generator.hasComponents && `this._lock = false;`} - } + ${(generator.slots.size || generator.hasComponents || generator.hasComplexBindings || templateProperties.oncreate || generator.hasIntroTransitions) && deindent` + connectedCallback() { + ${generator.slots.size && deindent` + Object.keys(this._slotted).forEach(key => { + this.appendChild(this._slotted[key]); + });`} + ${generator.hasComponents && `this._lock = true;`} + ${(generator.hasComponents || generator.hasComplexBindings) && `@callAll(this._beforecreate);`} + ${(generator.hasComponents || templateProperties.oncreate) && `@callAll(this._oncreate);`} + ${(generator.hasComponents || generator.hasIntroTransitions) && `@callAll(this._aftercreate);`} + ${generator.hasComponents && `this._lock = false;`} + } `} } - customElements.define("${generator.tag}", ${name}); - @assign(@assign(${prototypeBase}, ${proto}), { + Object.getOwnPropertyNames(${generator.options.dev ? `Object.getPrototypeOf(@Component.prototype)` : `@Component.prototype`}).forEach(name => { + ${name}.prototype[name] = @Component.prototype[name]; + }); + + @assign(${name}.prototype, { + fire: @Base.prototype.fire, + get: @Base.prototype.get, + on: @Base.prototype.on, + _differs: @Base.prototype._differs, + _mount(target, anchor) { target.insertBefore(this, anchor); }, @@ -330,36 +353,31 @@ export default function dom( this.parentNode.removeChild(this); } }); + + customElements.define("${generator.tag}", ${name}); `); } else { builder.addBlock(deindent` - function ${name}(options) { - ${constructorBody} - } + class ${name} extends @Component { + constructor(options) { + super(options); + ${constructorBody} + } - @assign(${prototypeBase}, ${proto}); - ${templateProperties.methods && `@assign(${prototypeBase}, %methods);`} + ${classMethods.length && classMethods.join('\n\n')} + } `); } const immutable = templateProperties.immutable ? templateProperties.immutable.value.value : options.immutable; builder.addBlock(deindent` - ${options.dev && deindent` - ${name}.prototype._checkReadOnly = function _checkReadOnly(newState) { - ${Array.from(generator.readonly).map( - prop => - `if ('${prop}' in newState && !this._updatingReadonlyProperty) throw new Error("${debugName}: Cannot set read-only property '${prop}'");` - )} - }; + ${extraMethods.length > 0 && deindent` + @assign(${name}.prototype, { + ${extraMethods.join(',\n\n')} + }); `} - ${computations.length ? deindent` - ${name}.prototype._recompute = function _recompute(changed, state) { - ${computationBuilder} - } - ` : (!sharedPath && `${name}.prototype._recompute = @noop;`)} - ${templateProperties.setup && `%setup(${name});`} ${templateProperties.preload && `${name}.preload = %preload;`} @@ -402,6 +420,12 @@ export default function dom( } else { let inlineHelpers = ''; + // super gross hack to ensure Component isn't declared before Base + usedHelpers.delete('Component'); + usedHelpers.delete('ComponentDev'); + usedHelpers.add('Base').add('Component'); + if (generator.options.dev) usedHelpers.add('ComponentDev'); + usedHelpers.forEach(key => { const str = shared[key]; const code = new MagicString(str); @@ -424,8 +448,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 +464,18 @@ 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/Base.js b/src/shared/Base.js new file mode 100644 index 000000000000..da798091f917 --- /dev/null +++ b/src/shared/Base.js @@ -0,0 +1,46 @@ +import { _differsImmutable } from './utils.js'; + +export function blankObject() { + return Object.create(null); +} + +export class Base { + constructor() { + this._handlers = blankObject(); + } + + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; + + 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; + } + } + } + + get() { + return this._state; + } + + 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); + } + }; + } + + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } +} \ No newline at end of file diff --git a/src/shared/Component.js b/src/shared/Component.js new file mode 100644 index 000000000000..33e6d1360c15 --- /dev/null +++ b/src/shared/Component.js @@ -0,0 +1,107 @@ +import { Base } from './Base.js'; +import { assign, callAll, noop } from './utils.js'; + +export class Component extends Base { + constructor(options) { + super(); + this._init(options); + } + + 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; + } + + 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; + } + + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; + } + + _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, 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); + } + + _recompute() {} + + _unmount() { + if (this._fragment) this._fragment.u(); + } +} + +export class ComponentDev extends Component { + constructor(options) { + if (!options || (!options.target && !options.root)) { + throw new Error(`'target' is a required option`); + } + + super(options); + } + + destroy(detach) { + super.destroy(detach); + this.destroy = () => { + console.warn('Component was already destroyed'); + }; + } + + 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); + } + + _checkReadOnly() {} +} \ No newline at end of file diff --git a/src/shared/SvelteElement.js b/src/shared/SvelteElement.js new file mode 100644 index 000000000000..185118e6d035 --- /dev/null +++ b/src/shared/SvelteElement.js @@ -0,0 +1,5 @@ +class SvelteElement extends HTMLElement { + constructor(options) { + + } +} \ No newline at end of file diff --git a/src/shared/_build.js b/src/shared/_build.js index 72bd5ab2a880..411e0b74ddbf 100644 --- a/src/shared/_build.js +++ b/src/shared/_build.js @@ -5,7 +5,7 @@ const acorn = require('acorn'); const declarations = {}; fs.readdirSync(__dirname).forEach(file => { - if (!/^[a-z\-]+\.js$/.test(file)) return; + if (!/^[a-z\-]+\.js$/i.test(file)) return; const source = fs.readFileSync(path.join(__dirname, file), 'utf-8'); const ast = acorn.parse(source, { diff --git a/src/shared/index.js b/src/shared/index.js index 0d7cb43bddbb..76655ca03027 100644 --- a/src/shared/index.js +++ b/src/shared/index.js @@ -5,169 +5,5 @@ export * from './keyed-each.js'; export * from './spread.js'; export * from './transitions.js'; export * from './utils.js'; - -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; -} - -export function destroyDev(detach) { - destroy.call(this, detach); - this.destroy = function() { - console.warn('Component was already destroyed'); - }; -} - -export function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} - -export function _differsImmutable(a, b) { - return a != a ? b == b : a !== b; -} - -export function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; - - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; - - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } - } -} - -export function get() { - return this._state; -} - -export function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; - - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} - -export function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); - - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} - -export function run(fn) { - fn(); -} - -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; -} - -export function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; - - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; - } - 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 }); - } -} - -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.' - ); - } - - this._checkReadOnly(newState); - set.call(this, newState); -} - -export function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} - -export function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); -} - -export function _unmount() { - if (this._fragment) this._fragment.u(); -} - -export function isPromise(value) { - return value && typeof value.then === 'function'; -} - -export var PENDING = {}; -export var SUCCESS = {}; -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 -}; +export * from './Base.js'; +export * from './Component.js'; \ No newline at end of file diff --git a/src/shared/utils.js b/src/shared/utils.js index c9a0f83449fc..b7496392d048 100644 --- a/src/shared/utils.js +++ b/src/shared/utils.js @@ -4,3 +4,27 @@ export function assign(tar, src) { for (var k in src) tar[k] = src[k]; return tar; } + +export function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +export function run(fn) { + fn(); +} + +export function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + +export function isPromise(value) { + return value && typeof value.then === 'function'; +} + +export var PENDING = {}; +export var SUCCESS = {}; +export var FAILURE = {}; + +export function removeFromStore() { + this.store._remove(this); +} \ No newline at end of file diff --git a/store.js b/store.js index 42f17c9bb87b..0b40ea2c9221 100644 --- a/store.js +++ b/store.js @@ -1,81 +1,24 @@ import { + Base, assign, blankObject, - _differs, - _differsImmutable, - get, - on, - fire + _differsImmutable } from './shared.js'; -function Store(state, options) { - this._handlers = {}; - this._dependents = []; +class Store extends Base { + 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/custom-elements/index.js b/test/custom-elements/index.js index 7675e0c188b6..52d4b7da59b8 100644 --- a/test/custom-elements/index.js +++ b/test/custom-elements/index.js @@ -23,6 +23,10 @@ describe('custom-elements', function() { console[type](...args); }); + nightmare.on('page', (type, message, stack) => { + console.error(stack); + }); + let svelte; let server; let bundle; diff --git a/test/js/samples/action/expected-bundle.js b/test/js/samples/action/expected-bundle.js index 093fb642a768..140dfd571492 100644 --- a/test/js/samples/action/expected-bundle.js +++ b/test/js/samples/action/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -21,117 +29,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function link(node) { @@ -174,24 +189,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 5562b845296b..e2776ce9411b 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 6786d1c2dcff..bea489055f21 100644 --- a/test/js/samples/collapses-text-around-comments/expected-bundle.js +++ b/test/js/samples/collapses-text-around-comments/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function appendNode(node, target) { target.appendChild(node); } @@ -29,117 +37,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function data() { @@ -185,20 +200,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 2f50f85a68b5..90c02f30b1a8 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 235f57755d3a..b5cc5a367c14 100644 --- a/test/js/samples/component-static-immutable/expected-bundle.js +++ b/test/js/samples/component-static-immutable/expected-bundle.js @@ -5,125 +5,136 @@ function assign(tar, src) { return tar; } -function blankObject() { - return Object.create(null); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -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 callAll(fns) { + while (fns && fns.length) fns.shift()(); } -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +function blankObject() { + return Object.create(null); } -function _differsImmutable(a, b) { - return a != a ? b == b : a !== b; -} +class Base { + 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 Base { + constructor(options) { + super(); + this._init(options); + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -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 (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; + } + + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ var Nested = window.Nested; @@ -151,38 +162,32 @@ 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); - - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - this._aftercreate = []; - } +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); - 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 ee5a447d64ea..f25cd25c4cf5 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,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 = []; - 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 235f57755d3a..b5cc5a367c14 100644 --- a/test/js/samples/component-static-immutable2/expected-bundle.js +++ b/test/js/samples/component-static-immutable2/expected-bundle.js @@ -5,125 +5,136 @@ function assign(tar, src) { return tar; } -function blankObject() { - return Object.create(null); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -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 callAll(fns) { + while (fns && fns.length) fns.shift()(); } -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +function blankObject() { + return Object.create(null); } -function _differsImmutable(a, b) { - return a != a ? b == b : a !== b; -} +class Base { + 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 Base { + constructor(options) { + super(); + this._init(options); + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -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 (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; + } + + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ var Nested = window.Nested; @@ -151,38 +162,32 @@ 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); - - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - this._aftercreate = []; - } +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); - 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 ee5a447d64ea..f25cd25c4cf5 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,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 = []; - 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 dc4f6dbf0dd9..c85beee6a0d5 100644 --- a/test/js/samples/component-static/expected-bundle.js +++ b/test/js/samples/component-static/expected-bundle.js @@ -5,121 +5,136 @@ function assign(tar, src) { return tar; } -function blankObject() { - return Object.create(null); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -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 callAll(fns) { + while (fns && fns.length) fns.shift()(); } -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +function blankObject() { + return Object.create(null); } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; +class Base { + constructor() { + this._handlers = blankObject(); + } + + 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 Base { + constructor(options) { + super(); + this._init(options); + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -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 (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + 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; + } + + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ var Nested = window.Nested; @@ -147,36 +162,30 @@ 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); - - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - this._aftercreate = []; - } +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); - 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 93584db49e8a..d756f6fece69 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,23 @@ 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 = []; - } - - 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 8cd4bf0011db..635830d7b3c4 100644 --- a/test/js/samples/computed-collapsed-if/expected-bundle.js +++ b/test/js/samples/computed-collapsed-if/expected-bundle.js @@ -5,121 +5,136 @@ function assign(tar, src) { return tar; } -function blankObject() { - return Object.create(null); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -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 callAll(fns) { + while (fns && fns.length) fns.shift()(); } -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +function blankObject() { + return Object.create(null); } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; +class Base { + constructor() { + this._handlers = blankObject(); + } + + 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 Base { + constructor(options) { + super(); + this._init(options); + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -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 (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + 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; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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 (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 (!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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function a({ x }) { @@ -145,26 +160,26 @@ 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; - if (this._differs(state.b, (state.b = b(state)))) changed.b = true; + _recompute(changed, state) { + if (changed.x) { + if (this._differs(state.a, (state.a = a(state)))) changed.a = true; + if (this._differs(state.b, (state.b = b(state)))) changed.b = true; + } } -}; +} export default SvelteComponent; diff --git a/test/js/samples/computed-collapsed-if/expected.js b/test/js/samples/computed-collapsed-if/expected.js index f1228ca0b698..1c66ea443e1f 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,25 +24,25 @@ 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; - if (this._differs(state.b, (state.b = b(state)))) changed.b = true; + _recompute(changed, state) { + if (changed.x) { + if (this._differs(state.a, (state.a = a(state)))) changed.a = true; + if (this._differs(state.b, (state.b = b(state)))) changed.b = true; + } } } export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/css-media-query/expected-bundle.js b/test/js/samples/css-media-query/expected-bundle.js index 6b1455f9c6ae..7c31522a9e6c 100644 --- a/test/js/samples/css-media-query/expected-bundle.js +++ b/test/js/samples/css-media-query/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function appendNode(node, target) { target.appendChild(node); } @@ -25,117 +33,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function add_css() { @@ -172,20 +187,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 893af2303512..a7d97adcc9b4 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 c2f35f7b986e..c7c4b69fdd58 100644 --- a/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js +++ b/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -21,117 +29,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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 (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 (!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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -161,10 +176,11 @@ function create_main_fragment(component, state) { class SvelteComponent extends HTMLElement { constructor(options = {}) { super(); - init(this, options); + this._handlers = {}; + this._init.call(this, options); + this.attachShadow({ mode: 'open' }); this._state = assign({}, options.data); - this.attachShadow({ mode: 'open' }); this.shadowRoot.innerHTML = ``; this._fragment = create_main_fragment(this, this._state); @@ -184,8 +200,16 @@ class SvelteComponent extends HTMLElement { } } -customElements.define("custom-element", SvelteComponent); -assign(assign(SvelteComponent.prototype, proto), { +Object.getOwnPropertyNames(Component.prototype).forEach(name => { + SvelteComponent.prototype[name] = Component.prototype[name]; +}); + +assign(SvelteComponent.prototype, { + fire: Base.prototype.fire, + get: Base.prototype.get, + on: Base.prototype.on, + _differs: Base.prototype._differs, + _mount(target, anchor) { target.insertBefore(this, anchor); }, @@ -195,4 +219,6 @@ assign(assign(SvelteComponent.prototype, proto), { } }); +customElements.define("custom-element", SvelteComponent); + export default SvelteComponent; diff --git a/test/js/samples/css-shadow-dom-keyframes/expected.js b/test/js/samples/css-shadow-dom-keyframes/expected.js index eeac231db1bf..1e4678841223 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 { Base, Component, assign, createElement, detachNode, insertNode, noop } from "svelte/shared.js"; function create_main_fragment(component, state) { var div; @@ -28,10 +28,11 @@ function create_main_fragment(component, state) { class SvelteComponent extends HTMLElement { constructor(options = {}) { super(); - init(this, options); + this._handlers = {}; + this._init.call(this, options); + this.attachShadow({ mode: 'open' }); this._state = assign({}, options.data); - this.attachShadow({ mode: 'open' }); this.shadowRoot.innerHTML = ``; this._fragment = create_main_fragment(this, this._state); @@ -51,8 +52,16 @@ class SvelteComponent extends HTMLElement { } } -customElements.define("custom-element", SvelteComponent); -assign(assign(SvelteComponent.prototype, proto), { +Object.getOwnPropertyNames(Component.prototype).forEach(name => { + SvelteComponent.prototype[name] = Component.prototype[name]; +}); + +assign(SvelteComponent.prototype, { + fire: Base.prototype.fire, + get: Base.prototype.get, + on: Base.prototype.on, + _differs: Base.prototype._differs, + _mount(target, anchor) { target.insertBefore(this, anchor); }, @@ -61,4 +70,6 @@ assign(assign(SvelteComponent.prototype, proto), { this.parentNode.removeChild(this); } }); + +customElements.define("custom-element", SvelteComponent); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/deconflict-builtins/expected-bundle.js b/test/js/samples/deconflict-builtins/expected-bundle.js index 2f9dd2db1d49..2a03d99c1e14 100644 --- a/test/js/samples/deconflict-builtins/expected-bundle.js +++ b/test/js/samples/deconflict-builtins/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function appendNode(node, target) { target.appendChild(node); } @@ -39,117 +47,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -220,7 +235,7 @@ function create_main_fragment(component, state) { detachNode(each_anchor); }, - d: function destroy$$1() { + d: function destroy() { destroyEach(each_blocks); } }; @@ -259,18 +274,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 000000000000..f6861d255714 --- /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 f6861d255714..da60ff688c40 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 214eed957865..aa850ceb1d94 100644 --- a/test/js/samples/deconflict-globals/expected-bundle.js +++ b/test/js/samples/deconflict-globals/expected-bundle.js @@ -5,121 +5,136 @@ function assign(tar, src) { return tar; } -function blankObject() { - return Object.create(null); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -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 callAll(fns) { + while (fns && fns.length) fns.shift()(); } -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +function blankObject() { + return Object.create(null); } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; +class Base { + constructor() { + this._handlers = blankObject(); + } + + 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 Base { + constructor(options) { + super(); + this._init(options); + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -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 (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; + } -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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; - 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 }); + 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function data_1() { @@ -146,33 +161,26 @@ 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 }); - }; + this._fragment = create_main_fragment(this, this._state); - if (!options.root) { - this._oncreate = []; - } - - this._fragment = create_main_fragment(this, this._state); - - this.root._oncreate.push(_oncreate); + this.root._oncreate.push(() => { + const changed = { }; + oncreate.call(this); + this.fire("update", { changed, current: 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._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 91192f4ccaa3..9d785bbf50d6 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,25 @@ 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 }); - }; - - 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(() => { + const changed = { }; + oncreate.call(this); + this.fire("update", { changed, current: this._state }); + }); - 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 e0ff6a11717d..cac872e562fd 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 @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function appendNode(node, target) { target.appendChild(node); } @@ -29,135 +37,152 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; -function destroyDev(detach) { - destroy.call(this, detach); - this.destroy = function() { - console.warn('Component was already destroyed'); - }; -} + for (let i = 0; i < handlers.length; i += 1) { + const handler = handlers[i]; -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); -} + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); + 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; + } + + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; } - }; -} -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.refs = {}; + this.slots = {}; + } + + _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; -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 { + constructor(options) { + if (!options || (!options.target && !options.root)) { + throw new Error(`'target' is a required option`); + } + + super(options); } - this._checkReadOnly(newState); - set.call(this, newState); -} + destroy(detach) { + super.destroy(detach); + this.destroy = () => { + console.warn('Component was already destroyed'); + }; + } -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + set(newState) { + if (typeof newState !== 'object') { + throw new Error(`${this._debugName}.set was called without an object of data key-values to update.`); + } -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); -} + this._checkReadOnly(newState); + super.set(newState); + } -function _unmount() { - if (this._fragment) this._fragment.u(); + _checkReadOnly() {} } -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,33 +225,33 @@ 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 = ''; + 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'"); -}; + _recompute(changed, state) { + if (changed.foo) { + if (this._differs(state.bar, (state.bar = bar(state)))) changed.bar = true; + } + } -SvelteComponent.prototype._recompute = function _recompute(changed, state) { - if (changed.foo) { - if (this._differs(state.bar, (state.bar = bar(state)))) changed.bar = true; + _checkReadOnly(newState) { + if (this._updatingReadonlyProperty) return; + if ('bar' in newState) throw new Error(": Cannot set read-only property 'bar'"); } -}; +} export default SvelteComponent; 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 52dee37250c4..99d6d4c43e6c 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,32 +41,32 @@ 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 = ''; + 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'"); -}; + _recompute(changed, state) { + if (changed.foo) { + if (this._differs(state.bar, (state.bar = bar(state)))) changed.bar = true; + } + } -SvelteComponent.prototype._recompute = function _recompute(changed, state) { - if (changed.foo) { - if (this._differs(state.bar, (state.bar = bar(state)))) changed.bar = true; + _checkReadOnly(newState) { + if (this._updatingReadonlyProperty) return; + if ('bar' in newState) throw new Error(": Cannot set read-only property 'bar'"); } } export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/do-use-dataset/expected-bundle.js b/test/js/samples/do-use-dataset/expected-bundle.js index 32af7494c3df..79902546677c 100644 --- a/test/js/samples/do-use-dataset/expected-bundle.js +++ b/test/js/samples/do-use-dataset/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -25,117 +33,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -176,18 +191,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 000000000000..b88636a606d5 --- /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 b88636a606d5..accd7a47e813 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 5e9def191391..af509392ad1b 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 @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -29,117 +37,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -180,18 +195,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 000000000000..699da1270b87 --- /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 699da1270b87..d4fa1c0fede7 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 54b8382a2db9..a0d7c7967b21 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 @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function appendNode(node, target) { target.appendChild(node); } @@ -29,117 +37,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -178,18 +193,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 000000000000..42693672549b --- /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 42693672549b..beb4c48d27fc 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 89c3cab24320..01edc06eb947 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function appendNode(node, target) { target.appendChild(node); } @@ -41,117 +49,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -231,7 +246,7 @@ function create_main_fragment(component, state) { detachNode(p); }, - d: function destroy$$1() { + d: function destroy() { destroyEach(each_blocks); } }; @@ -306,18 +321,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 000000000000..e7e0ccca306f --- /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 e7e0ccca306f..a677d72d27dd 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 5d85e177e1b4..f3e877aabb9c 100644 --- a/test/js/samples/event-handlers-custom/expected-bundle.js +++ b/test/js/samples/event-handlers-custom/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -21,128 +29,129 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + this.refs = {}; + this.slots = {}; } - 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; - 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 }); + 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function foo( node, callback ) { // code goes here } -var methods = { - foo ( bar ) { - console.log( bar ); - } -}; - function create_main_fragment(component, state) { var button, foo_handler; @@ -170,25 +179,28 @@ 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); + foo( bar ) { + console.log( bar ); + } +} export default SvelteComponent; diff --git a/test/js/samples/event-handlers-custom/expected.js b/test/js/samples/event-handlers-custom/expected.js index 3975641534d1..e0b2a67cc503 100644 --- a/test/js/samples/event-handlers-custom/expected.js +++ b/test/js/samples/event-handlers-custom/expected.js @@ -1,16 +1,10 @@ /* 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 }; -var methods = { - foo ( bar ) { - console.log( bar ); - } -}; - function create_main_fragment(component, state) { var button, foo_handler; @@ -44,18 +38,21 @@ 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); + foo( bar ) { + console.log( bar ); + } +} 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 7f88574b70f6..c4e58f6211ed 100644 --- a/test/js/samples/head-no-whitespace/expected-bundle.js +++ b/test/js/samples/head-no-whitespace/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function appendNode(node, target) { target.appendChild(node); } @@ -21,117 +29,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -167,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/head-no-whitespace/expected-v2.js b/test/js/samples/head-no-whitespace/expected-v2.js new file mode 100644 index 000000000000..9818f4211658 --- /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 9818f4211658..a2dbece70c17 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 1760a2ca607f..720966d58bef 100644 --- a/test/js/samples/if-block-no-update/expected-bundle.js +++ b/test/js/samples/if-block-no-update/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -25,117 +33,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -175,7 +190,7 @@ function create_main_fragment(component, state) { detachNode(if_block_anchor); }, - d: function destroy$$1() { + d: function destroy() { if_block.d(); } }; @@ -225,18 +240,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 000000000000..c06ff6ee679a --- /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 c06ff6ee679a..9c3faef253fd 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 f71aafff9415..c1075003bfb8 100644 --- a/test/js/samples/if-block-simple/expected-bundle.js +++ b/test/js/samples/if-block-simple/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -25,117 +33,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -173,7 +188,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 +216,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 000000000000..b3447471d31e --- /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 b3447471d31e..e5e83f73661c 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 bd3bacfac27a..8837d9d847a4 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -25,117 +33,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -174,18 +189,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 000000000000..7a94993b690c --- /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 7a94993b690c..9a7ec51e84fe 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 411990568209..72228302ede2 100644 --- a/test/js/samples/inline-style-optimized-url/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-url/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -25,117 +33,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -169,18 +184,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 000000000000..5a27ecfa4948 --- /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 5a27ecfa4948..c8bdab407851 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 7643ee066d12..43b50774d474 100644 --- a/test/js/samples/inline-style-optimized/expected-bundle.js +++ b/test/js/samples/inline-style-optimized/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -25,117 +33,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -169,18 +184,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 000000000000..86dabe8d21e1 --- /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 86dabe8d21e1..444f3307d80a 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 67b27347432a..0856f73e2aca 100644 --- a/test/js/samples/inline-style-unoptimized/expected-bundle.js +++ b/test/js/samples/inline-style-unoptimized/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -25,117 +33,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -180,18 +195,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 000000000000..7d7e933c1226 --- /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 7d7e933c1226..325d4e1e6c59 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 e881aa6de460..b8af16c6d289 100644 --- a/test/js/samples/input-without-blowback-guard/expected-bundle.js +++ b/test/js/samples/input-without-blowback-guard/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -33,117 +41,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -178,24 +193,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 ba99db454176..304614a6899f 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 1119454b3db6..d864a7117305 100644 --- a/test/js/samples/legacy-input-type/expected-bundle.js +++ b/test/js/samples/legacy-input-type/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -27,117 +35,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -167,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/legacy-input-type/expected.js b/test/js/samples/legacy-input-type/expected.js index 2426c064817c..2b4bcb75c433 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 abcdd409862d..f9c3a82f1f47 100644 --- a/test/js/samples/media-bindings/expected-bundle.js +++ b/test/js/samples/media-bindings/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -37,117 +45,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -221,7 +236,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 +248,20 @@ function create_main_fragment(component, state) { }; } -function SvelteComponent(options) { - init(this, options); - this._state = assign({}, options.data); - - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - } +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); - 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 a395dd4385eb..9f8a7f969fee 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,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 (!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/method-arrow-function/expected-bundle.js b/test/js/samples/method-arrow-function/expected-bundle.js new file mode 100644 index 000000000000..204f0f8892df --- /dev/null +++ b/test/js/samples/method-arrow-function/expected-bundle.js @@ -0,0 +1,179 @@ +function noop() {} + +function assign(tar, src) { + for (var k in src) tar[k] = src[k]; + return tar; +} + +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + +function blankObject() { + return Object.create(null); +} + +class Base { + constructor() { + this._handlers = blankObject(); + } + + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; + + 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; + } + } + } + + get() { + return this._state; + } + + 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); + } + }; + } + + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } +} + +class Component extends Base { + constructor(options) { + super(); + this._init(options); + } + + 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; + } + + 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; + } + + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; + } + + _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, 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); + } + + _recompute() {} + + _unmount() { + if (this._fragment) this._fragment.u(); + } +} + +/* generated by Svelte vX.Y.Z */ + +function create_main_fragment(component, state) { + + return { + c: noop, + + m: noop, + + p: noop, + + u: noop, + + d: noop + }; +} + +class SvelteComponent extends Component { + constructor(options) { + super(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); + } + } + + foo() { return console.log('foo'); } + + bar(x) { return console.log(x); } + + baz(x) { return console.log(x); } + + qux() { + return 42; + } +} + +export default SvelteComponent; diff --git a/test/js/samples/method-arrow-function/expected.js b/test/js/samples/method-arrow-function/expected.js new file mode 100644 index 000000000000..d0b138d8d314 --- /dev/null +++ b/test/js/samples/method-arrow-function/expected.js @@ -0,0 +1,42 @@ +/* generated by Svelte vX.Y.Z */ +import { Component, assign, noop } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + + return { + c: noop, + + m: noop, + + p: noop, + + u: noop, + + d: noop + }; +} + +class SvelteComponent extends Component { + constructor(options) { + super(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); + } + } + + foo() { return console.log('foo'); } + + bar(x) { return console.log(x); } + + baz(x) { return console.log(x); } + + qux() { + return 42; + } +} +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/method-arrow-function/input.html b/test/js/samples/method-arrow-function/input.html new file mode 100644 index 000000000000..423eda40fc50 --- /dev/null +++ b/test/js/samples/method-arrow-function/input.html @@ -0,0 +1,12 @@ + \ No newline at end of file diff --git a/test/js/samples/method-async/expected-bundle.js b/test/js/samples/method-async/expected-bundle.js new file mode 100644 index 000000000000..b0dc283fa1c0 --- /dev/null +++ b/test/js/samples/method-async/expected-bundle.js @@ -0,0 +1,173 @@ +function noop() {} + +function assign(tar, src) { + for (var k in src) tar[k] = src[k]; + return tar; +} + +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + +function blankObject() { + return Object.create(null); +} + +class Base { + constructor() { + this._handlers = blankObject(); + } + + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; + + 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; + } + } + } + + get() { + return this._state; + } + + 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); + } + }; + } + + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } +} + +class Component extends Base { + constructor(options) { + super(); + this._init(options); + } + + 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; + } + + 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; + } + + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; + } + + _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, 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); + } + + _recompute() {} + + _unmount() { + if (this._fragment) this._fragment.u(); + } +} + +/* generated by Svelte vX.Y.Z */ + +function create_main_fragment(component, state) { + + return { + c: noop, + + m: noop, + + p: noop, + + u: noop, + + d: noop + }; +} + +class SvelteComponent extends Component { + constructor(options) { + super(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); + } + } + + async foo() {} + + *bar() {} +} + +export default SvelteComponent; diff --git a/test/js/samples/method-async/expected.js b/test/js/samples/method-async/expected.js new file mode 100644 index 000000000000..3ec7ae454f7d --- /dev/null +++ b/test/js/samples/method-async/expected.js @@ -0,0 +1,36 @@ +/* generated by Svelte vX.Y.Z */ +import { Component, assign, noop } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + + return { + c: noop, + + m: noop, + + p: noop, + + u: noop, + + d: noop + }; +} + +class SvelteComponent extends Component { + constructor(options) { + super(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); + } + } + + async foo() {} + + *bar() {} +} +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/method-async/input.html b/test/js/samples/method-async/input.html new file mode 100644 index 000000000000..4774378a0228 --- /dev/null +++ b/test/js/samples/method-async/input.html @@ -0,0 +1,8 @@ + \ No newline at end of file diff --git a/test/js/samples/method-function-keyword/expected-bundle.js b/test/js/samples/method-function-keyword/expected-bundle.js new file mode 100644 index 000000000000..2115cc6b867e --- /dev/null +++ b/test/js/samples/method-function-keyword/expected-bundle.js @@ -0,0 +1,173 @@ +function noop() {} + +function assign(tar, src) { + for (var k in src) tar[k] = src[k]; + return tar; +} + +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + +function blankObject() { + return Object.create(null); +} + +class Base { + constructor() { + this._handlers = blankObject(); + } + + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; + + 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; + } + } + } + + get() { + return this._state; + } + + 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); + } + }; + } + + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } +} + +class Component extends Base { + constructor(options) { + super(); + this._init(options); + } + + 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; + } + + 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; + } + + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; + } + + _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, 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); + } + + _recompute() {} + + _unmount() { + if (this._fragment) this._fragment.u(); + } +} + +/* generated by Svelte vX.Y.Z */ + +function create_main_fragment(component, state) { + + return { + c: noop, + + m: noop, + + p: noop, + + u: noop, + + d: noop + }; +} + +class SvelteComponent extends Component { + constructor(options) { + super(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); + } + } + + foo() { + console.log('foo'); + } +} + +export default SvelteComponent; diff --git a/test/js/samples/method-function-keyword/expected.js b/test/js/samples/method-function-keyword/expected.js new file mode 100644 index 000000000000..8c4f9825816b --- /dev/null +++ b/test/js/samples/method-function-keyword/expected.js @@ -0,0 +1,36 @@ +/* generated by Svelte vX.Y.Z */ +import { Component, assign, noop } from "svelte/shared.js"; + +function create_main_fragment(component, state) { + + return { + c: noop, + + m: noop, + + p: noop, + + u: noop, + + d: noop + }; +} + +class SvelteComponent extends Component { + constructor(options) { + super(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); + } + } + + foo() { + console.log('foo'); + } +} +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/method-function-keyword/input.html b/test/js/samples/method-function-keyword/input.html new file mode 100644 index 000000000000..0eee56773d57 --- /dev/null +++ b/test/js/samples/method-function-keyword/input.html @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/test/js/samples/method-non-function-expression/expected-bundle.js b/test/js/samples/method-non-function-expression/expected-bundle.js new file mode 100644 index 000000000000..91c7b6d525ec --- /dev/null +++ b/test/js/samples/method-non-function-expression/expected-bundle.js @@ -0,0 +1,185 @@ +function noop() {} + +function assign(tar, src) { + for (var k in src) tar[k] = src[k]; + return tar; +} + +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + +function blankObject() { + return Object.create(null); +} + +class Base { + constructor() { + this._handlers = blankObject(); + } + + fire(eventName, data) { + const handlers = eventName in this._handlers && this._handlers[eventName].slice(); + if (!handlers) return; + + 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; + } + } + } + + get() { + return this._state; + } + + 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); + } + }; + } + + _differs(a, b) { + return _differsImmutable(a, b) || ((a && typeof a === 'object') || typeof a === 'function'); + } +} + +class Component extends Base { + constructor(options) { + super(); + this._init(options); + } + + 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; + } + + 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; + } + + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; + } + + _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, 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); + } + + _recompute() {} + + _unmount() { + if (this._fragment) this._fragment.u(); + } +} + +/* generated by Svelte vX.Y.Z */ + +function foo() { + console.log('foo'); +} + +function bar() { + console.log('bar'); +} + + + +function create_main_fragment(component, state) { + + return { + c: noop, + + m: noop, + + p: noop, + + u: noop, + + d: noop + }; +} + +class SvelteComponent extends Component { + constructor(options) { + super(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, { + foo, + + bar: [bar][0] +}); + +export default SvelteComponent; diff --git a/test/js/samples/method-non-function-expression/expected.js b/test/js/samples/method-non-function-expression/expected.js new file mode 100644 index 000000000000..6f2630061dfe --- /dev/null +++ b/test/js/samples/method-non-function-expression/expected.js @@ -0,0 +1,48 @@ +/* generated by Svelte vX.Y.Z */ +import { Component, assign, noop } from "svelte/shared.js"; + +function foo() { + console.log('foo'); +} + +function bar() { + console.log('bar'); +} + + + +function create_main_fragment(component, state) { + + return { + c: noop, + + m: noop, + + p: noop, + + u: noop, + + d: noop + }; +} + +class SvelteComponent extends Component { + constructor(options) { + super(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, { + foo, + + bar: [bar][0] +}); +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/method-non-function-expression/input.html b/test/js/samples/method-non-function-expression/input.html new file mode 100644 index 000000000000..90bbd192931f --- /dev/null +++ b/test/js/samples/method-non-function-expression/input.html @@ -0,0 +1,16 @@ + \ 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 b4adec303a0a..f38cc96cfc73 100644 --- a/test/js/samples/non-imported-component/expected-bundle.js +++ b/test/js/samples/non-imported-component/expected-bundle.js @@ -7,6 +7,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function insertNode(node, target, anchor) { target.insertBefore(node, anchor); } @@ -23,117 +31,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ @@ -170,37 +185,31 @@ 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); - - if (!options.root) { - this._oncreate = []; - this._beforecreate = []; - this._aftercreate = []; - } +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); - 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 c1637b7cc486..4ddb5d64f467 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,23 @@ 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 = []; - } - - 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 e1aaa9ae759a..3c696b564960 100644 --- a/test/js/samples/setup-method/expected-bundle.js +++ b/test/js/samples/setup-method/expected-bundle.js @@ -5,137 +5,146 @@ function assign(tar, src) { return tar; } -function blankObject() { - return Object.create(null); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -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 callAll(fns) { + while (fns && fns.length) fns.shift()(); } -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +function blankObject() { + return Object.create(null); } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; +class Base { + constructor() { + this._handlers = blankObject(); + } + + 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; -} -function init(component, options) { - component._handlers = blankObject(); - component._bind = options._bind; + get() { + return this._state; + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + return { + cancel: function() { + const index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; + } - return { - cancel: function() { - var 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 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; -} +class Component extends Base { + constructor(options) { + super(); + this._init(options); + } -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; } - if (!dirty) return; - this._state = assign(assign({}, oldState), newState); - this._recompute(changed, this._state); - if (this._bind) this._bind(changed, this._state); + 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; + } + + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - 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 (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } -} -function callAll(fns) { - while (fns && fns.length) fns.shift()(); -} + _set(newState) { + const previous = this._state; + const changed = {}; + let dirty = false; -function _mount(target, anchor) { - this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); -} + for (var key in newState) { + if (this._differs(newState[key], previous[key])) changed[key] = dirty = 1; + } -function _unmount() { - if (this._fragment) this._fragment.u(); -} + if (!dirty) return; -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; + this._state = assign(assign({}, previous), newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); -/* generated by Svelte vX.Y.Z */ + 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 }); + } + } -var methods = { - foo ( bar ) { - console.log( bar ); + _mount(target, anchor) { + this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); + } + + _recompute() {} + + _unmount() { + if (this._fragment) this._fragment.u(); } -}; +} + +/* generated by Svelte vX.Y.Z */ -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,20 +162,23 @@ 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); + foo( bar ) { + console.log( bar ); + } +} setup(SvelteComponent); diff --git a/test/js/samples/setup-method/expected.js b/test/js/samples/setup-method/expected.js index d39c4288e377..2aedc961330b 100644 --- a/test/js/samples/setup-method/expected.js +++ b/test/js/samples/setup-method/expected.js @@ -1,11 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, init, noop, proto } from "svelte/shared.js"; - -var methods = { - foo ( bar ) { - console.log( bar ); - } -}; +import { Component, assign, noop } from "svelte/shared.js"; function setup(Component) { Component.SOME_CONSTANT = 42; @@ -32,20 +26,23 @@ 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); + foo( bar ) { + console.log( bar ); + } +} setup(SvelteComponent); export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/svg-title/expected-bundle.js b/test/js/samples/svg-title/expected-bundle.js index 477d97c83947..2f6a4423b34b 100644 --- a/test/js/samples/svg-title/expected-bundle.js +++ b/test/js/samples/svg-title/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function appendNode(node, target) { target.appendChild(node); } @@ -29,117 +37,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; + + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -168,18 +183,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 3f7ea0b8e9c5..592aa300b111 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 27cae18b3d14..a7458a2cd3e1 100644 --- a/test/js/samples/title/expected-bundle.js +++ b/test/js/samples/title/expected-bundle.js @@ -5,121 +5,136 @@ function assign(tar, src) { return tar; } -function blankObject() { - return Object.create(null); +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } -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 callAll(fns) { + while (fns && fns.length) fns.shift()(); } -function _differs(a, b) { - return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +function blankObject() { + return Object.create(null); } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; +class Base { + constructor() { + this._handlers = blankObject(); + } + + 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 Base { + constructor(options) { + super(); + this._init(options); + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -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 (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; + } + + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -144,18 +159,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 000000000000..0e059bbb4c67 --- /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 0e059bbb4c67..db4821063a94 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 1fda063de122..40751cfc8ff5 100644 --- a/test/js/samples/use-elements-as-anchors/expected-bundle.js +++ b/test/js/samples/use-elements-as-anchors/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function appendNode(node, target) { target.appendChild(node); } @@ -33,117 +41,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -271,7 +286,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 +406,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 000000000000..d9cff08d3f98 --- /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 d9cff08d3f98..08c16e2a1e18 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 df4fc6b2c981..dc5f54aa3501 100644 --- a/test/js/samples/window-binding-scroll/expected-bundle.js +++ b/test/js/samples/window-binding-scroll/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + function appendNode(node, target) { target.appendChild(node); } @@ -29,117 +37,124 @@ function blankObject() { return Object.create(null); } -function destroy(detach) { - this.destroy = noop; - this.fire('destroy'); - this.set = this.get = noop; +class Base { + constructor() { + this._handlers = blankObject(); + } - if (detach !== false) this._fragment.u(); - this._fragment.d(); - this._fragment = this._state = null; -} + 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]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } + } + } -function fire(eventName, data) { - var handlers = - eventName in this._handlers && this._handlers[eventName].slice(); - if (!handlers) return; + get() { + return this._state; + } - for (var i = 0; i < handlers.length; i += 1) { - var handler = handlers[i]; + on(eventName, handler) { + const handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); - if (!handler.__calling) { - handler.__calling = true; - handler.call(this, data); - handler.__calling = false; - } + 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 Base { + constructor(options) { + super(); + this._init(options); + } - component.options = options; - component.root = options.root || component; - component.store = component.root.store || options.store; -} + destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; -function on(eventName, handler) { - var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); - handlers.push(handler); + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; + } - return { - cancel: function() { - var index = handlers.indexOf(handler); - if (~index) handlers.splice(index, 1); - } - }; -} + 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; + } -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; -} + _init(options) { + this._bind = options._bind; + this._slotted = options.slots || {}; -function _set(newState) { - var oldState = this._state, - changed = {}, - dirty = false; + this.options = options; + this.root = options.root || this; + this.store = this.root.store || options.store; - for (var key in newState) { - if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this.refs = {}; + this.slots = {}; } - 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() {} -function _unmount() { - if (this._fragment) this._fragment.u(); + _unmount() { + if (this._fragment) this._fragment.u(); + } } -var proto = { - destroy, - get, - fire, - on, - set, - _recompute: noop, - _set, - _mount, - _unmount, - _differs -}; - /* generated by Svelte vX.Y.Z */ function create_main_fragment(component, state) { @@ -188,25 +203,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 000000000000..59009aeb9b99 --- /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 d18b35687ed9..0dd9f98cc505 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 diff --git a/test/runtime/index.js b/test/runtime/index.js index 199a9a0c9ba0..52edb6f42284 100644 --- a/test/runtime/index.js +++ b/test/runtime/index.js @@ -110,12 +110,7 @@ describe("runtime", () => { }; }; - try { - SvelteComponent = require(`./samples/${dir}/main.html`); - } catch (err) { - showOutput(cwd, { shared, format: 'cjs', hydratable: hydrate, store: !!compileOptions.store }, compile); // eslint-disable-line no-console - throw err; - } + SvelteComponent = require(`./samples/${dir}/main.html`); global.window = window;