svelte-translator
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.0
or higher so that you can install svelte@2
and svelte@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.
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.
;; const s3r = ; const s2r = s3r; // Non-object values are mapped to the "value" key s2r; // { value : 0 } const s3w = ; const s2w = s3w; // Object values are splatted into the store s2w; // { fooga : 1, booga : 0 } s2w; s3w;
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; const store = ...; ;
<!-- my-component.html -->{$store.foo} {reactive} {$dstore}
Rollup bundling of Svelte v2 & Svelte v3
Loading Svelte2 Components
// Optional preprocessor preprocess : ... // Options for the svelte compiler options : dev : true
Loading Svelte3 Components
// Optional preprocessors preprocess : ... // Options for the svelte compiler options : dev : true