alibaba / module-federation4

Webpack5 module federation for Webpack4
https://github.com/webpack/webpack/issues/10352
BSD 3-Clause "New" or "Revised" License
54 stars 4 forks source link

在create-react-app中使用:react-dom.development.js:12000 Uncaught TypeError: Cannot read property '_federation_website2' of undefined #3

Open flyyuan opened 4 years ago

flyyuan commented 4 years ago

Uncaught TypeError: Cannot read property '_federation_website2' of undefined at remote "Title":3 at Object.website2/Title (remote "Title":5) at __webpack_require__ (bootstrap:790) at fn (bootstrap:150) at fn.t (bootstrap:194)

addWebpackPlugin( new ModuleFederationPlugin({ remotes: { website2: '_federation_website2', }, }), ), `const Title = lazy(() => import('website2/Title').then(mod => mod.default))

const HomeComp = () => (

loading
}> </Suspense> </div> <p>)`</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/xcodebuild"><img src="https://avatars.githubusercontent.com/u/5436704?v=4" />xcodebuild</a> commented <strong> 4 years ago</strong> </div> <div class="markdown-body"> <p>Thanks for reporting issue, could you provide a repo to reproduce this problem?</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/flyyuan"><img src="https://avatars.githubusercontent.com/u/19996552?v=4" />flyyuan</a> commented <strong> 4 years ago</strong> </div> <div class="markdown-body"> <p><a href="https://github.com/flyyuan/cra-module-federation4">https://github.com/flyyuan/cra-module-federation4</a></p> <p>1.Add config-overrides.js in the root directory</p> <pre><code class="language-js">const { override,addWebpackPlugin, } = require('customize-cra'); const ModuleFederationPlugin = require('webpack-plugin-module-federation') module.exports = { webpack: override( addWebpackPlugin( new ModuleFederationPlugin({ remotes: { website2: '_federation_website2', }, }), ), ) };</code></pre> <p>2.Override optimization for cra-federation4/node_modules/react-scripts/config/webpack.config.js</p> <pre><code class="language-js">optimization: { minimize: false, },</code></pre> <p>3.cra-federation4/public/index.html</p> <pre><code class="language-html"><!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="theme-color" content="#000000" /> <meta name="description" content="Web site created using create-react-app" /> <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> <title>React App</title> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div id="root"></div> <script src="http://localhost:3002/remoteEntry.js"></script> </body> </html></code></pre> <p>cra-federation4/src/App.tsx</p> <pre><code class="language-tsx">import React, { lazy, Suspense } from "react"; import logo from "./logo.svg"; import "./App.css"; const Title = lazy(() => import("website2/Title").then((mod) => mod.default)); function App() { return ( <div className="App"> <header className="App-header"> <img src={logo} className="App-logo" alt="logo" /> <Suspense fallback={<div>loading</div>}> <Title /> </Suspense> <p> Edit <code>src/App.tsx</code> and save to reload. </p> <a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer" > Learn React </a> </header> </div> ); } export default App; </code></pre> <p>cra-federation4\src\website2.d.ts</p> <pre><code class="language-ts">declare module "website2/Title"{ const Title: React.ComponentType<{},any>; export default Title; }</code></pre> <p>4.start cra-federation4 and module-federation4\examples\website2</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/wuweikd"><img src="https://avatars.githubusercontent.com/u/34428646?v=4" />wuweikd</a> commented <strong> 4 years ago</strong> </div> <div class="markdown-body"> <p>I have the same problem , maybe it does not work well in 'create-react-app'。Or it can't handle remote modules</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/ScriptedAlchemy"><img src="https://avatars.githubusercontent.com/u/25274700?v=4" />ScriptedAlchemy</a> commented <strong> 4 years ago</strong> </div> <div class="markdown-body"> <p>Cra uses runtimeChunk:single. Requiring additional steps to be take </p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/dmitrykuznetsovdev"><img src="https://avatars.githubusercontent.com/u/897840?v=4" />dmitrykuznetsovdev</a> commented <strong> 4 years ago</strong> </div> <div class="markdown-body"> <pre><code>// change react-scripts settings function patchConfig(conf) { const config = {...conf}; config.output.publicPath = packagejson.homepage; // It is very important config.output.globalObject = 'window'; delete config.optimization.runtimeChunk; return config; }</code></pre> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/ScriptedAlchemy"><img src="https://avatars.githubusercontent.com/u/25274700?v=4" />ScriptedAlchemy</a> commented <strong> 4 years ago</strong> </div> <div class="markdown-body"> <p>You may also want to investigate sidecar builds in webpack 5</p> </div> </div> <div class="page-bar-simple"> </div> <div class="footer"> <ul class="body"> <li>© <script> document.write(new Date().getFullYear()) </script> Githubissues.</li> <li>Githubissues is a development platform for aggregating issues.</li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script> <script src="/githubissues/assets/js.js"></script> <script src="/githubissues/assets/markdown.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/go.min.js"></script> <script> hljs.highlightAll(); </script> </body> </html>