-
Notifications
You must be signed in to change notification settings - Fork 24
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
Add ability to load custom Experiment Engine UI #101
Merged
krithika369
merged 8 commits into
caraml-dev:main
from
krithika369:exp_engine_module_federation
Sep 30, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8ac3a06
Add sample module federation for list experiments view
47e93f0
Include remote custom experiment engine views
fcc6449
Merge from master
7618587
Add dynamic loading of remote component
03eafce
Add cache busting for remote script file
0e9cf2c
Address PR comments
c0c2060
Bump up prettier version in deps and update the formatting flag
6c37a8a
Merge from main
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
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,3 +1,3 @@ | ||
REACT_APP_ENVIRONMENT=dev | ||
|
||
REACT_APP_TURING_API=http://localhost:8080/v1 | ||
REACT_APP_TURING_API=http://localhost:8080/v1 |
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,42 @@ | ||
const { ModuleFederationPlugin } = require("webpack").container; | ||
// Remove source code dependency react-lazylog which causes problems with sharing | ||
const { ["react-lazylog"]: undefined, ...sharedDeps } = require("./package.json").dependencies; | ||
|
||
module.exports = ({ env }) => ({ | ||
plugins: [ | ||
{ | ||
plugin: { | ||
overrideWebpackConfig: ({ webpackConfig }) => { | ||
// Suppress source-map related warnings (currently, react-use-dimensions is problematic). | ||
// This is setting was applied by CRA4, but CRA5 doesn't. | ||
webpackConfig.ignoreWarnings = [ | ||
...(webpackConfig.ignoreWarnings || []), | ||
/Failed to parse source map/, | ||
]; | ||
return webpackConfig; | ||
}, | ||
}, | ||
} | ||
], | ||
webpack: { | ||
plugins: { | ||
add: [ | ||
new ModuleFederationPlugin({ | ||
name: "turing", | ||
shared: { | ||
...sharedDeps, | ||
react: { | ||
shareScope: "default", | ||
singleton: true, | ||
requiredVersion: sharedDeps.react, | ||
}, | ||
"react-dom": { | ||
singleton: true, | ||
requiredVersion: sharedDeps["react-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
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,33 @@ | ||
import React from "react"; | ||
romanwozniak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import ReactDOM from "react-dom"; | ||
import "./assets/style.scss"; | ||
import * as serviceWorker from "./serviceWorker"; | ||
import App from "./App"; | ||
import * as Sentry from "@sentry/browser"; | ||
import { ConfigProvider, useConfig } from "./config"; | ||
|
||
const SentryApp = ({ children }) => { | ||
const { | ||
sentryConfig: { dsn, environment, tags }, | ||
} = useConfig(); | ||
|
||
Sentry.init({ dsn, environment }); | ||
Sentry.setTags(tags); | ||
|
||
return children; | ||
}; | ||
|
||
const TuringUI = () => ( | ||
<ConfigProvider> | ||
<SentryApp> | ||
<App /> | ||
</SentryApp> | ||
</ConfigProvider> | ||
); | ||
|
||
ReactDOM.render(TuringUI(), document.getElementById("root")); | ||
|
||
// If you want your app to work offline and load faster, you can change | ||
// unregister() to register() below. Note this comes with some pitfalls. | ||
// Learn more about service workers: https://bit.ly/CRA-PWA | ||
serviceWorker.unregister(); |
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,67 @@ | ||
import React from "react"; | ||
import { | ||
EuiPage, | ||
EuiPageBody, | ||
EuiPageContent, | ||
EuiPageHeader, | ||
EuiPageHeaderSection, | ||
EuiText, | ||
} from "@elastic/eui"; | ||
import { PageTitle } from "../components/page/PageTitle"; | ||
|
||
import useDynamicScript from "../hooks/useDynamicScript"; | ||
import loadComponent from "../utils/remoteComponent"; | ||
import { useConfig } from "../config"; | ||
|
||
const FallbackView = ({ text }) => ( | ||
<EuiPage> | ||
<EuiPageBody> | ||
<EuiPageHeader> | ||
<EuiPageHeaderSection> | ||
<PageTitle title="Experiments" /> | ||
</EuiPageHeaderSection> | ||
</EuiPageHeader> | ||
<EuiPageContent> | ||
<EuiText size="s" color="subdued"> | ||
{text} | ||
</EuiText> | ||
</EuiPageContent> | ||
</EuiPageBody> | ||
</EuiPage> | ||
); | ||
|
||
const RemoteRouter = ({ projectId }) => { | ||
const { defaultExperimentEngine } = useConfig(); | ||
|
||
// Retrieve script from host dynamically | ||
const { ready, failed } = useDynamicScript({ | ||
url: defaultExperimentEngine.url, | ||
romanwozniak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
if (!ready || failed) { | ||
const text = !ready | ||
? "Loading Experiment Engine ..." | ||
: "Failed to load Experiment Engine"; | ||
return <FallbackView text={text} />; | ||
} | ||
|
||
// Load component from remote host | ||
const ExperimentsLandingPage = React.lazy( | ||
loadComponent(defaultExperimentEngine.name, "./ExperimentsLandingPage") | ||
); | ||
|
||
return ( | ||
<React.Suspense fallback={<FallbackView text="Loading Experiments" />}> | ||
<ExperimentsLandingPage projectId={projectId} /> | ||
</React.Suspense> | ||
); | ||
}; | ||
|
||
export const ExperimentsRouter = ({ projectId }) => { | ||
const { defaultExperimentEngine } = useConfig(); | ||
return !!defaultExperimentEngine.url ? ( | ||
<RemoteRouter projectId /> | ||
) : ( | ||
<FallbackView text="No default Experiment Engine configured" /> | ||
); | ||
}; |
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,43 @@ | ||
import React from "react"; | ||
|
||
// Ref: | ||
// https://github.com/module-federation/module-federation-examples/blob/master/dynamic-system-host | ||
const useDynamicScript = (args) => { | ||
const [ready, setReady] = React.useState(false); | ||
const [failed, setFailed] = React.useState(false); | ||
|
||
React.useEffect(() => { | ||
const element = document.createElement("script"); | ||
|
||
// Append query string to prevent serving the cached version of the file | ||
element.src = args.url + "?" + Math.floor(Date.now() / 1000); | ||
element.type = "text/javascript"; | ||
element.async = true; | ||
|
||
setReady(false); | ||
setFailed(false); | ||
|
||
element.onload = () => { | ||
setReady(true); | ||
}; | ||
|
||
element.onerror = () => { | ||
console.error(`Dynamic Script Error: ${args.url}`); | ||
setReady(false); | ||
setFailed(true); | ||
}; | ||
|
||
document.head.appendChild(element); | ||
|
||
return () => { | ||
document.head.removeChild(element); | ||
}; | ||
}, [args.url]); | ||
|
||
return { | ||
ready, | ||
failed, | ||
}; | ||
}; | ||
|
||
export default useDynamicScript; |
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,33 +1 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import "./assets/style.scss"; | ||
import * as serviceWorker from "./serviceWorker"; | ||
import App from "./App"; | ||
import * as Sentry from "@sentry/browser"; | ||
import { ConfigProvider, useConfig } from "./config"; | ||
|
||
const SentryApp = ({ children }) => { | ||
const { | ||
sentryConfig: { dsn, environment, tags }, | ||
} = useConfig(); | ||
|
||
Sentry.init({ dsn, environment }); | ||
Sentry.setTags(tags); | ||
|
||
return children; | ||
}; | ||
|
||
const TuringUI = () => ( | ||
<ConfigProvider> | ||
<SentryApp> | ||
<App /> | ||
</SentryApp> | ||
</ConfigProvider> | ||
); | ||
|
||
ReactDOM.render(TuringUI(), document.getElementById("root")); | ||
|
||
// If you want your app to work offline and load faster, you can change | ||
// unregister() to register() below. Note this comes with some pitfalls. | ||
// Learn more about service workers: https://bit.ly/CRA-PWA | ||
serviceWorker.unregister(); | ||
import("./bootstrap"); |
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,25 +1,40 @@ | ||
const proxy = require("http-proxy-middleware"); | ||
const { createProxyMiddleware } = require("http-proxy-middleware"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
module.exports = function (app) { | ||
// Proxy settings required for remote components' API calls. | ||
const remoteProxyPaths = process.env.REMOTE_COMPONENTS_PROXY_PATHS | ||
? JSON.parse(process.env.REMOTE_COMPONENTS_PROXY_PATHS) | ||
: {}; | ||
Object.keys(remoteProxyPaths).forEach((key) => { | ||
app.use( | ||
key, | ||
createProxyMiddleware({ | ||
target: remoteProxyPaths[key], | ||
pathRewrite: { ["^" + key]: "" }, | ||
changeOrigin: true, | ||
}) | ||
); | ||
}); | ||
|
||
app.use( | ||
"/api/mlp", | ||
proxy({ | ||
createProxyMiddleware({ | ||
target: process.env.REACT_APP_MLP_API, | ||
pathRewrite: { "^/api/mlp": "" }, | ||
changeOrigin: true, | ||
}) | ||
); | ||
app.use( | ||
"/api/merlin", | ||
proxy({ | ||
createProxyMiddleware({ | ||
target: process.env.REACT_APP_MERLIN_API, | ||
pathRewrite: { "^/api/merlin": "" }, | ||
changeOrigin: true, | ||
}) | ||
); | ||
app.use( | ||
"/api/turing", | ||
proxy({ | ||
createProxyMiddleware({ | ||
target: process.env.REACT_APP_TURING_API, | ||
pathRewrite: { "^/api/turing": "" }, | ||
changeOrigin: true, | ||
|
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.
Context: facebook/create-react-app#11278 (comment)
Currently, this will lead to a warning on
yarn install
: