简体   繁体   English

捆绑的ReactJS应用的大小

[英]Size of bundled ReactJS app

I am trying to reduce the size of my reactjs application bundle.js file. 我试图减小我的reactjs应用程序bundle.js文件的大小。 For a relatively simple app i have its browserified js file size - 452Kb. 对于一个相对简单的应用程序,我具有其浏览器化的js文件大小-452Kb。

To build it, i use NPM with respective package.json file: 要构建它,我将NPM与相应的package.json文件一起使用:

   {
      "name": "MyApp-React",
      "version": "0.0.1",
      "description": "My App Description",
      "main": "app.js",
      "scripts": {
        "watch": "watchify app.js -o public/js/bundle.js -v",
        "browserify": "browserify app.js | uglifyjs > public/js/bundle.js",
        "build": "npm run browserify",
        "start": "npm install"
      },
      "author": "",
      "dependencies": {
        "axios": "^0.14.0",
        "body-parser": "^1.15.2",
        "clipboard": "^1.5.12",
        "express": "~4.9.7",
        "express-handlebars": "~1.1.0",
        "express-session": "^1.14.1",
        "highlight.js": "^9.7.0",
        "node-jsx": "~0.12.4",
        "react": "~15.3.1",
        "react-dom": "^15.3.1",
        "react-maskedinput": "^3.2.4",
        "react-modal": "^1.4.0",
        "react-router": "^2.8.1"
      },
      "devDependencies": {
        "browserify": "~6.0.3",
        "nodemon": "^1.2.1",
        "reactify": "~1.1.1",
        "uglify-js": "~2.4.15",
        "watchify": "^3.1.1"
      },
      "browserify": {
        "debug": false,
        "transform": [
          "reactify"
        ]
      }
    }

Not being an expert in Node JS/ ReactJS development i hope i do somethin wrong, but i cant find what. 我不是Node JS / ReactJS开发方面的专家,我希望我做错了什么,但我找不到。

For the time being i tried few things: app.js file has a line of code setting NODE_ENV variable to 'production' 我暂时尝试了一些事情:app.js文件中有一行代码,将NODE_ENV变量设置为'production'

process.env.NODE_ENV = 'production';

A second thing i have tried is to use npm prune command with --production parameter. 我尝试过的第二件事是将npm prune命令与--production参数一起使用。 Having this run, i see it removes a lot from node_modules folder, but then npm run build is failing to run as it misses devDependencies. 有了这个运行,我看到它从node_modules文件夹中删除了很多东西,但是随后npm run build无法运行,因为它错过了devDependencies。 What can i do? 我能做什么?

One thing that you could do to lighten the download burden for your users is get react and react-dom from a CDN instead of putting them in your bundle. 您可以为用户减轻下载负担的一件事是从CDN获得反应反应主导 ,而不是将其放入捆绑包中。

Move them from the dependencies section of your package.json to a new section peerDependencies ( see npm docs ). 将它们从package.jsondependencies部分移到新的peerDependencies部分( 请参阅npm docs )。

Then add 然后加 tags to your HTML code for loading React and ReactDOM like so: 标记为HTML代码以加载React和ReactDOM,如下所示:

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.min.js"></script>

→ See React on cdnjs.com →请参阅cdnjs.com上的React

Then you need to change your Browserify config to recognize react and react-dom as external modules. 然后,您需要更改Browserify配置,以将reactreact-dom识别为外部模块。

→ See related question on StackOverflow →请参阅有关StackOverflow的相关问题

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

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