reactjs - 部署后未加载 Ant Design
问题描述
我构建了一个应用ant设计的react项目,官方文档为react-app-rewired。Css 在 之后加载得很好yarn start
,但是 afteryarn build
没有加载。我使用 Docker 和 Nginx 进行部署。其他组件运行良好,但只有蚂蚁设计被破坏并出现在屏幕上。
- 配置覆盖.js
const { override, fixBabelImports } = require('customize-cra');
module.exports = override(
fixBabelImports('import', {
libraryName: 'antd',
libraryDirectory: 'es',
style: 'css',
}),
);
- tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"noImplicitAny": false,
"charset": "utf8"
},
"include": ["src"],
"exclude": ["./node_modules/**/*"]
}
- 包.json
...
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
},
...
"devDependencies": {
"@types/react-router-dom": "^5.1.7",
"babel-plugin-import": "^1.13.3",
"customize-cra": "^1.0.0",
"react-app-rewired": "^2.1.8"
}
...
- Dockerfile
FROM node:12 as builder
RUN mkdir /app
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY ./package*.json ./
COPY ./yarn.lock ./
RUN apt-get update
RUN apt install yarn -y
RUN yarn global add react-scripts@4.0.2
RUN yarn install
COPY . .
RUN yarn run build
# Production build
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY ./nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
解决方案
这是Nginx的问题。我使用 Nginx docker 容器来部署带有自定义的 React 构建项目nginx.conf
。它无法通过添加配置来提供具有 mime 类型的 css 文件include mime.types
。
worker_processes auto;
events { worker_connections 1024; }
http {
include mime.types; # MUST BE ADDED
sendfile on;
server {
listen 80;
推荐阅读
- autodesk-forge - Forge 查看器在 2D 视图中显示模糊的元素,并且 Measure 扩展对它们不起作用
- angular - Ionic - Angular 9:无法消化 Swiper 回调
- python - 用户使用 Flask python 登录后显示注销按钮
- javascript - 道具React发生变化时如何渲染组件?
- python - Jinja 中的变量 - Flask
- xml - 如何使用 xidel 从 XML 中提取数据
- javascript - 使用带有故事书的全局 scss
- python - 两个 Topos() 位置之间的路径:确定达到给定高度的纬度和经度
- python - 表格打印为空格,python
- performance - 提高 Neo4j Cypher 查询性能