|
| 1 | +# Output Format |
| 2 | + |
| 3 | +This sets the output format for the generated JavaScript files. There are three supported values now: `esm`, `cjs`, and `umd`. If you do not specify an output format, esbuild will choose one for you when bundling is enabled. If bundling is disabled, no format conversion will occur. |
| 4 | + |
| 5 | +{/* The library has different build methods and options. You can choose a proper build method based on the scenario. In these section, we will introduce some common build patterns and when to use them. */} |
| 6 | + |
| 7 | +## ESM / CJS |
| 8 | + |
| 9 | +Library authors need to carefully consider which module formats to support. Let's understand ESM (ECMAScript Modules) and CJS (CommonJS) and when to use them. |
| 10 | + |
| 11 | +### What are ESM and CJS? |
| 12 | + |
| 13 | +- ESM is a modern module system introduced in ES2015 that allows JavaScript code to be organized into reusable, self-contained modules. ESM is now the standard for both [browser](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and [Node.js](https://nodejs.org/api/esm.html) environments, replacing older module systems like [CommonJS (CJS)](https://nodejs.org/api/modules.html) and [AMD](https://requirejs.org/docs/whyamd.html). |
| 14 | + |
| 15 | +- [CommonJS](https://nodejs.org/api/modules.html#modules-commonjs-modules) is a module system- used in JavaScript, particularly in server-side environments like Node.js. It was created to allow JavaScript to be used outside of the browser by providing a way to manage modules and dependencies. |
| 16 | + |
| 17 | +### What is the dilemma of esm / cjs |
| 18 | + |
| 19 | +### When to support which format? |
| 20 | + |
| 21 | +For modern libraries, it's recommended to: |
| 22 | + |
| 23 | +1. **Default to ESM** |
| 24 | + |
| 25 | + - ESM is the future of JavaScript modules |
| 26 | + - Better tree-shaking support |
| 27 | + - Native browser support |
| 28 | + - Top-level await support |
| 29 | + |
| 30 | +2. **Consider CJS if:** |
| 31 | + |
| 32 | + - Supporting older Node.js versions (`<12.x`) |
| 33 | + - Supporting tools/frameworks that don't fully support ESM |
| 34 | + - Having users with CommonJS-only dependencies |
| 35 | + |
| 36 | +3. **Support both formats** |
| 37 | + |
| 38 | + - To maximize compatibility, you can build both formats: |
| 39 | + 1. Output ESM files with `.mjs` extension |
| 40 | + 2. Output CJS files with `.cjs` extension |
| 41 | + 3. Configure package.json with: |
| 42 | + ```json |
| 43 | + { |
| 44 | + "type": "module", |
| 45 | + "exports": { |
| 46 | + "import": "./dist/index.mjs", |
| 47 | + "require": "./dist/index.cjs" |
| 48 | + } |
| 49 | + } |
| 50 | + ``` |
| 51 | + |
| 52 | +## UMD |
| 53 | + |
| 54 | +UMD is a library that can be used in both the browser and Node.js environments. It is a combination of CommonJS and AMD. |
| 55 | + |
| 56 | +### How to build a UMD library? |
| 57 | + |
| 58 | +- Set the `output.format` to `umd` in the Rslib configuration file. |
| 59 | +- If the library need to be exported with a name, set `output.umdName` to the name of the UMD library. |
| 60 | +- Use `output.externals` to specify the external dependencies that the UMD library depends on, `lib.autoExtension` is enabled by default for UMD. |
| 61 | + |
| 62 | +### Examples |
| 63 | + |
| 64 | +The following Rslib config is an example to build a UMD library. |
| 65 | + |
| 66 | +- `output.format: 'umd'`: instruct Rslib to build in UMD format. |
| 67 | +- `output.umdName: 'RslibUmdExample'`: set the export name of the UMD library. |
| 68 | +- `output.externals.react: 'React'`: specify the external dependency `react` could be accessed by `window.React`. |
| 69 | +- `runtime: 'classic'`: use the classic runtime of React to support applications that using React version under 18. |
| 70 | + |
| 71 | +```ts title="rslib.config.ts" {7-12,22} |
| 72 | +import { pluginReact } from '@rsbuild/plugin-react'; |
| 73 | +import { defineConfig } from '@rslib/core'; |
| 74 | + |
| 75 | +export default defineConfig({ |
| 76 | + lib: [ |
| 77 | + { |
| 78 | + format: 'umd', |
| 79 | + umdName: 'RslibUmdExample', |
| 80 | + output: { |
| 81 | + externals: { |
| 82 | + react: 'React', |
| 83 | + }, |
| 84 | + distPath: { |
| 85 | + root: './dist/umd', |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + ], |
| 90 | + plugins: [ |
| 91 | + pluginReact({ |
| 92 | + swcReactOptions: { |
| 93 | + runtime: 'classic', |
| 94 | + }, |
| 95 | + }), |
| 96 | + ], |
| 97 | +}); |
| 98 | +``` |
0 commit comments