if you use maven you can build your react javascript files on mvn compile or mvn spring-boot:run
include in pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId> <!--Apache 2.0 https://github.com/eirslett/frontend-maven-plugin/blob/master/LICENSE -->
<artifactId>frontend-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<installDirectory>target</installDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v6.3.1</nodeVersion>
<npmVersion>3.9.2</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>webpack build dev</id>
<goals>
<goal>webpack</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
this will install npm and build your react modules.
make sure you have a webpack.config.js and package.json in the same directory:
ill include this as an example:
package.json:
{
"name": "app",
"version": "0.0.1",
"description": "",
"repository": {
"type": "git",
"url": ""
},
"keywords": [
"data"
],
"author": "xxx",
"license": "",
"bugs": {
"url": ""
},
"homepage": "",
"dependencies": {
"react": "^15.3.2",
"react-dom": "^15.3.2",
"prop-types": "^15.5.10",
"rest": "^1.3.1",
"webpack": "^1.12.2"
},
"scripts": {
"start": "node_modules/.bin/webpack-dev-server --progress --inline --hot",
"watch": "webpack --watch -d"
},
"devDependencies": {
"babel-core": "^6.18.2",
"babel-loader": "^6.2.7",
"babel-polyfill": "^6.16.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"babel-preset-react-hmre": "^1.1.1"
}
}
webpack.config.js:
var path = require('path');
var node_dir = __dirname + '/node_modules';
module.exports = {
entry: {
app: './src/main/js/app.jsx',
},
devtool: 'sourcemaps',
cache: false,
debug: true,
resolve: {
alias: {
'stompjs': node_dir + '/stompjs/lib/stomp.js',
}
},
output: {
path: __dirname,
filename: './src/main/resources/static/js/bundled/[name]bundle.js'
},
module: {
loaders: [
{
test: path.join(__dirname, '.'),
exclude: /(node_modules)/,
loader: ['babel-loader'],
query: {
//cacheDirectory: true,
presets: ['es2015', 'react']
}
}
]
}
};
"/src/main/js/app.jsx" -> this is your entrypoint then where you should write your react code.
In your thymeleaf or jsp files include the bundled javascript. (js/bundled/appbundle.js)
Your app.jsx could look like this:
const React = require('react');
const ReactDOM = require('react-dom');
import {MainPage} from './mainpage.jsx';
let exampleprop= 2;
ReactDOM.render(<MainPage prop1={exampleprop} />, document.getElementById('app_root'));