Skip to content

Commit d5f0bb4

Browse files
committed
docs: add redirect/syntax/externalHelpers/banner/footer/umdName lib config
1 parent eaa07c8 commit d5f0bb4

File tree

9 files changed

+288
-38
lines changed

9 files changed

+288
-38
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: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,7 @@ type AutoExternal =
1515

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

18-
Whether to automatically externalize dependencies and do not bundle them.
19-
20-
## autoExternal.dependencies
21-
22-
- **Type:** `boolean`
23-
- **Default:** `true`
24-
25-
Whether to externalize dependencies of type `dependencies`.
26-
27-
## autoExternal.optionalDependencies
28-
29-
- **Type:** `boolean`
30-
- **Default:** `true`
31-
32-
Whether to externalize dependencies of type `optionalDependencies`.
33-
34-
## autoExternal.peerDependencies
35-
36-
- **Type:** `boolean`
37-
- **Default:** `true`
38-
39-
Whether to externalize dependencies of type `peerDependencies`.
40-
41-
## autoExternal.devDependencies
42-
43-
- **Type:** `boolean`
44-
- **Default:** `false`
45-
46-
Whether to bundle dependencies of type `devDependencies`.
18+
Whether to automatically externalize dependencies of different dependency types and do not bundle them.
4719

4820
## Default Value
4921

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

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

1515
Inject content into the top of each JS, CSS or DTS file.
16+
17+
## Notice
18+
19+
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:
20+
21+
- `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.
22+
- 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.
23+
24+
## Customize Banner Content
25+
26+
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.
27+
28+
```ts title="rslib.config.ts"
29+
export default {
30+
lib: [
31+
{
32+
// ...
33+
tools: {
34+
rspack: {
35+
plugins: [
36+
new rspack.BannerPlugin({
37+
// ... options
38+
}),
39+
],
40+
},
41+
},
42+
},
43+
],
44+
};
45+
```
46+
47+
:::warning
48+
49+
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.
50+
51+
:::

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,107 @@
44
- **Default:** `false`
55

66
Whether to import SWC helper functions from `@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+
::: tip
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"
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,40 @@ type Footer = {
1313
- **Default:** `{}`
1414

1515
Inject content into the bottom of each JS, CSS or DTS file.
16+
17+
## Notice
18+
19+
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:
20+
21+
- `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.
22+
- 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.
23+
24+
## Customize Footer Content
25+
26+
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.
27+
28+
```ts title="rslib.config.ts"
29+
export default {
30+
lib: [
31+
{
32+
// ...
33+
tools: {
34+
rspack: {
35+
plugins: [
36+
new rspack.BannerPlugin({
37+
footer: true,
38+
// ... options
39+
}),
40+
],
41+
},
42+
},
43+
},
44+
],
45+
};
46+
```
47+
48+
:::warning
49+
50+
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.
51+
52+
:::

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

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,46 @@ type Syntax = EcmaScriptVersion | string[];
2222
```
2323

2424
- **Default:** `'esnext'`
25+
26+
Configure the syntax to which JavaScript and CSS will be downgraded.
27+
28+
See [Output Compatibility - Syntax Downgrade](/guide/advanced/output-compatibility) for more details.
29+
30+
## Set ECMAScript Version
31+
32+
You can set the ECMAScript version directly, such as `es2015`, `es2022`, etc.
33+
34+
```ts title="rslib.config.ts"
35+
export default {
36+
lib: [
37+
// ...
38+
],
39+
syntax: 'es2015',
40+
};
41+
```
42+
43+
## Set Browserslist Query
44+
45+
You can also set the Browserslist query, such as `last 2 versions`, `> 1%`, `node >= 16`, `chrome >= 80`, etc.
46+
47+
```ts title="rslib.config.ts"
48+
export default {
49+
lib: [
50+
// ...
51+
],
52+
syntax: ['last 2 versions', '> 1%'],
53+
};
54+
```
55+
56+
## Mix ECMAScript Version and Browserslist Query
57+
58+
You can also mix ECMAScript version and Browserslist query, such as `es2015` and `node 20`. We will turn ECMAScript Version into Browserslist query, and then merge them together.
59+
60+
```ts title="rslib.config.ts"
61+
export default {
62+
lib: [
63+
// ...
64+
],
65+
syntax: ['es2015', 'node 20'],
66+
};
67+
```

0 commit comments

Comments
 (0)