|
1 | 1 | # Handle Third-party Dependencies |
| 2 | + |
| 3 | +Generally, third-party dependencies required by a project can be installed via the `install` command in the package manager. After the third-party dependencies are successfully installed, they will generally appear under `dependencies` and `devDependencies` in the project `package.json`. |
| 4 | + |
| 5 | +```json title="package.json" |
| 6 | +{ |
| 7 | + "dependencies": {}, |
| 8 | + "devDependencies": {} |
| 9 | +} |
| 10 | +``` |
| 11 | + |
| 12 | +Dependencies under `"dependencies"` are generally related to project code and builds, and if these third-party dependencies are declared under `"devDependencies"`, then there will be missing dependencies in production runtime. |
| 13 | + |
| 14 | +In addition to `"dependencies"`, `"peerDependencies"`can also declare dependencies that are needed in the production environment, but it puts more emphasis on the existence of these dependencies declared by `"peerDependencies"` in the project's runtime environment, similar to the plugin mechanism. |
| 15 | + |
| 16 | +## Default handling of third-party dependencies |
| 17 | + |
| 18 | +By default, **third-party dependencies under `"dependencies"`, `"optionalDependencies"` and `"peerDependencies"` are not bundled by Rslib**. |
| 19 | + |
| 20 | +This is because when the npm package is installed, its `"dependencies"` will also be installed. By not packaging `"dependencies"`, you can reduce the size of the package product. |
| 21 | + |
| 22 | +If you need to package some dependencies, it is recommended to move them from `"dependencies"` to `"devDependencies"`, which is equivalent to **prebundle** the dependencies and reduces the size of the dependency installation. |
| 23 | + |
| 24 | +### Example |
| 25 | + |
| 26 | +If the project has a dependency on `react`. |
| 27 | + |
| 28 | +```json title="package.json" |
| 29 | +{ |
| 30 | + "dependencies": { |
| 31 | + "react": "^18" |
| 32 | + }, |
| 33 | + // or |
| 34 | + "peerDependencies": { |
| 35 | + "react": "^18" |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +When a `react` dependency is used in the source code: |
| 41 | + |
| 42 | +```tsx title="src/index.ts" |
| 43 | +import React from 'react'; |
| 44 | +console.info(React); |
| 45 | +``` |
| 46 | + |
| 47 | +The `react` code will not be bundled into the output: |
| 48 | + |
| 49 | +```js title="dist/index.js" |
| 50 | +import * as __WEBPACK_EXTERNAL_MODULE_react__ from 'react'; |
| 51 | +console.info(__WEBPACK_EXTERNAL_MODULE_react__['default']); |
| 52 | +``` |
| 53 | + |
| 54 | +If you want to modify the default processing, you can use the following API. |
| 55 | + |
| 56 | +- [`lib.autoExternal`](/config/lib/auto-external) |
| 57 | + |
| 58 | +## Exclude specified third-party dependencies |
| 59 | + |
| 60 | +We previously introduced the use of [`lib.autoExternal`](/config/lib/auto-external). This configuration lets you manage third-party dependencies more precisely. |
| 61 | + |
| 62 | +For example, when we need to leave only certain dependencies unbundled, we can configure it as follows. |
| 63 | + |
| 64 | +:::tip |
| 65 | +In this case, some dependencies may not be suitable for bundling. If so, you can handle it as follows. |
| 66 | +::: |
| 67 | + |
| 68 | +```ts |
| 69 | +export default defineConfig({ |
| 70 | + lib: [ |
| 71 | + { |
| 72 | + // ... |
| 73 | + autoExternal: true, |
| 74 | + output: { |
| 75 | + externals: ['pkg-1', /pkg-2/], |
| 76 | + }, |
| 77 | + // ... |
| 78 | + }, |
| 79 | + ], |
| 80 | +}); |
| 81 | +``` |
0 commit comments