简体   繁体   English

npm 运行构建不会缩小 reactJs 项目

[英]npm run build is not minifying the reactJs project

I executed the npm command "npm run build" from the reactJs App root folder and "build' folder is generated with the below output in the console.我从 reactJs App 根文件夹中执行了 npm 命令“npm run build”,并在控制台中使用下面的 output 生成了“build”文件夹。

    File sizes after gzip:

  646.8 KB  build\static\js\2.d370d4e1.chunk.js
  12.46 KB  build\static\js\main.fec451dd.chunk.js
  823 B     build\static\css\main.fc109ae9.chunk.css
  772 B     build\static\js\runtime-main.83c3e0c4.js

The project was built assuming it is hosted at /.
You can control this with the homepage field in your package.json.

The build folder is ready to be deployed.
You may serve it with a static server:

  serve -s build

Then I executed the command "serve -s build" and got the below output in the console.然后我执行命令“serve -s build”,在控制台中得到了下面的 output。

INFO: Accepting connections at http://localhost:5000

I could see all the *.tsx files not minified in developer tools.我可以看到所有 *.tsx 文件在开发者工具中都没有被缩小。 Not sure what am I doing wrong and why the files in the below mentioned "js" folder are not minified.不知道我做错了什么以及为什么下面提到的“js”文件夹中的文件没有被缩小。 在此处输入图像描述

package.json package.json

 {
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^4.11.0",
    "@material-ui/icons": "^4.9.1",
    "@material-ui/lab": "^4.0.0-alpha.56",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "@types/jest": "^24.9.1",
    "@types/node": "^12.12.68",
    "@types/react": "^16.9.53",
    "@types/react-dom": "^16.9.8",
    "node": "^15.4.0",
    "react": "^16.14.0",
    "react-dom": "^16.14.0",
    "react-scripts": "3.4.3",
    "typescript": "^3.7.5",
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "prod": "webpack --mode production"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "ts-loader": "^8.0.12",
    "typescript": "^3.7.5",
    "webpack-cli": "^4.3.0"
  }
}

The setup is obviously right.设置显然是正确的。 The thing you have seen was just the sourcemap which is to map to the source code (for debug purpose) that's why you can search under browser devtool.您所看到的只是sourcemap到源代码(用于调试目的)的源映射,这就是您可以在浏览器 devtool 下搜索的原因。

Basically sourcemap is generated by default setup of cra in webpack configuration file.基本上sourcemap是在webpack配置文件中默认设置cra生成的。

However, if you wish to stop generating it, you can do it via CLI:但是,如果您希望停止生成它,可以通过 CLI 进行:

package.json

{
  "scripts": {
    // ...
   "build": "GENERATE_SOURCEMAP=false react-scripts build",
    // In case of using Windows:
    "build": "set \"GENERATE_SOURCEMAP=false\" && react-scripts build",
  }
}

Then you would no longer see you code under devtool.然后你将不再看到你在 devtool 下的代码。

Install the React Developer Tools extension.安装 React 开发者工具扩展。 Hover over the extension if it says you are using production build then your files are minified. Hover 超过扩展如果它说你正在使用生产版本,那么你的文件就会被缩小。 As you can see the image below.如下所示。 Also read about production build from official docs from react.还可以从 react 的官方文档中阅读有关生产构建的信息。

https://reactjs.org/docs/optimizing-performance.html https://reactjs.org/docs/optimizing-performance.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM