Skip to content

Commit b74e137

Browse files
committed
docs: complete multiple pages under /guide
1 parent 8dbb782 commit b74e137

File tree

10 files changed

+204
-24
lines changed

10 files changed

+204
-24
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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
"output-files",
66
"bundle-mode",
77
"upgrade-rslib",
8-
"cjs-esm",
98
"umd"
109
]
Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,65 @@
1-
# Select Bundle Mode
1+
# Build Patterns
2+
3+
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.
4+
5+
## bundle / bundleless
6+
7+
So first let's understand bundle and bundleless.
8+
9+
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).
10+
11+
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**.
12+
13+
They have their own benefits.
14+
15+
- 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.
16+
- bundleless maintains the original file structure and is more conducive to debugging and tree shaking.
17+
18+
:::warning
19+
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 the [esbuild documentation](https://esbuild.github.io/content-types/#isolated-modules) for more information.
20+
:::
21+
22+
In `buildConfig` you can specify whether the current build task is bundle or bundleless by using [`buildConfig.buildType`](/en/api/config/build-config#buildtype).
23+
24+
## ESM / CJS
25+
26+
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.
27+
28+
### What are ESM and CJS?
29+
30+
- 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).
31+
32+
- [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.
33+
34+
### When to support which format?
35+
36+
For modern libraries, it's recommended to:
37+
38+
1. **Default to ESM**
39+
40+
- ESM is the future of JavaScript modules
41+
- Better tree-shaking support
42+
- Native browser support
43+
- Top-level await support
44+
45+
2. **Consider CJS if:**
46+
47+
- Supporting older Node.js versions (`<12.x`)
48+
- Supporting tools/frameworks that don't fully support ESM
49+
- Having users with CommonJS-only dependencies
50+
51+
3. **Support both formats**
52+
53+
- To maximize compatibility, you can build both formats:
54+
1. Output ESM files with `.mjs` extension
55+
2. Output CJS files with `.cjs` extension
56+
3. Configure package.json with:
57+
```json
58+
{
59+
"type": "module",
60+
"exports": {
61+
"import": "./dist/index.mjs",
62+
"require": "./dist/index.cjs"
63+
}
64+
}
65+
```

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

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
11
# Glossary
2+
3+
## ESM
4+
5+
ESM stands for ECMAScript Modules, 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).
6+
7+
## CJS
8+
9+
CJS stands for [CommonJS](https://nodejs.org/api/modules.html#modules-commonjs-modules), 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.
10+
11+
## UMD
12+
13+
UMD stands for [Universal Module Definition](https://github.com/umdjs/umd), a pattern for writing JavaScript modules that can work universally across different environments, such as both the browser and Node.js. Its primary goal is to ensure compatibility with the most popular module systems, including AMD (Asynchronous Module Definition), CommonJS (CJS), and browser globals.
14+
15+
## Bundleless
16+
17+
Bundleless refers to a development approach that avoids the traditional practice of bundling multiple JavaScript / TypeScript files into a single or fewer output files before serving them to the client. Instead, it aims to serve individual modules directly.
18+
19+
## More
20+
21+
See more glossary in [Rsbuild - Glossary](https://rsbuild.dev/misc/glossary) and [Rspack - Glossary](https://rspack.dev/misc/glossary).

website/docs/en/guide/start/quick-start.mdx

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ nvm use --lts
2323

2424
## Creating an Rslib Project
2525

26-
You can use the `create-rslib` to create a new Rslib project. Run the following command:
26+
You can use the [`create-rslib`](https://www.npmjs.com/package/create-rslib) to create a new Rslib project. Run the following command:
2727

2828
import { PackageManagerTabs } from '@theme';
2929

@@ -40,18 +40,37 @@ Then follow the prompts to complete the operation.
4040

4141
### Templates
4242

43-
When creating a project, you can choose from the following templates provided by `create-rslib`:
43+
`create-rslib` 是用来快速创建 Rslib 项目的工具。在创建项目时,你可以从以下模板中选择:
4444

45-
| Template | Description |
46-
| ------------ | -------------------------------------------------- |
47-
| node-dual-js | Node.js dual ESM/CJS package |
48-
| node-dual-ts | Node.js dual ESM/CJS package written in TypeScript |
49-
| node-esm-js | Node.js pure ESM package |
50-
| node-esm-ts | Node.js pure ESM package written in TypeScript |
45+
| Template | Description |
46+
| ---------------------------- | ---------------------------- |
47+
| Node.js dual ESM/CJS package | Node.js dual ESM/CJS package |
48+
| Node.js pure ESM package | Node.js pure ESM package |
49+
| React | React component library |
50+
51+
Each template support both use JavaScript and TypeScript.
52+
53+
:::info
54+
We're working to provide templates for more frameworks (such as Vue).
55+
:::
56+
57+
### Development Tools
58+
59+
`create-rslib` can help you set up some commonly used development linter tools, including [Vitest](https://vitest.dev/), [Storybook](https://storybook.js.org/). You can use the arrow keys and the space bar to make your selections. If you don't need these tools, you can simply press Enter to skip.
60+
61+
- Vitest is available for all templates, it will be adapted based on the template's selection.
62+
- Storybook is available for web targeted templates (React), it will be adapted based on the template's selection.
63+
64+
```text
65+
◆ Select development tools (Use <space> to select, <enter> to continue)
66+
│ ◻ Storybook
67+
│ ◻ Vitest
68+
69+
```
5170

5271
### Optional Tools
5372

54-
`create-rslib` can help you set up some commonly used tools, including [Biome](https://github.com/biomejs/biome), [ESLint](https://github.com/eslint/eslint), and [prettier](https://github.com/prettier/prettier). You can use the arrow keys and the space bar to make your selections. If you don't need these tools, you can simply press Enter to skip.
73+
`create-rslib` can help you set up some commonly used formatter and linter tools, including [Biome](https://biomejs.dev/), [ESLint](https://eslint.org/), and [prettier](https://prettier.io/). You can use the arrow keys and the space bar to make your selections. If you don't need these tools, you can simply press Enter to skip.
5574

5675
```text
5776
◆ Select additional tools (Use <space> to select, <enter> to continue)

0 commit comments

Comments
 (0)