|
| 1 | +--- |
| 2 | +title: Migrating from v2.x to v3.x |
| 3 | +sidebarPartial: docsSidebar |
| 4 | +--- |
| 5 | + |
| 6 | +# Migrating from v2.x to v3.x |
| 7 | + |
| 8 | +<!-- MarkdownTOC depth=1 autolink=true bracket=round --> |
| 9 | + |
| 10 | +- [RxJS imports](#rxjs-imports) |
| 11 | +- [Lodash imports](#lodash-imports) |
| 12 | +- [Webpack builds](#webpack-builds) |
| 13 | +- [`frint-store`](#frint-store) |
| 14 | +- [`frint-model`](#frint-model) |
| 15 | +- [`frint-compat`](#frint-compat) |
| 16 | + |
| 17 | +<!-- /MarkdownTOC --> |
| 18 | + |
| 19 | +## RxJS imports |
| 20 | + |
| 21 | +All RxJS modules are now imported individually, resulting in smaller bundle sizes in your applications. |
| 22 | + |
| 23 | +This also means, the observables returned by FrintJS packages will not have the operators (like `map`, `filter`, etc) built in any more, and they need to be imported manually too if you need them. |
| 24 | + |
| 25 | +### Before |
| 26 | + |
| 27 | +We can take an example from `frint-store`: |
| 28 | + |
| 29 | +```js |
| 30 | +const todos$ = store.getState$() |
| 31 | + .filter(state => state.showTodos === true) |
| 32 | + .map(state => state.todos); |
| 33 | + |
| 34 | +todos$.subscribe(todos => console.log(todos)); |
| 35 | +``` |
| 36 | +### After |
| 37 | + |
| 38 | +```js |
| 39 | +import { filter } from 'rxjs/operator/filter'; |
| 40 | +import { map } from 'rxjs/operator/map'; |
| 41 | + |
| 42 | +const todos$ = store.getState$() |
| 43 | + ::filter(state => state.showTodos === true) |
| 44 | + ::map(state => state.todos); |
| 45 | + |
| 46 | +todos$.subscribe(todos => console.log(todos)); |
| 47 | +``` |
| 48 | + |
| 49 | +The syntax above is written using the [bind-operator](https://github.com/tc39/proposal-bind-operator). |
| 50 | + |
| 51 | +It could also have been written like this: |
| 52 | + |
| 53 | +```js |
| 54 | +import { filter } from 'rxjs/operator/filter'; |
| 55 | +import { map } from 'rxjs/operator/map'; |
| 56 | + |
| 57 | +const todos$ = map.call( |
| 58 | + filter.call( |
| 59 | + store.getState$(), |
| 60 | + state => state.showTodos === true |
| 61 | + ), |
| 62 | + state => state.todos |
| 63 | +); |
| 64 | + |
| 65 | +todos$.subscribe(todos => console.log(todos)); |
| 66 | +``` |
| 67 | + |
| 68 | +## Lodash imports |
| 69 | + |
| 70 | +All lodash imports have also been updated to import only the modules that are needed. |
| 71 | + |
| 72 | +If you want to take benefit of reduced bundle size in your applications, then you can follow the same approach. |
| 73 | + |
| 74 | +### Before |
| 75 | + |
| 76 | +```js |
| 77 | +import _ from 'lodash'; |
| 78 | + |
| 79 | +_.isPlainObject({}); |
| 80 | +``` |
| 81 | + |
| 82 | +## After |
| 83 | + |
| 84 | +```js |
| 85 | +import isPlainObject from 'lodash/isPlainObject'; |
| 86 | + |
| 87 | +isPlainObject({}); |
| 88 | +``` |
| 89 | + |
| 90 | +## Webpack builds |
| 91 | + |
| 92 | +If your application is set up in a way like this: |
| 93 | + |
| 94 | +* `vendors.js`: All third party libraries (RxJS, Lodash, Frint, etc) |
| 95 | +* `app.js`: Your custom code for Frint App(s) |
| 96 | + |
| 97 | +Or you are loading the vendors directly from CDN via `<script>` tags, then you risk loading some RxJS and Lodash modules twice. |
| 98 | + |
| 99 | +To help avoid that, we are shipping a new `frint-config` package that has information about the list of external dependencies, that you can map accordingly. |
| 100 | + |
| 101 | +### Before |
| 102 | + |
| 103 | +```js |
| 104 | +// webpack.config.js |
| 105 | +module.exports = { |
| 106 | + entry: __dirname + '/src/index.js', |
| 107 | + output: { |
| 108 | + path: __dirname + '/build' |
| 109 | + }, |
| 110 | + externals: { |
| 111 | + 'lodash': '_', // window._ |
| 112 | + 'rxjs': 'Rx', // window.Rx |
| 113 | + |
| 114 | + // other libraries |
| 115 | + 'react': 'React', |
| 116 | + 'react-dom': 'ReactDOM', |
| 117 | + }, |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### After |
| 122 | + |
| 123 | +```js |
| 124 | +// webpack.js |
| 125 | + |
| 126 | +// all RxJS and Lodash imports |
| 127 | +// are mapped to global `window._` and `window.Rx` |
| 128 | +const externals = require('frint-config').externals; |
| 129 | + |
| 130 | +module.exports = { |
| 131 | + entry: __dirname + '/src/index.js', |
| 132 | + output: { |
| 133 | + path: __dirname + '/build' |
| 134 | + }, |
| 135 | + externals: Object.assign( |
| 136 | + {}, |
| 137 | + |
| 138 | + // RxJS and Lodash |
| 139 | + externals, |
| 140 | + |
| 141 | + // other libraries |
| 142 | + { |
| 143 | + 'react': 'React', |
| 144 | + 'react-dom': 'ReactDOM', |
| 145 | + } |
| 146 | + ), |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +## frint-store |
| 151 | + |
| 152 | +`thunkArgument` option has been renamed to `deps`. |
| 153 | + |
| 154 | +### Before |
| 155 | + |
| 156 | +```js |
| 157 | +import { createStore } from 'frint-store'; |
| 158 | + |
| 159 | +const Store = createStore({ |
| 160 | + thunkArgument: {}, |
| 161 | +}); |
| 162 | +``` |
| 163 | + |
| 164 | +### After |
| 165 | + |
| 166 | +```js |
| 167 | +import { createStore } from 'frint-store'; |
| 168 | + |
| 169 | +const Store = createStore({ |
| 170 | + deps: {}, |
| 171 | +}); |
| 172 | +``` |
| 173 | + |
| 174 | +## frint-model |
| 175 | + |
| 176 | +Deprecated and will be completely removed when v4.0 gets released. Use `frint-data` instead. |
| 177 | + |
| 178 | +### Before |
| 179 | + |
| 180 | +```js |
| 181 | +import { createModel } from 'frint-model'; |
| 182 | + |
| 183 | +const Todo = createModel({ |
| 184 | + getTitle() { |
| 185 | + return this.get('title'); |
| 186 | + }, |
| 187 | + setTitle(newTitle) { |
| 188 | + this.set('title', newTitle); |
| 189 | + }, |
| 190 | +}); |
| 191 | + |
| 192 | +const todo = new Todo({ |
| 193 | + title: 'Hello World', |
| 194 | + completed: false, |
| 195 | +}); |
| 196 | + |
| 197 | +const title = todo.getTitle(); |
| 198 | +todo.setTitle('Updated title'); |
| 199 | +``` |
| 200 | + |
| 201 | +### After |
| 202 | + |
| 203 | +```js |
| 204 | +import { Types, createModel } from 'frint-data'; |
| 205 | + |
| 206 | +const Todo = createModel({ |
| 207 | + schema: { |
| 208 | + title: Types.string, // or, Types.string.required |
| 209 | + completed: Types.bool, // or, Types.bool.defaults(false), |
| 210 | + }, |
| 211 | + setTitle(newTitle) { |
| 212 | + this.title = newTitle; |
| 213 | + }, |
| 214 | +}); |
| 215 | + |
| 216 | +const todo = new Todo({ |
| 217 | + title: 'Hello World', |
| 218 | + completed: false, |
| 219 | +}); |
| 220 | + |
| 221 | +const title = todo.title; |
| 222 | +todo.setTitle('Updated title'); |
| 223 | +``` |
| 224 | + |
| 225 | +## frint-compat |
| 226 | + |
| 227 | +Completely emptied, and `v0.x` is no longer supported in any way. |
0 commit comments