Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/griffio/spring-boot-web-nashorn
Feasibility of rendering React 16.x templates from Nashorn with babel-standalone, with Spring Boot and Kotlin
https://github.com/griffio/spring-boot-web-nashorn
kotlin nashorn react rendering-react spring-boot
Last synced: about 2 months ago
JSON representation
Feasibility of rendering React 16.x templates from Nashorn with babel-standalone, with Spring Boot and Kotlin
- Host: GitHub
- URL: https://github.com/griffio/spring-boot-web-nashorn
- Owner: griffio
- Created: 2015-04-01T20:25:22.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-02-21T11:55:34.000Z (almost 7 years ago)
- Last Synced: 2024-04-16T18:27:03.226Z (9 months ago)
- Topics: kotlin, nashorn, react, rendering-react, spring-boot
- Language: Kotlin
- Homepage:
- Size: 363 KB
- Stars: 27
- Watchers: 6
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Render React template from Nashorn
This is just an experiment tracking the feasibility of using v.latest React in Nashorn.
Recent Spring Developer presentation: [isomorphic-templating-with-spring-boot-nashorn-and-react](http://www.slideshare.net/SpringCentral/isomorphic-templating-with-spring-boot-nashorn-and-react)
React 16.1.x
0.13.x leaned on JSX Transformer to perform standalone JSX translation; this was removed in current React versions.
A BabelJs environment is needed to compile JSX.
However, BabelJs also needs NodeJs environment, so use a babel-standalone that bundles all required modules.### Now in 4.2.0.RELEASE
[Jira SPR-12266](https://jira.spring.io/browse/SPR-12266)
[Spring Framework SPR-12266](https://github.com/sdeleuze/spring-framework/tree/SPR-12266)Use jdk-8u72-ea or higher. https://jdk8.java.net/download.html
Early access fix for: Method code too large in Babel - https://bugs.openjdk.java.net/browse/JDK-8135190
Note:- this is also the case with Typescript https://github.com/Microsoft/TypeScript/issues/1789
### Spring Boot with ReactJs templates with Nashorn and Kotlin 1.2.x
React 16.1.x requires BabelJs environment to compile JSX
BabelJs requires nodejs api dependencies.However, a stand-alone build of Babel for use in non-Node.js environments used as https://babeljs.io/docs/usage/browser/ now removed.
https://github.com/Daniel15/babel-standaloneThe package create-react-class is used as Java8 Nashorn is ECMAScript 5.x as per https://reactjs.org/docs/react-without-es6.html
[Nashorn Extensions](https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions) - allow map support
~~~javascript
global = {};
console = {};
console.debug = print;
console.warn = print;
console.log = print;
console.error = print;function render(template, model) {
var data = {};
for (var k in model) {
if (model[k] instanceof Java.type("java.lang.Iterable")) {
data[k] = Java.from(model[k]);
} else {
data[k] = model[k];
}
}
var element = React.createElement(eval(template), data);
return ReactDOMServer.renderToStaticMarkup(element);
}function renderJsx(template, model) {
var jsTemplate = Babel.transform(template, { presets: ['react'] }).code;
return render(jsTemplate, model);
}~~~
[Notes on Spring Boot](http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-boot-application.html)
Test with JDK
~~~
./gradlew test
~~~Application
~~~
./gradlew bootRun
~~~Route
http://localhost:8080/about.html
Rendered from
https://github.com/griffio/spring-boot-web-nashorn/blob/master/src/main/resources/templates/about.jsx
==============
### Kotlin 1.2.x
kotlin_version=1.2.21
basic gradle (4.x) build template for Java 8