-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
extract React specific code into subapp-react module #1485
Merged
Merged
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
588fe15
1
jchip ccfa763
2
jchip dda2daa
3
jchip ac01e91
4
jchip ceb0e5c
5
jchip 7981656
6
jchip 588a867
7
jchip 18b4854
8
jchip ab89b81
9
jchip 5b155cd
10
jchip 9cf9a4d
11
jchip bb35ee3
12
jchip e777afe
13
jchip c9e2438
14
jchip 4edc37c
15
jchip e61e5d3
16
jchip 5a77f5d
17
jchip 8a2f2a3
18
jchip f2c9fc4
19
jchip 32ea79e
20
jchip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
presets: [["@babel/env", { modules: "auto", targets: { node: "current" } }], "@babel/react"] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
browser | ||
dist | ||
*-lock.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Electrode Subapp For React | ||
|
||
This module mainly serve to setup subapp-web with Facebook React framework. | ||
|
||
It basically re-exports the module subapp-web and sets it up with React specific APIs. | ||
|
||
For convenience, it also exports the module `react`. | ||
|
||
To use, a subapp's code should be doing: | ||
|
||
```js | ||
import { React, loadSubApp } from "subapp-react"; | ||
|
||
import Component from "./component"; | ||
|
||
export default loadSubApp({ name: "MyComponent", Component }); | ||
``` | ||
|
||
For all pratical purposes, if there's code somewhere else that ensures subapp-web is setup with the proper React framework, then it's equivalent to the following: | ||
|
||
```js | ||
import React from "react"; | ||
import { loadSubApp } from "subapp-web"; | ||
|
||
import Component from "./component"; | ||
|
||
export default loadSubApp({ name: "MyComponent", Component }); | ||
``` | ||
|
||
`react` and `react-dom` are specified as peerDependencies, so you must install them as part of your `package.json` dependencies. | ||
|
||
## SSR App Context | ||
|
||
This module also exports a default React context that SSR uses to pass in server `request` object to your React component. | ||
|
||
ie: | ||
|
||
```js | ||
import { AppContext } from "subapp-react"; | ||
|
||
const Component = () => { | ||
return ( | ||
<AppContext.Consumer> | ||
{({ isSsr, ssr, subApp }) => { | ||
return ( | ||
<div> | ||
IS_SSR: {`${Boolean(isSsr)}`} HAS_REQUEST: {ssr && ssr.request ? "yes" : "no"} | ||
</div> | ||
); | ||
}} | ||
</AppContext.Consumer> | ||
); | ||
}; | ||
``` | ||
|
||
## Support for React Router | ||
|
||
If you want to use [react-router] in your application, then you need to install the dependencies: | ||
|
||
- [react-router] and [react-router-dom] | ||
|
||
ie: | ||
|
||
```bash | ||
npm i react-router react-router-dom | ||
``` | ||
|
||
And then set the `useReactRouter` flag to true in your subapp: | ||
|
||
```js | ||
import { React, loadSubApp } from "subapp-react"; | ||
|
||
export default loadSubApp({ name: "MySubapp", Component, useReactRouter: true }); | ||
``` | ||
|
||
## License | ||
|
||
Copyright (c) 2016-present, WalmartLabs | ||
|
||
Licensed under the [Apache License, Version 2.0]. | ||
|
||
[apache license, version 2.0]: https://www.apache.org/licenses/LICENSE-2.0 | ||
[react-router]: https://www.npmjs.com/package/react-router | ||
[react-router-dom]: https://www.npmjs.com/package/react-router-dom | ||
|
||
``` | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"presets": [ | ||
["env", { | ||
"targets": { | ||
"node": "8.15.0" | ||
} | ||
} ], "react" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"use strict"; | ||
|
||
const subappWeb = require("subapp-web"); | ||
const React = require("react"); | ||
const FrameworkLib = require("./framework-lib"); | ||
const { default: AppContext } = require("../browser/app-context"); | ||
|
||
subappWeb.setupFramework(FrameworkLib); | ||
|
||
module.exports = { | ||
...subappWeb, | ||
AppContext, | ||
FrameworkLib, | ||
React | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
{ | ||
"name": "subapp-react", | ||
"version": "0.0.1", | ||
"description": "Electrode subapp support for React/Redux/React Router", | ||
"browser": "browser/index.js", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "clap check", | ||
"coverage": "clap check", | ||
"build": "clap -x -n compile minify", | ||
"compile": "babel src -d browser --source-maps" | ||
}, | ||
"keywords": [ | ||
"web", | ||
"react", | ||
"subapp", | ||
"redux", | ||
"react-router" | ||
], | ||
"author": "Electrode (http://www.electrode.io/)", | ||
"contributors": [ | ||
"Joel Chen <xchen@walmartlabs.com>" | ||
], | ||
"license": "Apache-2.0", | ||
"files": [ | ||
"lib", | ||
"browser", | ||
"dist" | ||
], | ||
"dependencies": { | ||
"subapp-web": "^1.0.0", | ||
"subapp-util": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.2.3", | ||
"@babel/core": "^7.2.2", | ||
"@babel/preset-env": "^7.3.1", | ||
"@babel/preset-react": "^7.0.0", | ||
"@babel/register": "^7.7.7", | ||
"babel-preset-minify": "^0.5.1", | ||
"electrode-archetype-njs-module-dev": "^3.0.0", | ||
"react": "^16.8.3", | ||
"react-dom": "^16.8.3", | ||
"react-redux": "^6.0.1", | ||
"redux": "^4.0.1" | ||
}, | ||
"peerDependencies": { | ||
"react": "*", | ||
"react-dom": "*" | ||
}, | ||
"fyn": { | ||
"dependencies": { | ||
"subapp-web": "../subapp-web", | ||
"subapp-util": "../subapp-util" | ||
} | ||
}, | ||
"nyc": { | ||
"all": true, | ||
"require": [ | ||
"@babel/register", | ||
"mocha" | ||
], | ||
"reporter": [ | ||
"lcov", | ||
"text", | ||
"text-summary" | ||
], | ||
"exclude": [ | ||
"coverage", | ||
"*clap.js", | ||
"gulpfile.js", | ||
"dist", | ||
"test", | ||
"browser", | ||
"**/.babelrc.js" | ||
], | ||
"check-coverage": true, | ||
"statements": 0, | ||
"branches": 0, | ||
"functions": 0, | ||
"lines": 0, | ||
"cache": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
presets: [ | ||
["@babel/env", { modules: "auto" }], | ||
"@babel/react", | ||
process.env.MINIFY ? "minify" : undefined | ||
].filter(x => x) | ||
}; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import FrameworkLib from "./fe-framework-lib"; | ||
|
||
import { setupFramework } from "subapp-web"; | ||
|
||
setupFramework(FrameworkLib); | ||
|
||
export * from "subapp-web"; | ||
|
||
export { default as React } from "react"; | ||
|
||
export { default as AppContext } from "./app-context"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--require node_modules/electrode-archetype-njs-module-dev/config/test/setup.js | ||
--require @babel/register | ||
--recursive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"use strict"; | ||
|
||
const React = require("react"); // eslint-disable-line | ||
const lib = require("../../lib"); | ||
|
||
describe("SSR React framework", function() { | ||
it("should setup React framework", () => { | ||
expect(lib.React).to.be.ok; | ||
expect(lib.AppContext).to.be.ok; | ||
expect(lib.FrameworkLib).to.be.ok; | ||
expect(lib.loadSubApp).to.be.a("function"); | ||
}); | ||
|
||
it("should not do SSR without component", async () => { | ||
const framework = new lib.FrameworkLib({ | ||
subApp: {}, | ||
subAppServer: {}, | ||
props: {} | ||
}); | ||
const res = await framework.handleSSR(); | ||
expect(res).contains("has no StartComponent"); | ||
}); | ||
|
||
it("should render Component from subapp with initial props from prepare", async () => { | ||
const framework = new lib.FrameworkLib({ | ||
subApp: { | ||
prepare: () => ({ test: "foo bar" }), | ||
Component: props => { | ||
return <div>Hello {props.test}</div>; | ||
} | ||
}, | ||
subAppServer: {}, | ||
props: { serverSideRendering: true }, | ||
context: { | ||
user: {} | ||
} | ||
}); | ||
const res = await framework.handleSSR(); | ||
expect(res).contains("Hello foo bar"); | ||
}); | ||
|
||
it("should render Component from subapp with initial props from server's prepare", async () => { | ||
const framework = new lib.FrameworkLib({ | ||
subApp: { | ||
Component: props => { | ||
return <div>Hello {props.test}</div>; | ||
} | ||
}, | ||
subAppServer: { | ||
prepare: () => ({ test: "foo bar" }) | ||
}, | ||
props: { serverSideRendering: true }, | ||
context: { | ||
user: {} | ||
} | ||
}); | ||
const res = await framework.handleSSR(); | ||
expect(res).contains("Hello foo bar"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const xclap = require("xclap"); | ||
xclap.load({ | ||
minify: xclap.exec("babel src -d dist --source-maps", { | ||
execOptions: { env: { MINIFY: "true" } } | ||
}) | ||
}); | ||
require("electrode-archetype-njs-module-dev")(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
browser | ||
dist | ||
*-lock.* |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Purposeful code block? I more frequently see
---
at the end of the License section.