You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/en/guide/advanced/output-compatibility.mdx
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ And install [core-js-pure](https://www.npmjs.com/package/core-js-pure) as the ru
33
33
34
34
<PackageManagerTabscommand="add core-js-pure" />
35
35
36
-
Configure the Babel plugin with polyfill options, set the [target](https://babeljs.io/docs/options#targets) field to specify the target browser version.
36
+
Configure the Babel plugin with polyfill options, set the [targets](https://babeljs.io/docs/options#targets) field to specify the target browser version.
- For projects with `bundle` enabled, the Node Polyfill will be injected and included in the output.
100
-
- For projects with `bundle` disabled, polyfills are not injected into the output by default. To avoid inlining the polyfill in every module. The modules are externalized and need to be added to dependencies manually, follow these steps:
100
+
- For projects with `bundle` disabled, polyfills are not injected into the output by default. To avoid inlining the polyfill in every module, the modules are externalized and need to be added to dependencies manually, follow these steps:
101
101
102
102
1. Configure `output.external` with `resolvedPolyfillToModules`, which you can import from [@rsbuild/plugin-node-polyfill](https://github.com/rspack-contrib/rsbuild-plugin-node-polyfill). This will externalize the polyfill modules to the installed polyfill dependencies.
Copy file name to clipboardExpand all lines: website/docs/en/guide/advanced/third-party-deps.mdx
+6-5Lines changed: 6 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -9,17 +9,17 @@ Generally, third-party dependencies required by a project can be installed via t
9
9
}
10
10
```
11
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.
12
+
Dependencies under `"dependencies"` are generally required for the package in runtime, and if these third-party dependencies are declared under `"devDependencies"`, then there will be missing dependencies in production runtime.
13
13
14
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
15
16
16
## Default handling of third-party dependencies
17
17
18
-
By default, **third-party dependencies under `"dependencies"`, `"optionalDependencies"` and `"peerDependencies"` are not bundled by Rslib**.
18
+
By default, third-party dependencies under `"dependencies"`, `"optionalDependencies"` and `"peerDependencies"` are not bundled by Rslib.
19
19
20
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
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.
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
23
24
24
### Example
25
25
@@ -51,13 +51,14 @@ import * as __WEBPACK_EXTERNAL_MODULE_react__ from 'react';
We previously introduced the use of [`lib.autoExternal`](/config/lib/auto-external). This configuration lets you manage third-party dependencies more precisely.
61
+
The configuration described above allows you to implement more fine-grained handling of third-party dependencies.
61
62
62
63
For example, when we need to leave only certain dependencies unbundled, we can configure it as follows.
Copy file name to clipboardExpand all lines: website/docs/en/guide/basic/output-format.mdx
+12-20Lines changed: 12 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -5,35 +5,33 @@ import MF from '../start/components/MF.mdx';
5
5
6
6
# Output Format
7
7
8
-
This sets the output format for the generated JavaScript files. There are three supported values now: `esm`, `cjs`, and `umd`. In this guide, we will discuss the differences between these formats and how to choose the right one for your library.
8
+
There are four supported output formats for the generated JavaScript files in Rslib: `esm`, `cjs`, `umd`, and `mf`. In this chapter, we will introduce the differences between these formats and how to choose the right one for your library.
9
9
10
10
## ESM / CJS
11
11
12
12
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.
13
13
14
14
### What are ESM and CJS?
15
15
16
-
<ESM />
17
-
18
16
-**ESM**: ESM is <ESM />
19
17
20
18
-**CommonJS**: [CommonJS](https://nodejs.org/api/modules.html#modules-commonjs-modules) is <CJS />
21
19
22
-
### What is the dilemma of esm / cjs
20
+
### What is the dilemma of ESM / CJS
23
21
24
22
> The following references are from [Node Modules at War: Why CommonJS and ES Modules Can't Get Along](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1).
25
23
26
24
1. You can't `require()` ESM scripts; you can only import ESM scripts, like this: `import {foo} from 'foo'`
27
25
2. CJS scripts can't use static `import` statements like the one above.
28
-
3. ESM scripts can `import` CJS scripts, but only by using the “default import” syntax `import _ from 'lodash'`, not the “named import” syntax `import {shuffle} from 'lodash'`, which is a hassle if the CJS script uses named exports. (Except, sometimes, unpredictibly, Node can figure out what you meant!)
26
+
3. ESM scripts can `import` CJS scripts, but only by using the **default import** syntax `import _ from 'lodash'`, not the **named import** syntax `import {shuffle} from 'lodash'`, which is a hassle if the CJS script uses named exports. (Except, sometimes, unpredictibly, Node can figure out what you meant!)
29
27
4. ESM scripts can `require()` CJS scripts, even with named exports, but it's typically not worth the trouble, because it requires even more boilerplate, and, worst of all, bundlers like Webpack and Rollup don't/won't know how to work with ESM scripts that use `require()`.
30
28
5. CJS is the default; you have to opt-in to ESM mode. You can opt-in to ESM mode by renaming your script from `.js` to `.mjs`. Alternately, you can set `"type": "module"` in `package.json`, and then you can opt-out of ESM by renaming scripts from `.js` to `.cjs`. (You can even tweak just an individual subdirectory by putting a one-line `{"type": "module"}``package.json` file in there.)
31
29
32
30
### When to support which format?
33
31
34
32
For different shapes of libraries, the choice of module format may vary. Here are two common scenarios:
shipping only ESM is the best choice for libraries that are intended to be used in modern environments, such as browser applications or Node.js applications that support ESM. However, if the upstream library is in format of CJS, they only can import pure ESM by using dynamic import like `const pureEsmLib = await import('pure-esm-lib')`.
39
37
@@ -47,7 +45,7 @@ shipping only ESM is the best choice for libraries that are intended to be used
47
45
- Some Node.js tools and libraries still have limited or incomplete support for ESM, requiring workarounds or additional configuration.
48
46
- You must explicitly include file extensions in import paths, which can be cumbersome, especially when working with TypeScript or other transpiled languages.
The community is migrating to ESM, but there are still many projects using CJS. If you want to support both ESM and CJS, you can publish a dual package. For most library authors, offering dual formats is a safer and smoother way to access the best of both worlds. You could read antfu' blog post [Publish ESM and CJS packages](https://antfu.me/posts/publish-esm-and-cjs) for more details.
53
51
@@ -60,8 +58,8 @@ The community is migrating to ESM, but there are still many projects using CJS.
60
58
61
59
-**Cons:**
62
60
63
-
- Increased complexity: Maintaining two module formats adds complexity to the build process, requiring additional configuration and testing to ensure both versions work correctly14.
64
-
- Dual package hazard: Mixing ESM and CJS can lead to issues such as broken instanceof checks or unexpected behavior when dependencies are loaded in different formats13.
61
+
- Increased complexity: Maintaining two module formats adds complexity to the build process, requiring additional configuration and testing to ensure both versions work correctly.
62
+
- Dual package hazard: Mixing ESM and CJS can lead to issues such as broken instanceof checks or unexpected behavior when dependencies are loaded in different formats.
65
63
66
64
## UMD
67
65
@@ -78,17 +76,17 @@ A detailed answer from StackOverflow: [What is the Universal Module Definition (
78
76
> However, for frontend libraries, you still offer a single file for convenience, that users can download (from a CDN) and directly embed in their web pages. This still commonly employs a UMD pattern, it's just no longer written/copied by the library author into their source code, but added automatically by the transpiler/bundler.
79
77
>
80
78
> And similarly, for backend/universal libraries that are supposed to work in
81
-
> node.js, you still also distribute a commonjs module build via npm to support
82
-
> all the users who still use a legacy version of node.js (and don't want/need
79
+
> Node.js, you still also distribute a commonjs module build via npm to support
80
+
> all the users who still use a legacy version of Node.js (and don't want/need
83
81
> to employ a transpiler themselves). This is less common nowadays for new
84
82
> libraries, but existing ones try hard to stay backwards-compatible and not
85
83
> cause applications to break.
86
84
87
85
### How to build a UMD library?
88
86
89
-
- Set the `lib.format` to `umd` in the Rslib configuration file.
90
-
- If the library need to be exported with a name, set `lib.umdName` to the name of the UMD library.
91
-
- Use `output.externals` to specify the external dependencies that the UMD library depends on, `lib.autoExtension` is enabled by default for UMD.
87
+
- Set the [lib.format](/config/lib/format) to `umd` in the Rslib configuration file.
88
+
- If the library need to be exported with a name, set [lib.umdName](/config/lib/umd-name) to the name of the UMD library.
89
+
- Use [output.externals](/config/rsbuild/output#outputexternals) to specify the external dependencies that the UMD library depends on, [lib.autoExtension](/config/lib/auto-extension) is enabled by default for UMD.
92
90
93
91
### Examples
94
92
@@ -136,9 +134,3 @@ export default defineConfig({
136
134
### What is MF?
137
135
138
136
MF stands for Module Federation. <MF />
139
-
140
-
### When to use MF?
141
-
142
-
### How to build a MF library?
143
-
144
-
Check out [Advanced - Module Federation](/guide/advanced/module-federation) chapter for more details.
Copy file name to clipboardExpand all lines: website/docs/en/guide/basic/output-structure.mdx
+3-1Lines changed: 3 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6,15 +6,17 @@ So first let's understand bundle and bundleless.
6
6
7
7
A bundle is a package of build artifacts, which may be a single file or multiple files based on a certain [code splitting strategy](https://esbuild.github.io/api/#splitting).
8
8
9
-
bundleless, on the other hand, means that each source file is compiled and built separately, but not bundled together. Each output file can be found with its corresponding source code file. The process of **bundleless build can also be understood as the process of code conversion of source files only**.
9
+
bundleless, on the other hand, means that each source file is compiled and built separately, but not bundled together. Each output file can be found with its corresponding source code file. The process of bundleless build can also be understood as the process of code conversion of source files only.
10
10
11
11
They have their own benefits.
12
12
13
13
- bundle can reduce the size of build artifacts and also pre-package dependencies to reduce the size of installed dependencies. Packaging libraries in advance can speed up application project builds.
14
14
- bundleless maintains the original file structure and is more conducive to debugging and tree shaking.
15
15
16
16
:::warning
17
+
17
18
bundleless is a single-file compilation mode, so for referencing and exporting types, you need to add the `type` keyword. For example, `import type { A } from './types'`. Please refer to [TypeScript - isolatedModules](/guide/basic/typescript#isolatedmodules) for more information.
19
+
18
20
:::
19
21
20
22
You can specify whether to bundle using the [bundle](/config/lib/bundle) option, which is set to `true` by default.
Copy file name to clipboardExpand all lines: website/docs/en/guide/basic/typescript.mdx
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -32,9 +32,9 @@ export type { SomeType } from './types';
32
32
33
33
## tsconfig.json Path
34
34
35
-
Rslib by default reads the `tsconfig.json` file from the root directory. You can use [source.tsconfigPath](https://rsbuild.dev/config/source/tsconfig-path) to configure a custom tsconfig.json file path.
35
+
Rslib by default reads the `tsconfig.json` file from the root directory. You can use [source.tsconfigPath](/config/rsbuild/source#sourcetsconfigpath) to configure a custom `tsconfig.json` file path.
Copy file name to clipboardExpand all lines: website/docs/en/guide/basic/upgrade-rslib.mdx
+4-2Lines changed: 4 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,9 @@ This section explains how to upgrade the project's Rslib dependencies to the lat
6
6
{/* > Please see [Releases](/community/releases/index) to understand the Rsbuild release strategy. */}
7
7
8
8
:::info
9
-
Rslib is still in the early stages of development, and the API may change frequently. We recommend upgrading to the latest version to access new features and bug fixes. We plan to release version 0.1.0 in the fourth quarter of 2024.
9
+
10
+
Rslib is still in 0.x version stage, and the API may change frequently. We recommend upgrading to the latest version to access new features and bug fixes.
11
+
10
12
:::
11
13
12
14
## Using Taze
@@ -22,7 +24,7 @@ npx taze major --include "/(rsbuild|rslib)/" -w
22
24
```
23
25
24
26
:::tip
25
-
Rslib has not yet reached version 0.1.0, so you need to add the `major` parameter when updating.
27
+
Rslib has not yet reached version 1.0.0, so you need to add the `major` parameter when updating.
在处理兼容性问题之前,建议了解以下背景知识,以便更好地处理相关问题。请查看有关[语法降级和 API 降级](https://rsbuild.dev/zh/guide/advanced/browser-compatibility#%E8%AF%AD%E6%B3%95%E9%99%8D%E7%BA%A7%E5%92%8C-api-%E9%99%8D%E7%BA%A7)的背景知识。
19
+
20
+
### 浏览器
21
+
22
+
通常,我们不需要为 npm 包注入 polyfill,这个步骤应该在 web 应用框架侧完成,但在某些场景下,我们需要注入 polyfill 以使我们的库直接在低版本浏览器中运行。
0 commit comments