Skip to content

Commit cadbb64

Browse files
authored
docs: add how to preserve module variables (#1010)
1 parent effa6b0 commit cadbb64

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed

website/docs/en/config/rsbuild/source.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ If [experimentalDecorators](https://www.typescriptlang.org/tsconfig/#experimenta
1717

1818
## source.define <RsbuildDocBadge path="/config/source/define" text="source.define" />
1919

20-
Replaces variables in your code with other values or expressions at compile time. This can be useful for allowing different behavior between development builds and production builds.
20+
Replaces variables in your code with other values or expressions at compile time. This can be useful for injecting env variables and other information to the code during build time.
2121

2222
## source.entry <RsbuildDocBadge path="/config/source/entry" text="source.entry" />
2323

website/docs/en/guide/faq/features.mdx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ By default, Rslib uses SWC to remove comments. The corresponding SWC [jsc.minify
4848
}
4949
```
5050

51-
This will only preserve some legal comments and annotations. If you want to preserve all comments, you can refer to the following configuration
51+
This will only preserve some legal comments and annotations. If you want to preserve all comments, you can refer to the following configuration:
5252

5353
```ts title="rslib.config.ts"
5454
export default {
@@ -113,3 +113,30 @@ export default {
113113
},
114114
};
115115
```
116+
117+
## Miscellaneous
118+
119+
### How to preserve module variables such as `__webpack_hash__` in the source code when generating outputs?
120+
121+
Rslib based on Rspack will transform [module variables](https://rspack.dev/api/runtime-api/module-variables) like `__webpack_hash__`, `__webpack_nonce__`, `__webpack_public_path__`, etc. to runtime code containing `__webpack_require__` by default during build process. If you need to preserve these module variables in the outputs, you can configure [source.define](/config/rsbuild/source#sourcedefine) as follows:
122+
123+
1. Replace the module variables that need to be preserved in the source code with a unique name, such as `__webpack_hash__` with `WEBPACK_HASH`, `__webpack_nonce__` with `WEBPACK_NONCE`, `__webpack_public_path__` with `WEBPACK_PUBLIC_PATH`, etc.
124+
125+
```ts
126+
const isUpdateAvailable = () => lastCompilationHash !== __webpack_hash__; // [!code --]
127+
const isUpdateAvailable = () => lastCompilationHash !== WEBPACK_HASH; // [!code ++]
128+
```
129+
130+
2. Add the module variables that need to be preserved in `source.define`. The key of the passed configuration object is the replaced variable name in the source code, and the value is the module variable that needs to be preserved in the outputs.
131+
132+
```ts title="rslib.config.ts"
133+
export default defineConfig({
134+
source: {
135+
define: {
136+
WEBPACK_HASH: '__webpack_hash__',
137+
WEBPACK_NONCE: '__webpack_nonce__',
138+
WEBPACK_PUBLIC_PATH: '__webpack_public_path__',
139+
},
140+
},
141+
});
142+
```

website/docs/zh/config/rsbuild/source.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { RsbuildDocBadge } from '@components/RsbuildDocBadge';
1616

1717
## source.define <RsbuildDocBadge path="/config/source/define" text="source.define" />
1818

19-
构建时将代码中的变量替换成其它值或者表达式,可以用于在代码逻辑中区分开发模式与生产模式等场景
19+
构建时将代码中的变量替换成其它值或者表达式,可以用于在构建时向代码注入环境变量等信息
2020

2121
## source.entry <RsbuildDocBadge path="/config/source/entry" text="source.entry" />
2222

website/docs/zh/guide/faq/features.mdx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default defineConfig({
4848
}
4949
```
5050

51-
这将仅保留部分 legal 注释及 annotations。如果你想保留所有注释,可以参考如下配置
51+
这将仅保留部分 legal 注释及 annotations。如果你想保留所有注释,可以参考如下配置
5252

5353
```ts title="rslib.config.ts"
5454
export default {
@@ -113,3 +113,30 @@ export default {
113113
},
114114
};
115115
```
116+
117+
## 其他
118+
119+
### 如何在生成产物时保留源码中的 `__webpack_hash__` 等模块变量?
120+
121+
Rslib 底层使用的 Rspack,在构建时会默认将 `__webpack_hash__``__webpack_nonce__``__webpack_public_path__`[模块变量](https://rspack.dev/zh/api/runtime-api/module-variables) 转换为包含 `__webpack_require__` 的运行时代码。如果你需要在产物中保留这些模块变量,可以通过配置 [source.define](/config/rsbuild/source#sourcedefine) 来实现,如下所示:
122+
123+
1. 在源码中将需要保留的模块变量替换为一个特征名称,如 `__webpack_hash__` 替换为 `WEBPACK_HASH``__webpack_nonce__` 替换为 `WEBPACK_NONCE``__webpack_public_path__` 替换为 `WEBPACK_PUBLIC_PATH` 等。
124+
125+
```ts
126+
const isUpdateAvailable = () => lastCompilationHash !== __webpack_hash__; // [!code --]
127+
const isUpdateAvailable = () => lastCompilationHash !== WEBPACK_HASH; // [!code ++]
128+
```
129+
130+
2.`source.define` 中添加需要保留的模块变量,传入的配置对象的键名是源码中替换后的变量名称,值是需要在产物中保留的模块变量。
131+
132+
```ts title="rslib.config.ts"
133+
export default defineConfig({
134+
source: {
135+
define: {
136+
WEBPACK_HASH: '__webpack_hash__',
137+
WEBPACK_NONCE: '__webpack_nonce__',
138+
WEBPACK_PUBLIC_PATH: '__webpack_public_path__',
139+
},
140+
},
141+
});
142+
```

website/rspress.config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import path from 'node:path';
22
import { pluginSass } from '@rsbuild/plugin-sass';
33
import { pluginLlms } from '@rspress/plugin-llms';
44
import { pluginRss } from '@rspress/plugin-rss';
5-
import { transformerNotationHighlight } from '@shikijs/transformers';
5+
import {
6+
transformerNotationDiff,
7+
transformerNotationHighlight,
8+
} from '@shikijs/transformers';
69
import { pluginGoogleAnalytics } from 'rsbuild-plugin-google-analytics';
710
import { pluginOpenGraph } from 'rsbuild-plugin-open-graph';
811
import { pluginFontOpenSans } from 'rspress-plugin-font-open-sans';
@@ -53,7 +56,7 @@ export default defineConfig({
5356
markdown: {
5457
checkDeadLinks: true,
5558
shiki: {
56-
transformers: [transformerNotationHighlight()],
59+
transformers: [transformerNotationHighlight(), transformerNotationDiff()],
5760
},
5861
},
5962
search: {

0 commit comments

Comments
 (0)