You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8" /><linkrel="icon" href="%PUBLIC_URL%/favicon.ico" /><metaname="viewport" content="width=device-width, initial-scale=1" /><metaname="theme-color" content="#000000" /><metaname="description"
content="Web site created using create-react-app"
/><linkrel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /><!-- manifest.json provides metadata used when your web app is installed on a user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ --><linkrel="manifest" href="%PUBLIC_URL%/manifest.json" /><!-- Notice the use of %PUBLIC_URL% in the tags above. It will be replaced with the URL of the `public` folder during the build. Only files inside the `public` folder can be referenced from the HTML. Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will work correctly both with client-side routing and a non-root public URL. Learn how to configure a non-root public URL by running `npm run build`. --><title>React App</title></head><body><noscript>You need to enable JavaScript to run this app.</noscript><divid="root"></div><!-- This HTML file is a template. If you open it directly in the browser, you will see an empty page. You can add webfonts, meta tags, or analytics to this file. The build step will place the bundled scripts into the <body> tag. To begin the development, run `npm start` or `yarn start`. To create a production bundle, use `npm run build` or `yarn build`. --></body></html>
基于 create-react-app 的多页面配置 (MPA)
引用文章:基于create-react-app的多页面配置
✅ 创建 React 项目
index.tsx
和App.tsx
放入 pages 文件夹中react-app-env.d.ts
,否则在解析一些*.scss
文件路径时会出错。✅ 多页面应用配置
🔆 弹出webpack配置
由于create-react-app隐藏了webpack配置,因此首先需要将该项目的webpack配置暴露出来。运行如下命令:
npm run eject
yarn eject
🔆 创建多页面文件
假设该多页面应用程序包括前台展示页面和后台管理页面。则在
src/pages/
目录下下创建两个文件夹app
和admin
,分别对应两个页面:然后对应这两个页面,还需要在
public
目录下创建对应的 html 模板:🔆 修改打包相关配置
❇️ 首先修改文件路径映射
来到
config/paths.js
文件下,在module.exports
中,修改appIndexJs
的文件路径,添加adminHtml
和adminIndexJs
的文件路径❇️ 修改 webpack 中的配置
来到
config/webpack.config.js
文件中:首先修改entry入口配置:
其次修改output出口配置:
最后修改plugin插件配置:
💡 HtmlWebpackPlugin这个plugin曝光率很高,他主要有两个作用:
🔆 修改启动脚本配置
在启动脚本代码里,他会检查 html 模板文件、多页面的入口文件的路径映射是否存在。由于修改了路径映射文件,因此也需要对启动脚本代码进行修改。
分别来到
scripts/build.js
scripts/start.js
找到如下代码并进行修改。
至此,多页面的相关配置已修改完毕,运行
yarn start
启动项目,并输入对应的 url 查看。💡 总结
配置多页面应用的步骤如下:
[name].bundle.js
,其中name为entry中的key;The text was updated successfully, but these errors were encountered: