Skip to content

Commit 70e3e41

Browse files
nelsie borjanelsie borja
nelsie borja
authored and
nelsie borja
committed
Code Splitting, prod build, localisation
1 parent d117ae5 commit 70e3e41

27 files changed

+9244
-38
lines changed

.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Misc
2+
.DS_Store
3+
.vscode/
4+
5+
# Java
6+
.classpath
7+
.project
8+
.settings/
9+
bin/
10+
target/
11+
tomcat.8080/
12+
13+
# Client
14+
node_modules

pom.xml

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<url>http://maven.apache.org</url>
1010
<properties>
1111
<tomcat.version>8.5.2</tomcat.version>
12+
<maven.compiler.source>1.6</maven.compiler.source>
13+
<maven.compiler.target>1.6</maven.compiler.target>
1214
</properties>
1315
<dependencies>
1416
<dependency>

src/main/java/launch/Main.java

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
import org.apache.catalina.webresources.DirResourceSet;
99
import org.apache.catalina.webresources.StandardRoot;
1010

11+
// import java.io.FileReader;
12+
13+
// import javax.script.ScriptEngine;
14+
// import javax.script.ScriptEngineManager;
15+
// import javax.script.ScriptException;
16+
1117
public class Main {
1218

1319
public static void main(String[] args) throws Exception {
@@ -37,5 +43,12 @@ public static void main(String[] args) throws Exception {
3743

3844
tomcat.start();
3945
tomcat.getServer().await();
46+
47+
48+
// Server-rendering with Nashorn
49+
// ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn");
50+
// nashorn.eval(new FileReader(webappDirLocation + "public/bundle.js"));
51+
// String markup = nashorn.invokeFunction("renderOnServer");
52+
// Then Pass markup as Java variable
4053
}
4154
}

src/main/webapp/client/.eslintrc.json

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
"indent": ["error", 2],
2626
"linebreak-style": ["error", "unix"],
2727
"quotes": ["error", "double"],
28+
// Append "G_" to all global variables
29+
// "no-unused-vars": ["error", { "varsIgnorePattern": "G_" }],
2830
// Get rid of React warnings
2931
"react/jsx-uses-react": [2],
3032
"react/jsx-uses-vars": [2],

src/main/webapp/client/app/components/App.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from "react";
2-
2+
import { customAddEventListener } from "../helpers/util";
3+
import { importModules } from "../helpers/moduleLoader";
34
import Aux from "./Aux";
45
import Header from "./header";
56
import Routes from "./Routes";
@@ -12,12 +13,25 @@ import "../scss/style.scss";
1213
// To avoid array notation and manually added keys to each of the element,
1314
// you can use an Aux helper function that simply returns all its children.
1415
// const Aux = props => props.children;
15-
export default () => (
16-
<Aux>
17-
<Header />
18-
<Routes />
19-
</Aux>
20-
);
16+
export default class App extends React.Component {
17+
componentDidMount() {
18+
// lazy load routes
19+
customAddEventListener(window, "load", this.lazyLoadRoutes);
20+
}
21+
22+
lazyLoadRoutes = () => {
23+
this.props.history && importModules(this.props.history);
24+
};
25+
26+
render() {
27+
return (
28+
<Aux>
29+
<Header />
30+
<Routes />
31+
</Aux>
32+
);
33+
}
34+
}
2135

2236
// Render modal outside DOM tree
2337
// render() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Reference: https://scotch.io/tutorials/lazy-loading-routes-in-react
2+
3+
import React from "react";
4+
5+
export default function asyncComponent(getComponent) {
6+
class AsyncComponent extends React.Component {
7+
static Component = null;
8+
state = { Component: AsyncComponent.Component };
9+
10+
componentWillMount() {
11+
if (!this.state.Component) {
12+
getComponent().then(Component => {
13+
AsyncComponent.Component = Component;
14+
this.setState({ Component });
15+
});
16+
}
17+
}
18+
render() {
19+
const { Component } = this.state;
20+
if (Component) {
21+
return <Component {...this.props} />;
22+
}
23+
return null;
24+
}
25+
}
26+
return AsyncComponent;
27+
}

src/main/webapp/client/app/components/Provider.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import App from "./App";
77
export default () => (
88
<Provider store={store}>
99
<ConnectedRouter history={history}>
10-
<App />
10+
<App history={history} />
1111
</ConnectedRouter>
1212
</Provider>
1313
);

src/main/webapp/client/app/components/Routes.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import React from "react";
22
import { Switch, Route } from "react-router-dom";
3-
import Home from "./home";
4-
import About from "./about";
3+
import { chunkLoadFailed } from "../helpers/moduleLoader";
4+
// import Home from "./home";
5+
// import About from "./about";
6+
import asyncComponent from "./AsyncComponent";
7+
8+
const Home = asyncComponent(() =>
9+
import(/* webpackChunkName: "homepage" */ "./home")
10+
.then(module => module.default)
11+
.catch(chunkLoadFailed)
12+
);
13+
const About = asyncComponent(() =>
14+
import(/* webpackChunkName: "aboutpage" */ "./about")
15+
.then(module => module.default)
16+
.catch(chunkLoadFailed)
17+
);
518

619
export default () => (
720
<Switch>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default () => "Brands!!!";

src/main/webapp/client/app/components/home/Counter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import MaterialButton from "../material-button";
33

44
export default props => (
55
<section className="g-flex-item-stretch">
6-
<p className="counter">❤️ Rate: {props.count}%</p>
6+
<p className="counter">❤️ Rates: {props.count}%</p>
77

88
<MaterialButton
99
text="Increment"

src/main/webapp/client/app/components/home/Notes.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from "react";
22
import Aux from "../Aux";
3+
import { welcome } from "../../labels";
34

45
export default () => (
56
<Aux>
@@ -10,5 +11,6 @@ export default () => (
1011
<p className="home__note">
1112
Also now supports custom HTML attributes eg: ❤️ 🦄
1213
</p>
14+
{welcome}
1315
</Aux>
1416
);

src/main/webapp/client/app/components/home/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,10 @@ const mapDispatchToProps = dispatch =>
4848
// })
4949

5050
export default connect(mapStateToProps, mapDispatchToProps)(Home);
51+
52+
/* To push to history:
53+
import { push } from "react-router-redux";
54+
push("/about")
55+
-- OR --
56+
this.props.histtory.push("/about")
57+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default () => "Search!!!";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function getLanguage() {
2+
return window.__INITIAL_STATE__.language;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { contains } from "./util";
2+
3+
const importModulesOnDemand = (location, action) => {
4+
if (action === "PUSH") {
5+
if (contains(location.pathname, "about")) {
6+
import(/* webpackChunkName: "brandspage" */ "../components/brands")
7+
.then(module => module)
8+
.catch(chunkLoadFailed);
9+
}
10+
}
11+
};
12+
13+
export function chunkLoadFailed(err) {
14+
console.error("Failed to load chunk => " + err);
15+
}
16+
17+
export function importModules(history) {
18+
// Load immediate dependencies => modules required in all pages
19+
import(/* webpackChunkName: "searchpage" */ "../components/search")
20+
.then(module => module)
21+
.catch(chunkLoadFailed);
22+
23+
// Load current page immediate dependencies => load accessible modules from the page
24+
history.listen(importModulesOnDemand);
25+
}

src/main/webapp/client/app/helpers/util.js

+27
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,32 @@ export function getUniqueId() {
8585
"uid-" + new Date().getTime() + parseInt(Math.random() * 100).toString()
8686
);
8787
}
88+
export function contains(search, target) {
89+
return !!~search.indexOf(target);
90+
}
8891
// END: String creation/manipulation
8992
//<<<<<<<<<<<<<<<<<<<<<<<<
93+
94+
//>>>>>>>>>>>>>>>>>>>>>>>>
95+
// START: Handlers
96+
export function customAddEventListener(
97+
elem,
98+
event,
99+
callback,
100+
useCapture = false
101+
) {
102+
if (elem.addEventListener) {
103+
elem.addEventListener(event, callback, useCapture);
104+
} else {
105+
elem.attachEvent(`on${event}`, callback);
106+
}
107+
}
108+
export function customRemoveEventListener(elem, event, callback) {
109+
if (elem.removeEventListener) {
110+
elem.removeEventListener(event, callback);
111+
} else {
112+
elem.detachEvent(`on${event}`, callback);
113+
}
114+
}
115+
// END: Handlers
116+
//<<<<<<<<<<<<<<<<<<<<<<<<

src/main/webapp/client/app/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ const rootEl = document.querySelector("#root");
66

77
render(<Provider />, rootEl);
88

9+
// window.renderOnClient = function () {
10+
// render(<Provider />, rootEl);
11+
// };
12+
13+
// window.renderOnServer = function () {
14+
// return React.renderToString(<Provider />);
15+
// };
16+
917
if (module.hot) {
1018
module.hot.accept("./components/Provider", () => {
1119
const NextProvider = require("./components/Provider").default;
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
welcome: "Arabic text"
3+
};
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
welcome: "Welcome!!!"
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { getLanguage } from "../helpers/globals";
2+
module.exports = require("./" + getLanguage());
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { combineReducers } from "redux";
22
import { routerReducer } from "react-router-redux";
33
import counter from "./counter";
4+
import language from "./language";
45

56
export default combineReducers({
67
routing: routerReducer,
7-
counter
8+
counter,
9+
language
810
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default (state = {}) => {
2+
return state;
3+
};

0 commit comments

Comments
 (0)