Skip to content

Commit ce4dea9

Browse files
authored
docs: add redirect/syntax/externalHelpers/banner/footer/umdName lib config (#383)
1 parent 3f0c8cf commit ce4dea9

File tree

9 files changed

+343
-17
lines changed

9 files changed

+343
-17
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,18 @@ export interface LibConfig extends RsbuildConfig {
8888
*/
8989
autoExtension?: boolean;
9090
/**
91-
* Whether to automatically externalize dependencies and do not bundle them.
91+
* Whether to automatically externalize dependencies of different dependency types and do not bundle them.
9292
* @default true
9393
*/
9494
autoExternal?: AutoExternal;
9595
/**
96-
* Configure the redirect of the import paths.
96+
* Configure the redirect of the import paths, applicable when `bundle: false`.
9797
* @default {}
9898
*/
9999
redirect?: Redirect;
100100
/**
101-
* Support esX and browserslist query
101+
* Configure the syntax to which JavaScript and CSS will be downgraded.
102+
* Support ECMAScript version and browserslist query.
102103
* @default 'esnext'
103104
*/
104105
syntax?: Syntax;

website/docs/en/config/lib/auto-external.mdx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,32 @@ type AutoExternal =
1515

1616
- **Default:** `true`
1717

18-
Whether to automatically externalize dependencies and do not bundle them.
18+
Whether to automatically externalize dependencies of different dependency types and do not bundle them.
1919

20-
## autoExternal.dependencies
20+
## Object Type
21+
22+
### autoExternal.dependencies
2123

2224
- **Type:** `boolean`
2325
- **Default:** `true`
2426

2527
Whether to externalize dependencies of type `dependencies`.
2628

27-
## autoExternal.optionalDependencies
29+
### autoExternal.optionalDependencies
2830

2931
- **Type:** `boolean`
3032
- **Default:** `true`
3133

3234
Whether to externalize dependencies of type `optionalDependencies`.
3335

34-
## autoExternal.peerDependencies
36+
### autoExternal.peerDependencies
3537

3638
- **Type:** `boolean`
3739
- **Default:** `true`
3840

3941
Whether to externalize dependencies of type `peerDependencies`.
4042

41-
## autoExternal.devDependencies
43+
### autoExternal.devDependencies
4244

4345
- **Type:** `boolean`
4446
- **Default:** `false`
@@ -57,7 +59,7 @@ And the following dependency types will be **bundled**:
5759

5860
- `devDependencies`
5961

60-
It is equivalent to the following configuration:
62+
This configuration is equivalent to the following object type:
6163

6264
```ts
6365
export default {

website/docs/en/config/lib/banner.mdx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,62 @@ type Banner = {
1313
- **Default:** `{}`
1414

1515
Inject content into the top of each JS, CSS or DTS file.
16+
17+
## Object Type
18+
19+
### banner.js
20+
21+
- **Type:** `string`
22+
- **Default:** `undefined`
23+
24+
Inject content into the top of each JS file.
25+
26+
### banner.css
27+
28+
- **Type:** `string`
29+
- **Default:** `undefined`
30+
31+
Inject content into the top of each CSS file.
32+
33+
### banner.dts
34+
35+
- **Type:** `string`
36+
- **Default:** `undefined`
37+
38+
Inject content into the top of each DTS file.
39+
40+
## Notice
41+
42+
The banner content in JS/CSS file is based on the [BannerPlugin](https://rspack.dev/plugins/webpack/banner-plugin) of Rspack. You should notice the following points:
43+
44+
- `raw: true` is enabled by default, so the banner content will be injected as a raw string instead of wrapping in a comment. So if you want to inject a comment, you should add `/*` and `*/` or other comment syntax by yourself.
45+
- The `stage` option is set to the stage after the JavaScript and CSS files are optimized, thus preventing the banner content from being optimized away.
46+
47+
## Customize Banner Content
48+
49+
If the above default settings cannot meet your requirements, you can customize the banner content through `tools.rspack.plugins` to add a [BannerPlugin](https://rspack.dev/plugins/webpack/banner-plugin) instance with the corresponding options.
50+
51+
```ts title="rslib.config.ts"
52+
export default {
53+
lib: [
54+
{
55+
// ...
56+
tools: {
57+
rspack: {
58+
plugins: [
59+
new rspack.BannerPlugin({
60+
// ... options
61+
}),
62+
],
63+
},
64+
},
65+
},
66+
],
67+
};
68+
```
69+
70+
:::warning
71+
72+
The banner content in DTS files is handled differently from JS/CSS files. It is written directly using the file system API, so setting `BannerPlugin` will not affect it.
73+
74+
:::

website/docs/en/config/lib/external-helpers.mdx

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,108 @@
33
- **Type:** `boolean`
44
- **Default:** `false`
55

6-
Whether to import SWC helper functions from `@swc/helpers` instead of inlining them.
6+
Whether to import SWC helper functions from [@swc/helpers](https://www.npmjs.com/package/@swc/helpers) instead of inlining them.
7+
8+
By default, the output JavaScript file may depend on helper functions to support the target environment or output format, and these helper functions will be inlined in the file that requires it.
9+
10+
When `externalHelpers` set to `true`, the output JavaScript will import helper functions from the external module `@swc/helpers`.
11+
12+
::: note
13+
14+
Make sure to declare the `@swc/helpers` in `dependencies` field of `package.json`.
15+
16+
:::
17+
18+
## Example
19+
20+
Take the following code as an example:
21+
22+
```ts title="index.ts"
23+
export default class FOO {
24+
get bar() {
25+
return;
26+
}
27+
}
28+
```
29+
30+
When `externalHelpers` is disabled, the output JavaScript will inline helper functions.
31+
32+
```ts title="rslib.config.ts" {6}
33+
export default {
34+
lib: [
35+
// ...
36+
syntax: 'es5'
37+
],
38+
externalHelpers: false,
39+
};
40+
```
41+
42+
Below is the output JavaScript file:
43+
44+
```js title="index.js"
45+
function _class_call_check(instance, Constructor) {
46+
if (!(instance instanceof Constructor))
47+
throw new TypeError('Cannot call a class as a function');
48+
}
49+
function _defineProperties(target, props) {
50+
for (var i = 0; i < props.length; i++) {
51+
var descriptor = props[i];
52+
descriptor.enumerable = descriptor.enumerable || false;
53+
descriptor.configurable = true;
54+
if ('value' in descriptor) descriptor.writable = true;
55+
Object.defineProperty(target, descriptor.key, descriptor);
56+
}
57+
}
58+
function _create_class(Constructor, protoProps, staticProps) {
59+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
60+
if (staticProps) _defineProperties(Constructor, staticProps);
61+
return Constructor;
62+
}
63+
var src_FOO = /*#__PURE__*/ (function () {
64+
'use strict';
65+
function FOO() {
66+
_class_call_check(this, FOO);
67+
}
68+
_create_class(FOO, [
69+
{
70+
key: 'bar',
71+
get: function () {},
72+
},
73+
]);
74+
return FOO;
75+
})();
76+
export { src_FOO as default };
77+
```
78+
79+
When `externalHelpers` is enabled, the output JavaScript will import helper functions from the external module `@swc/helpers`.
80+
81+
```ts title="rslib.config.ts" {6}
82+
export default {
83+
lib: [
84+
// ...
85+
syntax: 'es5'
86+
],
87+
externalHelpers: true,
88+
};
89+
```
90+
91+
Below is the output JavaScript file:
92+
93+
```js title="index.js" {1-2}
94+
import * as __WEBPACK_EXTERNAL_MODULE__swc_helpers_class_call_check__ from '@swc/helpers/_/_class_call_check';
95+
import * as __WEBPACK_EXTERNAL_MODULE__swc_helpers_create_class__ from '@swc/helpers/_/_create_class';
96+
var src_FOO = /*#__PURE__*/ (function () {
97+
'use strict';
98+
function FOO() {
99+
(0, __WEBPACK_EXTERNAL_MODULE__swc_helpers_class_call_check__._)(this, FOO);
100+
}
101+
(0, __WEBPACK_EXTERNAL_MODULE__swc_helpers_create_class__._)(FOO, [
102+
{
103+
key: 'bar',
104+
get: function () {},
105+
},
106+
]);
107+
return FOO;
108+
})();
109+
export { src_FOO as default };
110+
```

website/docs/en/config/lib/footer.mdx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,63 @@ type Footer = {
1313
- **Default:** `{}`
1414

1515
Inject content into the bottom of each JS, CSS or DTS file.
16+
17+
## Object Type
18+
19+
### footer.js
20+
21+
- **Type:** `string`
22+
- **Default:** `undefined`
23+
24+
Inject content into the bottom of each JS file.
25+
26+
### footer.css
27+
28+
- **Type:** `string`
29+
- **Default:** `undefined`
30+
31+
Inject content into the bottom of each CSS file.
32+
33+
### footer.dts
34+
35+
- **Type:** `string`
36+
- **Default:** `undefined`
37+
38+
Inject content into the bottom of each DTS file.
39+
40+
## Notice
41+
42+
The footer content in JS/CSS file is based on the [BannerPlugin](https://rspack.dev/plugins/webpack/banner-plugin) of Rspack. You should notice the following points:
43+
44+
- `raw: true` is enabled by default, so the footer content will be injected as a raw string instead of wrapping in a comment. So if you want to inject a comment, you should add `/*` and `*/` or other comment syntax by yourself.
45+
- The `stage` option is set to the stage after the JavaScript and CSS files are optimized, thus preventing the footer content from being optimized away.
46+
47+
## Customize Footer Content
48+
49+
If the above default settings cannot meet your requirements, you can customize the footer content through `tools.rspack.plugins` to add a [BannerPlugin](https://rspack.dev/plugins/webpack/banner-plugin) instance with the corresponding options.
50+
51+
```ts title="rslib.config.ts"
52+
export default {
53+
lib: [
54+
{
55+
// ...
56+
tools: {
57+
rspack: {
58+
plugins: [
59+
new rspack.BannerPlugin({
60+
footer: true,
61+
// ... options
62+
}),
63+
],
64+
},
65+
},
66+
},
67+
],
68+
};
69+
```
70+
71+
:::warning
72+
73+
The footer content in DTS files is handled differently from JS/CSS files. It is written directly using the file system API, so setting `BannerPlugin` will not affect it.
74+
75+
:::

website/docs/en/config/lib/redirect.mdx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,42 @@ type Redirect = {
88
};
99
```
1010

11-
- **Default:** `{}`
11+
- **Default:**
12+
13+
```ts
14+
const defaultRedirect = {
15+
style: true,
16+
};
17+
```
1218

1319
Configure the redirect of the import paths.
20+
21+
When `bundle: false`, the import path is redirected to ensure that it points to the correct output.
22+
23+
:::note
24+
25+
As bundleless mode is still under development, additional redirect configurations will be introduced in the future.
26+
27+
:::
28+
29+
If you don't need these redirects, you can turn it off, and its import path will not be changed.
30+
31+
```ts title="rslib.config.ts"
32+
export default {
33+
lib: [
34+
// ..
35+
],
36+
redirect: {
37+
style: false, // Turn off the redirect of the style file
38+
},
39+
};
40+
```
41+
42+
## redirect.style
43+
44+
- **Type:** `boolean`
45+
- **Default:** `true`
46+
47+
Whether to redirect the import path of the style file. For example:
48+
49+
- `import './index.less'` will be rewritten to `import './index.css'`

website/docs/en/config/lib/shims.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const defaultShims = {
3434
};
3535
```
3636

37-
Used to configure the shims for CommonJS and ESM output.
37+
Configure the shims for CommonJS and ESM output.
3838

3939
## shims.cjs
4040

@@ -46,7 +46,7 @@ Set the fields to `true` to enable the corresponding shims for CommonJS output.
4646

4747
Options:
4848

49-
- `true`: when `format` is `cjs`, the `import.meta.url` in source code will be replaced with the URL of the current module.
49+
- `true`: when [format](/config/lib/format) is `cjs`, the `import.meta.url` in source code will be replaced with the URL of the current module.
5050

5151
For example, given the following source code:
5252

@@ -88,7 +88,7 @@ Whether to shim the global `__filename` of CommonJS in ESM output.
8888
8989
Options:
9090
91-
- `true`: when `format` is `esm`, the `__filename` in source code will be replaced with the filename of the current module.
91+
- `true`: when [format](/config/lib/format) is `esm`, the `__filename` in source code will be replaced with the filename of the current module.
9292
9393
For example, given the following source code:
9494
@@ -118,7 +118,7 @@ Whether to shim the global `__dirname` of CommonJS in ESM output.
118118
119119
Options:
120120
121-
- `true`: when `format` is `esm`, the `__dirname` in source code will be replaced with the directory name of the current module.
121+
- `true`: when [format](/config/lib/format) is `esm`, the `__dirname` in source code will be replaced with the directory name of the current module.
122122
123123
For example, given the following source code:
124124
@@ -147,7 +147,7 @@ Whether to shim the global `require` of CommonJS in ESM output.
147147
148148
Options:
149149
150-
- `true`: when `format` is `esm`, there will be a `require` that created by `createRequire` at the beginning of the output which can be accessed in source code like the global `require` like CommonJS.
150+
- `true`: when [format](/config/lib/format) is `esm`, there will be a `require` that created by `createRequire` at the beginning of the output which can be accessed in source code like the global `require` like CommonJS.
151151
152152
For example, given the following source code:
153153

0 commit comments

Comments
 (0)