为什么在使用create-react-app的生产版本中丢失Bootstrap样式?

时间:2019-05-24 22:56:28

标签: css node.js reactjs create-react-app react-bootstrap

当运行npm run build并将我的react项目部署到netlify时,我网站的netlify版本丢失了一些自举样式...这是本地主机版本的图像: http://i.imgur.com/gZoqkY1.png

,这是netlify版本的链接: https://ncnews-timdowd.netlify.com/

我已确保按照react-bootstrap,bootstrap和CRA文档中的指示,在index.html中添加相关的样式表链接,并将相关的导入项添加到index.js中,而不是真正确定出了什么问题。我发现如果删除该行:

导入'bootstrap / dist / css / bootstrap.css'

从我的index.js并在本地运行,我在本地版本中遇到的问题与在netlify版本中遇到的问题完全相同,因此这肯定与该引导程序样式表有关,而没有使其部署...

如果您想查看我的github仓库: https://github.com/timothydowd/FE-NC-News

package.json显示引导程序和react-bootstrap版本:

{
  "name": "front-end-nc-news",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.17",
    "@fortawesome/free-solid-svg-icons": "^5.8.1",
    "@fortawesome/react-fontawesome": "^0.1.4",
    "@reach/router": "^1.2.1",
    "axios": "^0.18.0",
    "bootstrap": "^4.3.1",
    "css-loader": "^2.1.1",
    "mdbreact": "^4.13.0",
    "react": "^16.8.6",
    "react-bootstrap": "^1.0.0-beta.8",
    "react-dom": "^16.8.6",
    "react-scripts": "2.1.8",
    "style-loader": "^0.23.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {}
}

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import 'bootstrap/dist/css/bootstrap.css';
import './index.css';


ReactDOM.render(<App />, document.getElementById('root'));

index.html中的头:

<head>
    <meta charset="utf-8" />
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />


    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    <meta name="theme-color" content="#000000" />

    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />

    <title>NC NEWS</title>
    <link
      rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
      integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
      crossorigin="anonymous"
    />
  </head>

如前所述。.

预期的结果是我在开发服务器中看到的:http://i.imgur.com/gZoqkY1.png

可以在我部署的构建中看到实际结果,但缺少样式: https://ncnews-timdowd.netlify.com/

1 个答案:

答案 0 :(得分:0)

问题在于cra构建CSS的顺序与其在开发环境中编译CSS的方式相比。通过修改app.css文件并删除一些不必要的CSS来解决此问题。