diff --git a/.gitignore b/.gitignore index 3d1322c33e89..35888493fa95 100644 --- a/.gitignore +++ b/.gitignore @@ -4,14 +4,14 @@ node_modules *.map /src/compile/internal-exports.ts -/compiler.js -/index.js -/internal.* -/store.* -/easing.js -/motion.* -/transition.js -/animate.js +/compiler.*js +/index.*js +/internal.*js +/store.*js +/easing.*js +/motion.*js +/transition.*js +/animate.*js /scratch/ /coverage/ /coverage.lcov/ @@ -21,6 +21,7 @@ node_modules /test/sourcemaps/samples/*/output.css.map /yarn-error.log _actual*.* +/dist /site/cypress/screenshots/ /site/__sapper__/ diff --git a/animate.d.ts b/animate.d.ts new file mode 100644 index 000000000000..3284bfd8c09e --- /dev/null +++ b/animate.d.ts @@ -0,0 +1 @@ +export * from './dist/animate'; diff --git a/compiler.d.ts b/compiler.d.ts new file mode 100644 index 000000000000..977efefb6d7d --- /dev/null +++ b/compiler.d.ts @@ -0,0 +1 @@ +export * from './dist/compiler'; diff --git a/easing.d.ts b/easing.d.ts new file mode 100644 index 000000000000..c07764f4f04b --- /dev/null +++ b/easing.d.ts @@ -0,0 +1 @@ +export * from './dist/easing'; diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 000000000000..e4ddc9027ee3 --- /dev/null +++ b/index.d.ts @@ -0,0 +1 @@ +export * from './dist/index'; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index ee5b575171e3..000000000000 --- a/index.mjs +++ /dev/null @@ -1,10 +0,0 @@ -export { - onMount, - onDestroy, - beforeUpdate, - afterUpdate, - setContext, - getContext, - tick, - createEventDispatcher -} from './internal'; diff --git a/internal.d.ts b/internal.d.ts new file mode 100644 index 000000000000..be034cd88a81 --- /dev/null +++ b/internal.d.ts @@ -0,0 +1 @@ +export * from './dist/internal'; diff --git a/motion.d.ts b/motion.d.ts new file mode 100644 index 000000000000..2fdaa86c4e7b --- /dev/null +++ b/motion.d.ts @@ -0,0 +1 @@ +export * from './dist/motion'; diff --git a/package.json b/package.json index b32e19786fd3..7963e2178dca 100644 --- a/package.json +++ b/package.json @@ -30,10 +30,10 @@ "prepare": "npm run build && npm run tsd", "dev": "rollup -cw", "pretest": "npm run build", - "posttest": "agadoo src/internal/index.js", + "posttest": "agadoo internal.mjs", "prepublishOnly": "export PUBLISH=true && npm run lint && npm test", - "tsd": "tsc -d src/store.ts --outDir .", - "typecheck": "tsc --noEmit" + "tsd": "tsc -p . --emitDeclarationOnly", + "typecheck": "tsc -p . --noEmit" }, "repository": { "type": "git", diff --git a/rollup.config.js b/rollup.config.js index 0d19e59d4ade..9fd49f3e8f97 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -9,10 +9,19 @@ import pkg from './package.json'; const is_publish = !!process.env.PUBLISH; +const tsPlugin = is_publish + ? typescript({ + include: 'src/**', + typescript: require('typescript') + }) + : sucrase({ + transforms: ['typescript'] + }); + export default [ /* internal.[m]js */ { - input: `src/internal/index.js`, + input: `src/internal/index.ts`, output: [ { file: `internal.mjs`, @@ -26,19 +35,22 @@ export default [ } ], external: id => id.startsWith('svelte/'), - plugins: [{ - generateBundle(options, bundle) { - const mod = bundle['internal.mjs']; - if (mod) { - fs.writeFileSync('src/compile/internal-exports.ts', `// This file is automatically generated\nexport default new Set(${JSON.stringify(mod.exports)});`); + + plugins: [ + tsPlugin, + { + generateBundle(options, bundle) { + const mod = bundle['internal.mjs']; + if (mod) { + fs.writeFileSync('src/compile/internal-exports.ts', `// This file is automatically generated\nexport default new Set(${JSON.stringify(mod.exports)});`); + } } - } - }] + }] }, /* compiler.js */ { - input: 'src/index.ts', + input: 'src/compiler.ts', plugins: [ replace({ __VERSION__: pkg.version @@ -48,15 +60,7 @@ export default [ include: ['node_modules/**'] }), json(), - is_publish - ? typescript({ - include: 'src/**', - exclude: 'src/internal/**', - typescript: require('typescript') - }) - : sucrase({ - transforms: ['typescript'] - }) + tsPlugin ], output: { file: 'compiler.js', @@ -71,7 +75,7 @@ export default [ /* motion.mjs */ { - input: `src/motion/index.js`, + input: `src/motion/index.ts`, output: [ { file: `motion.mjs`, @@ -84,46 +88,30 @@ export default [ paths: id => id.startsWith('svelte/') && id.replace('svelte', '.') } ], + plugins: [ + tsPlugin + ], external: id => id.startsWith('svelte/') }, - /* store.mjs */ - { - input: `src/store.ts`, + // everything else + ...['index', 'easing', 'transition', 'animate', 'store'].map(name => ({ + input: `src/${name}.ts`, output: [ { - file: `store.mjs`, + file: `${name}.mjs`, format: 'esm', paths: id => id.startsWith('svelte/') && id.replace('svelte', '.') }, { - file: `store.js`, + file: `${name}.js`, format: 'cjs', paths: id => id.startsWith('svelte/') && id.replace('svelte', '.') } ], plugins: [ - is_publish - ? typescript({ - include: 'src/**', - exclude: 'src/internal/**', - typescript: require('typescript') - }) - : sucrase({ - transforms: ['typescript'] - }) + tsPlugin ], external: id => id.startsWith('svelte/') - }, - - // everything else - ...['index', 'easing', 'transition', 'animate'].map(name => ({ - input: `${name}.mjs`, - output: { - file: `${name}.js`, - format: 'cjs', - paths: id => id.startsWith('svelte/') && id.replace('svelte', '.') - }, - external: id => id !== `${name}.mjs` })) ]; diff --git a/animate.mjs b/src/animate.ts similarity index 86% rename from animate.mjs rename to src/animate.ts index f22fabe401c9..cf64cd060a13 100644 --- a/animate.mjs +++ b/src/animate.ts @@ -1,5 +1,5 @@ -import { cubicOut } from './easing'; -import { is_function } from './internal'; +import { cubicOut } from 'svelte/easing'; +import { is_function } from 'svelte/internal'; export function flip(node, animation, params) { const style = getComputedStyle(node); @@ -22,4 +22,4 @@ export function flip(node, animation, params) { easing, css: (t, u) => `transform: ${transform} translate(${u * dx}px, ${u * dy}px);` }; -} \ No newline at end of file +} diff --git a/src/compile/index.ts b/src/compile/index.ts index 526d8abaeb2a..dac75f23e0e5 100644 --- a/src/compile/index.ts +++ b/src/compile/index.ts @@ -1,4 +1,4 @@ -import { assign } from '../internal'; +import { assign } from '../internal/index'; import Stats from '../Stats'; import parse from '../parse/index'; import render_dom from './render-dom/index'; diff --git a/src/compile/render-dom/wrappers/RawMustacheTag.ts b/src/compile/render-dom/wrappers/RawMustacheTag.ts index d8f863c67433..326852bb1865 100644 --- a/src/compile/render-dom/wrappers/RawMustacheTag.ts +++ b/src/compile/render-dom/wrappers/RawMustacheTag.ts @@ -1,7 +1,7 @@ import Renderer from '../Renderer'; import Block from '../Block'; import Tag from './shared/Tag'; -import Wrapper from './shared/wrapper'; +import Wrapper from './shared/Wrapper'; import deindent from '../../utils/deindent'; import MustacheTag from '../../nodes/MustacheTag'; import RawMustacheTag from '../../nodes/RawMustacheTag'; diff --git a/src/compiler.ts b/src/compiler.ts new file mode 100644 index 000000000000..4987c18a15be --- /dev/null +++ b/src/compiler.ts @@ -0,0 +1,6 @@ +export { default as compile } from './compile/index'; +export { default as parse } from './parse/index'; +export { default as preprocess } from './preprocess/index'; +export { walk } from 'estree-walker'; + +export const VERSION = '__VERSION__'; \ No newline at end of file diff --git a/easing.mjs b/src/easing.ts similarity index 98% rename from easing.mjs rename to src/easing.ts index 4fc625c24f97..7a762394c2ad 100644 --- a/easing.mjs +++ b/src/easing.ts @@ -3,7 +3,7 @@ Adapted from https://github.com/mattdesl Distributed under MIT License https://github.com/mattdesl/eases/blob/master/LICENSE.md */ -export { identity as linear } from './internal'; +export { identity as linear } from 'svelte/internal'; export function backInOut(t) { const s = 1.70158 * 1.525; diff --git a/src/index.ts b/src/index.ts index 4987c18a15be..40928da8a0eb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,10 @@ -export { default as compile } from './compile/index'; -export { default as parse } from './parse/index'; -export { default as preprocess } from './preprocess/index'; -export { walk } from 'estree-walker'; - -export const VERSION = '__VERSION__'; \ No newline at end of file +export { + onMount, + onDestroy, + beforeUpdate, + afterUpdate, + setContext, + getContext, + tick, + createEventDispatcher +} from 'svelte/internal'; diff --git a/src/internal/Component.js b/src/internal/Component.ts similarity index 89% rename from src/internal/Component.js rename to src/internal/Component.ts index 871dbd6054de..714d15df343e 100644 --- a/src/internal/Component.js +++ b/src/internal/Component.ts @@ -1,7 +1,23 @@ -import { add_render_callback, flush, schedule_update, dirty_components } from './scheduler.js'; -import { current_component, set_current_component } from './lifecycle.js'; -import { blank_object, is_function, run, run_all, noop } from './utils.js'; -import { children } from './dom.js'; +import { add_render_callback, flush, schedule_update, dirty_components } from './scheduler'; +import { current_component, set_current_component } from './lifecycle'; +import { blank_object, is_function, run, run_all, noop } from './utils'; +import { children } from './dom'; + +interface T$$ { + dirty: null; + ctx: null|any; + bound: any; + update: () => void; + callbacks: any; + after_render: any[]; + props: any; + fragment: null|any; + not_equal: any; + before_render: any[]; + context: Map; + on_mount: any[]; + on_destroy: any[] +} export function bind(component, name, callback) { if (component.$$.props.indexOf(name) === -1) return; @@ -59,7 +75,7 @@ export function init(component, options, instance, create_fragment, not_equal, p const props = options.props || {}; - const $$ = component.$$ = { + const $$: T$$ = component.$$ = { fragment: null, ctx: null, @@ -99,9 +115,9 @@ export function init(component, options, instance, create_fragment, not_equal, p if (options.target) { if (options.hydrate) { - $$.fragment.l(children(options.target)); + $$.fragment!.l(children(options.target)); } else { - $$.fragment.c(); + $$.fragment!.c(); } if (options.intro && component.$$.fragment.i) component.$$.fragment.i(); @@ -115,13 +131,16 @@ export function init(component, options, instance, create_fragment, not_equal, p export let SvelteElement; if (typeof HTMLElement !== 'undefined') { SvelteElement = class extends HTMLElement { + $$: T$$; constructor() { super(); this.attachShadow({ mode: 'open' }); } connectedCallback() { + // @ts-ignore todo: improve typings for (const key in this.$$.slotted) { + // @ts-ignore todo: improve typings this.appendChild(this.$$.slotted[key]); } } @@ -153,6 +172,8 @@ if (typeof HTMLElement !== 'undefined') { } export class SvelteComponent { + $$: T$$; + $destroy() { destroy(this, true); this.$destroy = noop; diff --git a/src/internal/animations.js b/src/internal/animations.ts similarity index 92% rename from src/internal/animations.js rename to src/internal/animations.ts index 8d55c196eed3..b6c7ae2df21a 100644 --- a/src/internal/animations.js +++ b/src/internal/animations.ts @@ -1,6 +1,6 @@ -import { identity as linear, noop, now } from './utils.js'; -import { loop } from './loop.js'; -import { create_rule, delete_rule } from './style_manager.js'; +import { identity as linear, noop, now } from './utils'; +import { loop } from './loop'; +import { create_rule, delete_rule } from './style_manager'; export function create_animation(node, from, fn, params) { if (!from) return noop; @@ -90,4 +90,4 @@ export function fix_position(node) { node.style.transform = `${transform} translate(${a.left - b.left}px, ${a.top - b.top}px)`; } } -} \ No newline at end of file +} diff --git a/src/internal/await-block.js b/src/internal/await-block.ts similarity index 89% rename from src/internal/await-block.js rename to src/internal/await-block.ts index 9e14c50342d2..9527b000ca44 100644 --- a/src/internal/await-block.js +++ b/src/internal/await-block.ts @@ -1,11 +1,11 @@ -import { assign, is_promise } from './utils.js'; -import { check_outros, group_outros, on_outro } from './transitions.js'; -import { flush } from '../internal/scheduler.js'; +import { assign, is_promise } from './utils'; +import { check_outros, group_outros, on_outro } from './transitions'; +import { flush } from '../internal/scheduler'; export function handle_promise(promise, info) { const token = info.token = {}; - function update(type, index, key, value) { + function update(type, index, key?, value?) { if (info.token !== token) return; info.resolved = key && { [key]: value }; @@ -61,4 +61,4 @@ export function handle_promise(promise, info) { info.resolved = { [info.value]: promise }; } -} \ No newline at end of file +} diff --git a/src/internal/dom.js b/src/internal/dom.ts similarity index 80% rename from src/internal/dom.js rename to src/internal/dom.ts index 46ffaa84727c..293040ce202e 100644 --- a/src/internal/dom.js +++ b/src/internal/dom.ts @@ -1,28 +1,28 @@ -export function append(target, node) { +export function append(target:Node, node:Node) { target.appendChild(node); } -export function insert(target, node, anchor) { +export function insert(target: Node, node: Node, anchor?:Node) { target.insertBefore(node, anchor || null); } -export function detach(node) { +export function detach(node: Node) { node.parentNode.removeChild(node); } -export function detach_between(before, after) { +export function detach_between(before: Node, after: Node) { while (before.nextSibling && before.nextSibling !== after) { before.parentNode.removeChild(before.nextSibling); } } -export function detach_before(after) { +export function detach_before(after:Node) { while (after.previousSibling) { after.parentNode.removeChild(after.previousSibling); } } -export function detach_after(before) { +export function detach_after(before:Node) { while (before.nextSibling) { before.parentNode.removeChild(before.nextSibling); } @@ -34,25 +34,30 @@ export function destroy_each(iterations, detaching) { } } -export function element(name) { - return document.createElement(name); +export function element(name: K) { + return document.createElement(name); } -export function object_without_properties(obj, exclude) { - const target = {}; +export function object_without_properties(obj:T, exclude: K[]) { + const target = {} as Pick>; for (const k in obj) { - if (Object.prototype.hasOwnProperty.call(obj, k) && exclude.indexOf(k) === -1) { + if ( + Object.prototype.hasOwnProperty.call(obj, k) + // @ts-ignore + && exclude.indexOf(k) === -1 + ) { + // @ts-ignore target[k] = obj[k]; } } return target; } -export function svg_element(name) { +export function svg_element(name:string):SVGElement { return document.createElementNS('http://www.w3.org/2000/svg', name); } -export function text(data) { +export function text(data:string) { return document.createTextNode(data); } @@ -64,7 +69,7 @@ export function empty() { return text(''); } -export function listen(node, event, handler, options) { +export function listen(node: Node, event: string, handler: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions | EventListenerOptions) { node.addEventListener(event, handler, options); return () => node.removeEventListener(event, handler, options); } @@ -72,6 +77,7 @@ export function listen(node, event, handler, options) { export function prevent_default(fn) { return function(event) { event.preventDefault(); + // @ts-ignore return fn.call(this, event); }; } @@ -79,16 +85,17 @@ export function prevent_default(fn) { export function stop_propagation(fn) { return function(event) { event.stopPropagation(); + // @ts-ignore return fn.call(this, event); }; } -export function attr(node, attribute, value) { +export function attr(node: Element, attribute: string, value?: string) { if (value == null) node.removeAttribute(attribute); else node.setAttribute(attribute, value); } -export function set_attributes(node, attributes) { +export function set_attributes(node: HTMLElement, attributes: { [x: string]: string; }) { for (const key in attributes) { if (key === 'style') { node.style.cssText = attributes[key]; @@ -243,8 +250,8 @@ export function toggle_class(element, name, toggle) { element.classList[toggle ? 'add' : 'remove'](name); } -export function custom_event(type, detail) { - const e = document.createEvent('CustomEvent'); +export function custom_event(type: string, detail?: T) { + const e: CustomEvent = document.createEvent('CustomEvent'); e.initCustomEvent(type, false, false, detail); return e; } diff --git a/src/internal/index.js b/src/internal/index.js deleted file mode 100644 index f3654f6b7791..000000000000 --- a/src/internal/index.js +++ /dev/null @@ -1,12 +0,0 @@ -export * from './animations.js'; -export * from './await-block.js'; -export * from './dom.js'; -export * from './keyed-each.js'; -export * from './lifecycle.js'; -export * from './loop.js'; -export * from './scheduler.js'; -export * from './spread.js'; -export * from './ssr.js'; -export * from './transitions.js'; -export * from './utils.js'; -export * from './Component.js'; \ No newline at end of file diff --git a/src/internal/index.ts b/src/internal/index.ts new file mode 100644 index 000000000000..6487f04525f3 --- /dev/null +++ b/src/internal/index.ts @@ -0,0 +1,12 @@ +export * from './animations'; +export * from './await-block'; +export * from './dom'; +export * from './keyed-each'; +export * from './lifecycle'; +export * from './loop'; +export * from './scheduler'; +export * from './spread'; +export * from './ssr'; +export * from './transitions'; +export * from './utils'; +export * from './Component'; diff --git a/src/internal/keyed-each.js b/src/internal/keyed-each.ts similarity index 98% rename from src/internal/keyed-each.js rename to src/internal/keyed-each.ts index c40a1cc00bbd..0ec8b0940051 100644 --- a/src/internal/keyed-each.js +++ b/src/internal/keyed-each.ts @@ -1,4 +1,4 @@ -import { on_outro } from './transitions.js'; +import { on_outro } from './transitions'; export function destroy_block(block, lookup) { block.d(1); @@ -110,4 +110,4 @@ export function measure(blocks) { let i = blocks.length; while (i--) rects[blocks[i].key] = blocks[i].node.getBoundingClientRect(); return rects; -} \ No newline at end of file +} diff --git a/src/internal/lifecycle.js b/src/internal/lifecycle.ts similarity index 100% rename from src/internal/lifecycle.js rename to src/internal/lifecycle.ts diff --git a/src/internal/loop.js b/src/internal/loop.ts similarity index 71% rename from src/internal/loop.js rename to src/internal/loop.ts index 60c7df4d7096..73f118c234a3 100644 --- a/src/internal/loop.js +++ b/src/internal/loop.ts @@ -1,4 +1,6 @@ -import { now, raf } from './utils.js'; +import { now, raf } from './utils'; + +export interface Task { abort(): void; promise: Promise } const tasks = new Set(); let running = false; @@ -21,7 +23,7 @@ export function clear_loops() { running = false; } -export function loop(fn) { +export function loop(fn: (number)=>void): Task { let task; if (!running) { @@ -30,11 +32,11 @@ export function loop(fn) { } return { - promise: new Promise(fulfil => { + promise: new Promise(fulfil => { tasks.add(task = [fn, fulfil]); }), abort() { tasks.delete(task); } }; -} \ No newline at end of file +} diff --git a/src/internal/scheduler.js b/src/internal/scheduler.ts similarity index 94% rename from src/internal/scheduler.js rename to src/internal/scheduler.ts index 749c3971dcb2..a26a4f8c337c 100644 --- a/src/internal/scheduler.js +++ b/src/internal/scheduler.ts @@ -1,5 +1,5 @@ -import { run_all } from './utils.js'; -import { set_current_component } from './lifecycle.js'; +import { run_all } from './utils'; +import { set_current_component } from './lifecycle'; export const dirty_components = []; export const intros = { enabled: false }; diff --git a/src/internal/spread.js b/src/internal/spread.ts similarity index 100% rename from src/internal/spread.js rename to src/internal/spread.ts diff --git a/src/internal/ssr.js b/src/internal/ssr.ts similarity index 97% rename from src/internal/ssr.js rename to src/internal/ssr.ts index e8d96c609c88..aaa391b4dcf5 100644 --- a/src/internal/ssr.js +++ b/src/internal/ssr.ts @@ -1,5 +1,5 @@ -import { set_current_component, current_component } from './lifecycle.js'; -import { run_all, blank_object } from './utils.js'; +import { set_current_component, current_component } from './lifecycle'; +import { run_all, blank_object } from './utils'; export const invalid_attribute_name_character = /[\s'">/=\u{FDD0}-\u{FDEF}\u{FFFE}\u{FFFF}\u{1FFFE}\u{1FFFF}\u{2FFFE}\u{2FFFF}\u{3FFFE}\u{3FFFF}\u{4FFFE}\u{4FFFF}\u{5FFFE}\u{5FFFF}\u{6FFFE}\u{6FFFF}\u{7FFFE}\u{7FFFF}\u{8FFFE}\u{8FFFF}\u{9FFFE}\u{9FFFF}\u{AFFFE}\u{AFFFF}\u{BFFFE}\u{BFFFF}\u{CFFFE}\u{CFFFF}\u{DFFFE}\u{DFFFF}\u{EFFFE}\u{EFFFF}\u{FFFFE}\u{FFFFF}\u{10FFFE}\u{10FFFF}]/u; // https://html.spec.whatwg.org/multipage/syntax.html#attributes-2 @@ -117,4 +117,4 @@ export function get_store_value(store) { let value; store.subscribe(_ => value = _)(); return value; -} \ No newline at end of file +} diff --git a/src/internal/style_manager.js b/src/internal/style_manager.ts similarity index 93% rename from src/internal/style_manager.js rename to src/internal/style_manager.ts index 367947039997..9ef1b12d1cba 100644 --- a/src/internal/style_manager.js +++ b/src/internal/style_manager.ts @@ -1,5 +1,5 @@ -import { element } from './dom.js'; -import { raf } from './utils.js'; +import { element } from './dom'; +import { raf } from './utils'; let stylesheet; let active = 0; @@ -44,7 +44,7 @@ export function create_rule(node, a, b, duration, delay, ease, fn, uid = 0) { return name; } -export function delete_rule(node, name) { +export function delete_rule(node, name?) { node.style.animation = (node.style.animation || '') .split(', ') .filter(name @@ -63,4 +63,4 @@ export function clear_rules() { while (i--) stylesheet.deleteRule(i); current_rules = {}; }); -} \ No newline at end of file +} diff --git a/src/internal/transitions.js b/src/internal/transitions.ts similarity index 95% rename from src/internal/transitions.js rename to src/internal/transitions.ts index af15e8969eb8..04942e3d16ea 100644 --- a/src/internal/transitions.js +++ b/src/internal/transitions.ts @@ -1,8 +1,8 @@ -import { identity as linear, noop, now, run_all } from './utils.js'; -import { loop } from './loop.js'; -import { create_rule, delete_rule } from './style_manager.js'; -import { custom_event } from './dom.js'; -import { add_render_callback } from './scheduler.js'; +import { identity as linear, noop, now, run_all } from './utils'; +import { loop } from './loop'; +import { create_rule, delete_rule } from './style_manager'; +import { custom_event } from './dom'; +import { add_render_callback } from './scheduler'; let promise; @@ -229,6 +229,7 @@ export function create_bidirectional_transition(node, fn, params, intro) { }; if (!b) { + // @ts-ignore todo: improve typings program.group = outros; outros.remaining += 1; } @@ -309,4 +310,4 @@ export function create_bidirectional_transition(node, fn, params, intro) { running_program = pending_program = null; } }; -} \ No newline at end of file +} diff --git a/src/internal/utils.js b/src/internal/utils.ts similarity index 98% rename from src/internal/utils.js rename to src/internal/utils.ts index 7823a5b920a1..c562cd02b6bc 100644 --- a/src/internal/utils.js +++ b/src/internal/utils.ts @@ -82,7 +82,7 @@ export function exclude_internal_props(props) { const is_client = typeof window !== 'undefined'; -export let now = is_client +export let now: () => number = is_client ? () => window.performance.now() : () => Date.now(); @@ -95,4 +95,4 @@ export function set_now(fn) { export function set_raf(fn) { raf = fn; -} \ No newline at end of file +} diff --git a/src/motion/index.js b/src/motion/index.js deleted file mode 100644 index e0e9bcf1aee5..000000000000 --- a/src/motion/index.js +++ /dev/null @@ -1,2 +0,0 @@ -export * from './spring.js'; -export * from './tweened.js'; \ No newline at end of file diff --git a/src/motion/index.ts b/src/motion/index.ts new file mode 100644 index 000000000000..ea6c646dd94b --- /dev/null +++ b/src/motion/index.ts @@ -0,0 +1,2 @@ +export * from './spring'; +export * from './tweened'; diff --git a/src/motion/spring.js b/src/motion/spring.ts similarity index 73% rename from src/motion/spring.js rename to src/motion/spring.ts index 8b280366b2eb..3977c08f79be 100644 --- a/src/motion/spring.js +++ b/src/motion/spring.ts @@ -1,6 +1,6 @@ -import { writable } from 'svelte/store'; // eslint-disable-line import/no-unresolved -import { loop, now } from 'svelte/internal'; // eslint-disable-line import/no-unresolved -import { is_date } from './utils.js'; +import { Readable, writable } from 'svelte/store'; +import { loop, now, Task } from 'svelte/internal'; +import { is_date } from './utils'; function tick_spring(ctx, last_value, current_value, target_value) { if (typeof current_value === 'number' || is_date(current_value)) { @@ -27,25 +27,41 @@ function tick_spring(ctx, last_value, current_value, target_value) { next_value[k] = tick_spring(ctx, last_value[k], current_value[k], target_value[k]); return next_value; } else { - throw new Error(`Cannot spring ${typeof value} values`); + throw new Error(`Cannot spring ${typeof current_value} values`); } } -export function spring(value, opts = {}) { +interface SpringOpts { + stiffness?: number, + damping?: number, + precision?: number, +} + +type SpringUpdateOpts = { hard?: any; soft?: string | number | boolean; }; + +interface Spring extends Readable{ + set: (new_value: T, opts?: SpringUpdateOpts) => (Promise | Promise); + precision: number; + update: (fn, opts: SpringUpdateOpts) => Promise; + damping: number; + stiffness: number +} + +export function spring(value: T, opts: SpringOpts = {}) { const store = writable(value); const { stiffness = 0.15, damping = 0.8, precision = 0.01 } = opts; - let last_time; - let task; - let current_token; - let last_value = value; - let target_value = value; + let last_time: number; + let task: Task; + let current_token: object; + let last_value:T = value; + let target_value:T = value; let inv_mass = 1; let inv_mass_recovery_rate = 0; let cancel_task = false; - function set(new_value, opts = {}) { + function set(new_value: any, opts: SpringUpdateOpts={}) { target_value = new_value; const token = current_token = {}; @@ -100,9 +116,9 @@ export function spring(value, opts = {}) { }); } - const spring = { + const spring: Spring = { set, - update: (fn, opts) => set(fn(target_value, value), opts), + update: (fn, opts:SpringUpdateOpts) => set(fn(target_value, value), opts), subscribe: store.subscribe, stiffness, damping, diff --git a/src/motion/tweened.js b/src/motion/tweened.ts similarity index 98% rename from src/motion/tweened.js rename to src/motion/tweened.ts index 45520832b79b..3b2d0ef5a2cd 100644 --- a/src/motion/tweened.js +++ b/src/motion/tweened.ts @@ -1,7 +1,7 @@ import { writable } from 'svelte/store'; // eslint-disable-line import/no-unresolved import { assign, loop, now } from 'svelte/internal'; // eslint-disable-line import/no-unresolved import { linear } from 'svelte/easing'; // eslint-disable-line import/no-unresolved -import { is_date } from './utils.js'; +import { is_date } from './utils'; function get_interpolator(a, b) { if (a === b || a !== a) return () => a; @@ -109,4 +109,4 @@ export function tweened(value, defaults = {}) { update: (fn, opts) => set(fn(target_value, value), opts), subscribe: store.subscribe }; -} \ No newline at end of file +} diff --git a/src/motion/utils.js b/src/motion/utils.ts similarity index 63% rename from src/motion/utils.js rename to src/motion/utils.ts index 97a764bf4648..f8bfcce14c9f 100644 --- a/src/motion/utils.js +++ b/src/motion/utils.ts @@ -1,3 +1,3 @@ -export function is_date(obj) { +export function is_date(obj: any) { return Object.prototype.toString.call(obj) === '[object Date]'; -} \ No newline at end of file +} diff --git a/src/store.ts b/src/store.ts index b0ee41fc8d02..361632b61d03 100644 --- a/src/store.ts +++ b/src/store.ts @@ -1,4 +1,4 @@ -import { run_all, noop, safe_not_equal, is_function } from './internal/utils'; +import { run_all, noop, safe_not_equal, is_function } from 'svelte/internal'; /** Callback to inform of a value updates. */ type Subscriber = (value: T) => void; diff --git a/transition.mjs b/src/transition.ts similarity index 97% rename from transition.mjs rename to src/transition.ts index 857fd03165d6..20619ee13178 100644 --- a/transition.mjs +++ b/src/transition.ts @@ -1,5 +1,5 @@ -import { cubicOut, cubicInOut } from './easing'; -import { assign, is_function } from './internal'; +import { cubicOut, cubicInOut } from 'svelte/easing'; +import { assign, is_function } from 'svelte/internal'; export function fade(node, { delay = 0, @@ -179,4 +179,4 @@ export function crossfade({ fallback, ...defaults }) { transition(to_send, to_receive, false), transition(to_receive, to_send, true) ]; -} \ No newline at end of file +} diff --git a/src/utils/indentation.ts b/src/utils/indentation.ts index 765a62bfbf8a..54ededc37d99 100644 --- a/src/utils/indentation.ts +++ b/src/utils/indentation.ts @@ -54,4 +54,4 @@ export function add_indentation(code: MagicString, node: Node, levels = 1) { code.appendLeft(index + 1, indent); } -} \ No newline at end of file +} diff --git a/store.d.ts b/store.d.ts new file mode 100644 index 000000000000..2c1307011b74 --- /dev/null +++ b/store.d.ts @@ -0,0 +1 @@ +export * from './dist/store'; diff --git a/transition.d.ts b/transition.d.ts new file mode 100644 index 000000000000..54d5f036da38 --- /dev/null +++ b/transition.d.ts @@ -0,0 +1 @@ +export * from './dist/transition'; diff --git a/tsconfig.json b/tsconfig.json index fdb7367e054d..994bc61b8534 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,18 +1,35 @@ { - "compilerOptions": { - "target": "ES6", - "diagnostics": true, - "noImplicitThis": true, - "noEmitOnError": true, - "allowJs": true, - "lib": ["es5", "es6", "dom"], - "importHelpers": true, - "moduleResolution": "node" - }, - "include": [ - "src" - ], - "exclude": [ - "node_modules" - ] + "compilerOptions": { + "target": "es2015", + "module": "es6", + "declarationDir": "./dist", + "outDir": "./dist", + "declaration": true, + "noImplicitThis": true, + "noEmitOnError": true, + "lib": [ + "es5", + "es6", + "dom", + "es2015" + ], + "importHelpers": true, + "moduleResolution": "node", + "baseUrl": ".", + "paths": { + "svelte/internal": ["./src/internal"], + "svelte/easing": ["./src/easing"], + "svelte/motion": ["./src/motion"], + "svelte/store": ["./src/store"] + }, + "typeRoots": [ + "node_modules/@types" + ] + }, + "include": [ + "src/**/*" + ], + "exclude": [ + "dist" + ] }