Search code examples
reactjswebpackbundle

react-scripts and bundle - Are all the 'require' and 'dependencies' added into the final bundle?


I am working in a React project that is using react-scripts in its version 3.4.4 among other dependencies and I have to check all the third-party libraries added into the final bundle.

As example, if I check the requires and dependencies from react-scripts in the package-lock.json file:

"react-scripts": {
      "version": "3.4.4",
      "resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-3.4.4.tgz",
      "integrity": "sha512-7J7GZyF/QvZkKAZLneiOIhHozvOMHey7hO9cdO9u68jjhGZlI8hDdOm6UyuHofn6Ajc9Uji5I6Psm/nKNuWdyw==",
      "requires": {
        "@babel/core": "7.9.0",
        "@svgr/webpack": "4.3.3",
        "@typescript-eslint/eslint-plugin": "^2.10.0",
        "@typescript-eslint/parser": "^2.10.0",
        "babel-eslint": "10.1.0",
        "babel-jest": "^24.9.0",
        "babel-loader": "8.1.0",
        "babel-plugin-named-asset-import": "^0.3.6",
        "babel-preset-react-app": "^9.1.2",
        "camelcase": "^5.3.1",
        "case-sensitive-paths-webpack-plugin": "2.3.0",
        "css-loader": "3.4.2",
        "dotenv": "8.2.0",
        "dotenv-expand": "5.1.0",
        "eslint": "^6.6.0",
        "eslint-config-react-app": "^5.2.1",
        "eslint-loader": "3.0.3",
        "eslint-plugin-flowtype": "4.6.0",
        "eslint-plugin-import": "2.20.1",
        "eslint-plugin-jsx-a11y": "6.2.3",
        "eslint-plugin-react": "7.19.0",
        "eslint-plugin-react-hooks": "^1.6.1",
        "file-loader": "4.3.0",
        "fs-extra": "^8.1.0",
        "fsevents": "2.1.2",
        "html-webpack-plugin": "4.0.0-beta.11",
        "identity-obj-proxy": "3.0.0",
        "jest": "24.9.0",
        "jest-environment-jsdom-fourteen": "1.0.1",
        "jest-resolve": "24.9.0",
        "jest-watch-typeahead": "0.4.2",
        "mini-css-extract-plugin": "0.9.0",
        "optimize-css-assets-webpack-plugin": "5.0.3",
        "pnp-webpack-plugin": "1.6.4",
        "postcss-flexbugs-fixes": "4.1.0",
        "postcss-loader": "3.0.0",
        "postcss-normalize": "8.0.1",
        "postcss-preset-env": "6.7.0",
        "postcss-safe-parser": "4.0.1",
        "react-app-polyfill": "^1.0.6",
        "react-dev-utils": "^10.2.1",
        "resolve": "1.15.0",
        "resolve-url-loader": "3.1.2",
        "sass-loader": "8.0.2",
        "semver": "6.3.0",
        "style-loader": "0.23.1",
        "terser-webpack-plugin": "2.3.8",
        "ts-pnp": "1.1.6",
        "url-loader": "2.3.0",
        "webpack": "4.42.0",
        "webpack-dev-server": "3.11.0",
        "webpack-manifest-plugin": "2.2.0",
        "workbox-webpack-plugin": "4.3.1"
      },
      "dependencies": {
        "fs-extra": {
          "version": "8.1.0",
          "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz",
          "integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==",
          "requires": {
            "graceful-fs": "^4.2.0",
            "jsonfile": "^4.0.0",
            "universalify": "^0.1.0"
          }
        },
        "semver": {
          "version": "6.3.0",
          "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz",
          "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw=="
        }
      }
    },

My question is, are all the requires and dependencies such, as example, webpack-dev-server, dotenv, sass-loader added into the final bundle?

Thanks!


Solution

  • No. What Webpack ends up including is not something published or reported. Using react-scripts alone would seen hundreds of modules and versions being shipped in production. Any library you add on top just adds to that weight.

    With tree shaking and build deps, you can't rely upon that requires at all. Some of those, like Jest or ESLint, are dev-only. They have no runtime. Others will. Some runtime deps will be shaken out too, so can't rely on just recognizing the lib.