Skip to content

Commit 6a93381

Browse files
committed
docs: complete multiple pages under /guide
1 parent f2acb19 commit 6a93381

File tree

13 files changed

+261
-27
lines changed

13 files changed

+261
-27
lines changed

packages/core/src/types/config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type AutoExternal =
4141
| boolean
4242
| {
4343
dependencies?: boolean;
44+
optionalDependencies?: boolean;
4445
devDependencies?: boolean;
4546
peerDependencies?: boolean;
4647
};

packages/create-rslib/src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ async function getTemplateName({ template }: Argv) {
3737
}),
3838
);
3939

40+
const language = checkCancel<string>(
41+
await select({
42+
message: 'Select language',
43+
options: [
44+
{ value: 'ts', label: 'TypeScript' },
45+
{ value: 'js', label: 'JavaScript' },
46+
],
47+
}),
48+
);
49+
4050
const supportStorybook = templateName === 'react';
4151

4252
type ExcludesFalse = <T>(x: T | false) => x is T;
@@ -56,16 +66,6 @@ async function getTemplateName({ template }: Argv) {
5666
}),
5767
);
5868

59-
const language = checkCancel<string>(
60-
await select({
61-
message: 'Select language',
62-
options: [
63-
{ value: 'ts', label: 'TypeScript' },
64-
{ value: 'js', label: 'JavaScript' },
65-
],
66-
}),
67-
);
68-
6969
return composeTemplateName({
7070
template: templateName,
7171
lang: language as Lang,

website/docs/en/guide/advanced/_meta.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
[
22
"package-json",
3-
"templates",
43
"third-party-deps",
54
"polyfill",
65
"css",

website/docs/en/guide/advanced/templates.mdx

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,81 @@
11
# 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"`](/en/guide/basic/before-getting-started#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+
```

website/docs/en/guide/basic/_meta.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
"cli",
33
"configure-rslib",
44
"typescript-usage",
5-
"output-files",
6-
"bundle-mode",
5+
"output-format",
6+
"output-structure",
77
"upgrade-rslib",
8-
"cjs-esm",
98
"umd"
109
]

website/docs/en/guide/basic/bundle-mode.mdx

Lines changed: 0 additions & 1 deletion
This file was deleted.

website/docs/en/guide/basic/cjs-esm.mdx

Lines changed: 0 additions & 1 deletion
This file was deleted.

website/docs/en/guide/basic/output-files.mdx

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)