User Tools

Site Tools


Writing /app/www/public/data/meta/toolsandtechnologies/reactwithgradle.meta failed
toolsandtechnologies:reactwithgradle

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
toolsandtechnologies:reactwithgradle [2017/07/26 16:17] – created 1carew1toolsandtechnologies:reactwithgradle [2021/06/25 10:09] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +====== How to Add React to a Gradle Project ======
 + --- //[[colm.carew@errigal.com|Colm Carew]] 2017/07/26 08:16//
 +===== Modifications to build.gradle =====
 +  * You will need to add the following to the buildscript dependencies : <code>classpath "com.moowork.gradle:gradle-node-plugin:0.13"</code>
 +  * You will need to apply the Node Plugin : <code>apply plugin: "com.moowork.node"</code>
 +  * Add the following to the very end of the file : <code>node {
 + version = '6.10.2'
 + download = true
 +}
 +
 +task bundle {
 + dependsOn(['npmInstall', 'npm_run_bundle'])
 +}
 +
 +task webpack {
 + dependsOn(['npmInstall', 'npm_run_webpack'])
 +}
 +
 +// This is spring boot specific, it is for hot reloading static resouces such as index.html which is where your bundle.js is called from
 +bootRun {
 + addResources = true
 +}
 +
 +
 +bootRun.dependsOn(['bundle'])
 +build.dependsOn(['bundle'])</code>
 +
 +===== New files in Project =====
 +  * In the root of your project create a file called .babelrc who's content should be :<code>{
 +  "presets": ["es2015"]
 +}
 +</code>
 +  * Create a file called webpack.config.js in the root of the project who's content should be like : <code>let path = require('path');
 +let webpack = require("webpack");
 +
 +module.exports = {
 +    plugins: [
 +        new webpack.DefinePlugin({ // <-- key to reducing React's size
 +            'process.env': {
 +                'NODE_ENV': JSON.stringify('production')
 +            }
 +        }),
 +        new webpack.optimize.DedupePlugin(), //dedupe similar code
 +        new webpack.optimize.UglifyJsPlugin(), //minify everything
 +        new webpack.optimize.AggressiveMergingPlugin()//Merge chunks
 +    ],
 +    entry: {
 +        index: './src/main/webapp/index.js'
 +    },
 +    output: {
 +        path: './src/main/resources/static',
 +        publicPath: '/assets/',
 +        filename: 'bundle.js'
 +    },
 +    module: {
 +        loaders: [
 +            {
 +                test: /\.js$/,
 +                include: path.join(__dirname, 'src/main/webapp'),
 +                loader: 'babel',
 +                query: {
 +                    presets: ['es2015', 'react']
 +                }
 +            },
 +            {
 +                test: /\.css$/,
 +                loader: 'style!css!'
 +            },
 +            {
 +                test: /\.svg$/,
 +                loader: 'svg-inline'
 +            },
 +            {
 +                test: /\.(png|woff|woff2|eot|ttf|svg)$/,
 +                loader: 'url-loader?limit=100000'
 +            },
 +            {
 +                test: /\.json$/,
 +                loader: 'json'
 +            }
 +        ]
 +    }
 +}
 +;
 +
 +</code>
 +  * For this file note the : <code>    entry: {
 +        index: './src/main/webapp/index.js'
 +    },
 +    output: {
 +        path: './src/main/resources/static',
 +        publicPath: '/assets/',
 +        filename: 'bundle.js'
 +    }</code> 
 +  * entry is where the main React component is so make this where you want all the react code to be. Output path is where the bundle file will go. Ensure this is in the same directory/relatively close directory as the index.html file.
 +  * In the root of the project create a package.json file with contents like : <code>{
 +  "name": "react-grails",
 +  "version": "0.0.1",
 +  "description": "A profile for creating applications with node-based frontends using webpack",
 +  "main": "index.js",
 +  "repository": {},
 +  "dependencies": {
 +    "bootstrap": "^3.3.6",
 +    "react": "^15.4.2",
 +    "react-bootstrap": "^0.30.0-src.1",
 +    "react-dom": "^15.4.2",
 +    "superagent": "^3.5.2",
 +    "superagent-no-cache": "^0.1.1"
 +  },
 +  "scripts": {
 +    "start": "webpack --progress --watch",
 +    "bundle": "webpack",
 +    "test": "echo \"Error: no test specified\" && exit 1"
 +  },
 +  "license": "ISC",
 +  "devDependencies": {
 +    "babel-core": "6.9.1",
 +    "babel-loader": "6.2.4",
 +    "babel-preset-es2015": "6.9.0",
 +    "babel-preset-react": "^6.23.0",
 +    "css-loader": "^0.27.3",
 +    "file-loader": "^0.10.1",
 +    "json-loader": "^0.5.4",
 +    "style-loader": "^0.16.0",
 +    "svg-inline-loader": "^0.7.1",
 +    "url-loader": "^0.5.8",
 +    "webpack": "1.13.1"
 +  }
 +}
 +</code>
 +  * Create you index.html file in a close directory to where your bundle.js goes. If you are replacing the root div in React then the index.html should look something like : <code><!DOCTYPE html>
 +<html>
 +<head>
 +    <title>Content Distributor</title>
 +    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
 +</head>
 +<body>
 +<div id='root'></div>
 +
 +<script src="./bundle.js"></script>
 +</body>
 +</html></code>
 +  * Run npm install and npm run bundle. Ensure the bundle.js file is created where the output should be. You will obviously need node installed locally for this. Once bundle.js is building through node, delete the bundle.js file and run ./gradlew build and ensure the bundle.js creates
 +  * In your backend you will obviously need some controller that renders the index.html or that uses the bundle.js