Skip to content

Commit 375009f

Browse files
authored
docs: update import.meta documentation for clarity (#13771)
* docs: fix CJS examples * docs: update import.meta documentation for clarity * docs: fix
1 parent 2ae10ae commit 375009f

6 files changed

Lines changed: 22 additions & 30 deletions

File tree

website/docs/en/api/runtime-api/module-variables.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ __dirname
9494

9595
</Columns>
9696

97-
## import.meta (ESM)
97+
## import.meta (ESM) \{#import-meta}
9898

9999
The `import.meta` exposes context-specific metadata to a JavaScript module, such as the URL of the module. It is only available in modules that support ESM ([module type](/misc/glossary#module-type) is `javascript/auto` or `javascript/esm`).
100100

website/docs/en/config/module.mdx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,16 +1219,12 @@ export default {
12191219
12201220
<ApiMeta addedVersion="1.0.0-alpha.6" />
12211221
1222-
<PropertyType
1223-
type="boolean | 'preserve-unknown'"
1224-
defaultValueList={[{ defaultValue: 'true' }]}
1225-
/>
1226-
1227-
Enable or disable evaluating `import.meta`.
1222+
- **Type:** `boolean | 'preserve-unknown'`
1223+
- **Default:** When using [ESM output](/guide/features/esm), the default value is `'preserve-unknown'`; otherwise, it is `true`
12281224
1229-
When using [ESM output](/guide/features/esm), default value is set to `'preserve-unknown'`, otherwise `true`.
1225+
Control whether Rspack parses and replaces `import.meta` in source code. Available values:
12301226
1231-
- `"preserve-unknown"`: [Known `import.meta` properties](/api/runtime-api/module-variables#importmeta-esm) are statically analyzed at compile-time, other properties are preserved and evaluated at runtime.
1227+
- `"preserve-unknown"`: [Known `import.meta` properties](/api/runtime-api/module-variables#import-meta) are statically analyzed at compile-time, other properties are preserved and evaluated at runtime.
12321228
- `true`: All `import.meta` properties are statically analyzed at compile-time.
12331229
- `false`: All `import.meta` properties are evaluated at runtime.
12341230

website/docs/en/guide/optimization/tree-shaking.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ When an unused variable's initial value is marked as side-effect-free (pure), it
191191

192192
This feature is currently experimental and needs to be enabled via following experimental feature configuration, and it depends on [optimization.sideEffects](/config/optimization#optimizationsideeffects) (`sideEffects` is enabled by default in production mode):
193193

194-
```javascript title="rspack.config.js"
195-
module.exports = {
194+
```js title="rspack.config.mjs"
195+
export default {
196196
experiments: {
197197
pureFunctions: true,
198198
},
@@ -203,7 +203,7 @@ module.exports = {
203203

204204
You can place this annotation before function declarations, function expressions, arrow functions, or export statements, check [spec](https://github.com/javascript-compiler-hints/compiler-notations-spec/blob/main/no-side-effects-notation-spec.md) for details:
205205

206-
```javascript
206+
```js
207207
/*#__NO_SIDE_EFFECTS__*/
208208
function foo() {
209209
console.log('foo');
@@ -241,8 +241,8 @@ For business source code where modification is possible, it is recommended to us
241241

242242
This option is configured in `module.rules[i].parser`:
243243

244-
```javascript title="rspack.config.js"
245-
module.exports = {
244+
```js title="rspack.config.mjs"
245+
export default {
246246
experiments: {
247247
pureFunctions: true,
248248
},

website/docs/zh/api/runtime-api/module-variables.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ __dirname
9494

9595
</Columns>
9696

97-
## import.meta(ESM)
97+
## import.meta(ESM) \{#import-meta}
9898

9999
`import.meta` 将特定上下文的元数据暴露给 JavaScript 模块,例如模块的 URL。它仅在支持 ESM([模块类型](/misc/glossary#module-type模块类型)`javascript/auto``javascript/esm`)的模块中可用。
100100

website/docs/zh/config/module.mdx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,18 +1216,14 @@ export default {
12161216
12171217
<ApiMeta addedVersion="1.0.0-alpha.6" />
12181218
1219-
<PropertyType
1220-
type="boolean | 'preserve-unknown'"
1221-
defaultValueList={[{ defaultValue: 'true' }]}
1222-
/>
1223-
1224-
是否要解析替换 `import.meta`
1219+
- **类型:** `boolean | 'preserve-unknown'`
1220+
- **默认值:** 当输出为 [ESM 格式](/guide/features/esm) 时,默认值为 `'preserve-unknown'`,其他情况默认值为 `true`
12251221
1226-
当输出为[ESM 格式](/guide/features/esm)时,默认值为`'preserve-unknown'`,其他情况默认值为`true`
1222+
用于控制是否要解析和替换源代码中的 `import.meta`,可选值有:
12271223
1228-
- `"preserve-unknown"`: [已知的`import.meta`字段](/api/runtime-api/module-variables#importmeta-esm)会在编译期被替换,其他字段会被保留并在运行时被计算。
1229-
- `true`: 所有`import.meta`字段在编译期被替换。
1230-
- `false`: 所有`import.meta`字段都在运行时被计算。
1224+
- `"preserve-unknown"`: [已知的 `import.meta` 字段](/api/runtime-api/module-variables#import-meta) 会在编译期被替换,其他字段会被保留并在运行时被计算。
1225+
- `true`: 所有 `import.meta` 字段在编译期被替换。
1226+
- `false`: 所有 `import.meta` 字段都在运行时被计算。
12311227
12321228
```js title="rspack.config.mjs"
12331229
export default {

website/docs/zh/guide/optimization/tree-shaking.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ export const foo = 42;
194194

195195
该功能目前处于实验阶段,需要通过下面的实验性 feature 开关开启,且依赖于 [optimization.sideEffects](/config/optimization#optimizationsideeffects)(sideEffects 在生产模式下默认开启):
196196

197-
```javascript title="rspack.config.js"
198-
module.exports = {
197+
```js title="rspack.config.mjs"
198+
export default {
199199
experiments: {
200200
pureFunctions: true,
201201
},
@@ -204,7 +204,7 @@ module.exports = {
204204

205205
你可以将该注解放在函数声明、函数表达式、箭头函数或导出语句之前,更多请参考社区[规范](https://github.com/javascript-compiler-hints/compiler-notations-spec/blob/main/no-side-effects-notation-spec.md)
206206

207-
```javascript
207+
```js
208208
/*#__NO_SIDE_EFFECTS__*/
209209
function foo() {
210210
console.log('foo');
@@ -242,8 +242,8 @@ export function baz() {}
242242

243243
该选项配置在 `module.rules``parser` 中:
244244

245-
```javascript title="rspack.config.js"
246-
module.exports = {
245+
```js title="rspack.config.mjs"
246+
export default {
247247
experiments: {
248248
pureFunctions: true,
249249
},

0 commit comments

Comments
 (0)