Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

svelte-translator

tivac35MIT4.0.1

Tools for combining svelte2 and svelte3

svelte, svelte2, svelte3, svelte, migration, adapter, translator

readme

svelte-translator Build Status NPM Version NPM License NPM Downloads

Use svelte v2 features inside svelte v3, and use svelte v3 features inside svelte v2. Because sometimes there's no amount of automated component rewriting that will make things work but you want to start a gradual transition.

Features

Feature Svelte 3 in 2 Svelte 2 in 3
Stores
Components 💭
Rollup Bundling

Legend

  • ✔ feature exists and works
  • 🔧 feature in progress
  • 💭 feature is planned

Installation

$> npm install svelte-translator

You'll also need to use `npm@6.9.0or higher so that you can installsvelte@2andsvelte@3` side-by-side.

$> npm install svelte2@npm:svelte@2
$> npm install svelte@latest

⚠️ Make sure you update any existing references to svelte to point to svelte2 now that svelte v3 is the default

Usage

Svelte v3 in Svelte v2

Components

Wrap up a v3 component so it can be included within a svelte2 component.

<Wrapper {component} {props} />

<script>
import Component from "./svelte3-component.svelte";

export default {
    components : {
        Wrapper : "svelte-translator/3in2/component.html",
    },

    data : () => ({
        component : Component,
        props     : {
            // any props for the component
        }
    }),
};
Data Usage
component The v3 component to wrap
props Properties to set on the v3 component

Stores

A small svelte2 store that expects to be passed either a readable or writable svelte3 store and will make the value of the svelte3 store available to components using the standard APIs.

import { readable } from "svelte/store";
import Adapter from "svelte-translator/3in2/store.js";

const s3r = readable(0);

const s2r = new Adapter(s3r);

// Non-object values are mapped to the "value" key

s2r.get(); // { value : 0 }

const s3w = writable({ fooga : 1, booga : 0 });

const s2w = new Adapater(s3w);

// Object values are splatted into the store

s2w.get(); // { fooga : 1, booga : 0 }

s2w.set({ fooga : 2 });

s3w.subscribe((value) => {
    // { fooga : 2, booga : 0 }
});

Svelte v2 in Svelte v3

Stores

svelte-translator/2in3/store.js implements the v3 store contract (.set() is provided natively by v2 stores, it implements .subscribe() & .update()) in a class that extends svelte/store.js so it can be a drop-in replacement.

Here's an example of how to make v2 stores usable in v3 components.

- import { Store } from "svelte/store.js";
+ import { Store } from "svelte-translator/2in3/store.js";

The svelte-translator version is fully functional in v3 components. You can use its values directly within the template, within the component <script> block either staticly or in reactive statements, and it can also be used as an input to any derived stores.

It also remains a completely valid svelte v2 store that functions just as they always have in your v2 components.

// my-store.js
import { Store } from "svelte-translator/2in3/store.js";

const store = new Store({
    ...
});

export default store;
<!-- my-component.html -->
<div>{$store.foo}</div>

<p>
{reactive}
</p>

<span>{$dstore}</span>

<script>
import store from "./my-store.js";
import { derived } from "svelte/store";

$: reactive = $store.foo + "ey";

const dstore = derived(store, ($foo, set) => set(foo + "ey"));
</script>

Rollup bundling of Svelte v2 & Svelte v3

Loading Svelte2 Components

require("svelte-translator/rollup/svelte2.js")({
    // Optional preprocessor
    preprocess : { ... },

    // Options for the svelte compiler
    options : {
        dev : true
    },
}),

Loading Svelte3 Components

require("svelte-translator/rollup/svelte3.js")({
    // Optional preprocessors
    preprocess : [
        { ... },
    ],

    // Options for the svelte compiler
    options : {
        dev : true
    },
}),

changelog

4.0.1 (2019-08-28)

Bug Fixes

4.0.0 (2019-07-01)

Features

  • Split props out into a separate field (#35) (774acd6)

BREAKING CHANGES

  • Props for the svelte3 component must now be passed to the wrapped as props instead of the entire state of the wrapper being passed along. This prevents warnings from svelte3 components and is generally cleaner overall.

3.1.4 (2019-05-29)

Features

  • rollup: dependency tracking for svelte2 (98ede4c)

3.1.3 (2019-05-17)

Bug Fixes

  • rollup: Given components reasonable names (4ca036e)

3.1.2 (2019-05-09)

Bug Fixes

  • 3in2: smarter diff check for component values (cff84bf)

3.1.1 (2019-05-04)

3.1.0 (2019-05-04)

Features

  • 2in3: Add store adapter for 2in3 (6eabc7a)

3.0.0 (2019-05-03)

Features

  • 3in2: No object spread, and no
    wrapper (ddb2795)
  • 3in2: pass props along transparently (371a370)

BREAKING CHANGES

  • 3in2: Instead of inserting a <div> that contaiins the svelte3 component now a <span> will be injected and used as an anchor point so that the svelte3 component is in the correct place in the DOM hierarchy without an extra wrapping element.
  • 3in2: Previously props to be passed to the svelte3 component would be set in a props object, now they live alongside the special component and attrs properties. This facilitates easier porting of code since the wrapper component can be instantiated & have updated props passed to it exactly like the svelte2 component did previously.

2.1.0 (2019-05-02)

Bug Fixes

  • rollup: use correct preprocessor args (550ca5d)

Features

  • rollup: pass params to svelte compilers (94ec5ae)

2.0.1 (2019-04-22)

2.0.0 (2019-04-22)

1.2.0 (2019-04-22)

Features

  • 3in2: add component swapping support (80f19cf)

1.1.0 (2019-04-08)

Bug Fixes

  • more complete svelte3 component cleanup (1365315)

Features

  • add svelte2 rollup loader (d45a4fb)
  • get svelte3 rollup loader functional (a81008e)

1.0.0 (2019-04-07)

Bug Fixes

  • properly :skull: on missing svelte3 component (24ab520)

Features

  • first pass at some basic functionality (43e380b)
  • flesh out 3in2 store adapter & tests (c90ea72)