0

I am trying to reduce the size of my reactjs application bundle.js file. For a relatively simple app i have its browserified js file size - 452Kb.

To build it, i use NPM with respective package.json file:

   {
      "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.

For the time being i tried few things: app.js file has a line of code setting NODE_ENV variable to 'production'

process.env.NODE_ENV = 'production';

A second thing i have tried is to use npm prune command with --production parameter. 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. What can i do?

4

1 回答 1

0

为了减轻用户的下载负担,你可以做的一件事是从 CDN 获取reactreact-dom,而不是将它们放在你的包中。

将它们从package.json的依赖项部分移动到新的部分peerDependencies请参阅 npm 文档)。

然后加标签到您的 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>

→ 参见cdnjs.com 上的 React

然后您需要更改 Browserify 配置以将reactreact-dom识别为外部模块。

→ 查看StackOverflow 上的相关问题

于 2016-10-13T12:14:51.087 回答