reagent/doc/0.8-upgrade.md

107 lines
4.3 KiB
Markdown
Raw Normal View History

2017-07-31 10:36:34 +00:00
# 0.8 Upgrade guide
The necessary changes depend on what environment you target, and
how you want to provide React.
2018-03-13 22:54:51 +00:00
| | Build | Browser | Node |
|---|---|---|---|
| Cljsjs | `:none` | Supported | Requires Cljs 1.10.238+ |
| Cljsjs | `:advanced` | Supported | Requires Cljs 1.10.238+ |
2018-06-15 22:01:15 +00:00
| `node modules` | `:none` | Requires Cljs 1.10.312 | Supported |
| `node modules` | `:advanced` | Requires Cljs 1.10.312 | Supported |
2018-03-13 22:54:51 +00:00
2017-07-31 10:36:34 +00:00
## Browser - Cljsjs
2018-04-04 17:41:03 +00:00
**This is the recommended setup.**
2018-03-13 22:54:51 +00:00
Using Reagent with Cljsjs packages doesn't require changes,
other than making sure you update Cljsjs React dependencies,
2017-07-31 10:36:34 +00:00
if you have direct dependencies to them.
2018-04-04 17:41:03 +00:00
To ensure Cljsjs libs are used instead of Node Modules, you can add `:npm-deps false`, which will make sure ClojureScript compiler doesn't look into `node_modules` directory if it exists.
2017-07-31 10:36:34 +00:00
## Browser - node modules
If `react`, `react-dom` and `create-react-class` are available in `node_modules`
2018-04-04 17:41:03 +00:00
directory, ClojureScript compiler will use these with Reagent.
2018-03-13 22:54:51 +00:00
If you don't want to call `npm` and manage `package.json`, you can use `:npm-deps` and `:install-deps` compiler options to
2018-04-04 17:41:03 +00:00
have ClojureScript install the packages automatically.
2017-07-31 10:36:34 +00:00
2017-10-31 12:16:20 +00:00
You can use `:process-shim` compiler option to provide `process.env.NODE_ENV`
2017-07-31 10:36:34 +00:00
constant which is used by JS code to enable development and production
builds. ClojureScript compiler will automatically set this constant to
`production` value when using `:advanced` optimizations. This enables
the React production build.
When using module processing, it should be possible to split output into several
[modules](https://clojurescript.org/reference/compiler-options#modules).
**Externs are required for use with node modules also!** React created objects
statically in several places and then accesses those dynamically. Closure-compiler
will in these cases rename the statically object properties, which will break
dynamically accessing the objects. Externs fix this by defining which properties
must not be renamed.
2018-06-15 22:01:15 +00:00
## Browser - Webpack
2018-01-09 12:18:23 +00:00
2018-06-15 22:01:15 +00:00
https://clojurescript.org/guides/webpack
2018-01-09 12:18:23 +00:00
If you want to load React.js yourself from external JS file (CDN) or from custom bundle,
it should be possible to override the Cljsjs foreign-libs, while still using externs from Cljsjs packages. To override the foreign-libs, you can provide following compiler option:
```clj
:foreign-libs
2018-06-15 22:01:15 +00:00
[{:file "bundje.js",
2018-01-09 12:18:23 +00:00
:provides ["react" "react-dom" "create-react-class" "react-dom/server"],
2018-01-09 12:19:17 +00:00
:global-exports {react React
react-dom ReactDOM
create-react-class createReactClass
react-dom/server ReactDOMServer}}]
2018-01-09 12:18:23 +00:00
```
## NodeJS - Cljsjs
Requires https://github.com/clojure/clojurescript/commit/f7d611d87f6ea8a605eae7c0339f30b79a840b49
2018-04-23 08:09:59 +00:00
Available in 1.10.238
2018-03-13 22:54:51 +00:00
2018-01-09 12:18:23 +00:00
Reagent should use Cljsjs libraries by default even when running on Node.
## NodeJS - node modules
2017-07-31 10:36:34 +00:00
Install `react`, `react-dom` and `create-react-class` npm packages,
and ClojureScript should automatically use `require` to
load React for Reagent.
## Electron
???
## React-native
https://github.com/drapanjanas/re-natal/issues/128
## Common Problems
### Mismatch with Cljsjs and npm packages
If you have one npm package installed, e.g. `react`, you also need
to provide others (`react-dom` and `create-react-class`), else
Cljsjs packages would be used for these, and packages from different sources
don't work together.
2018-06-15 22:01:15 +00:00
### Previous problems
Before ClojureScript 1.10.312 there were couple of problems with npm support:
1. Closure can't properly handle React 16 CommonJS module pattern: https://github.com/google/closure-compiler/issues/2841
This causes the production React code being loaded even for development builds.
Using Chrome React Developer Tools with this setup will break Reagent. Fixed by `[com.google.javascript/closure-compiler-unshaded "v20180610"]` ([PR](https://github.com/google/closure-compiler/pull/2963)), will be
the included in next the ClojureScript release.
2. Closure optimization currently breaks certain statically created objects which are
accessed dynamically in `ReactDOM/server`: https://github.com/facebook/react/issues/12368
Fixed by using `[com.google.javascript/closure-compiler-unshaded "v20180319"]` ([fix commit](https://github.com/google/closure-compiler/commit/c13cf48b98477e44409dba6359246bffa95b1c7b)), will be
the default in next ClojureScript release.