Skip to content

Commit

Permalink
docs: add tip for import.meta.webpackContext literal (#7815)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Sep 6, 2024
1 parent d9a6d59 commit 3ad52de
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
6 changes: 5 additions & 1 deletion website/docs/en/api/runtime-api/module-methods.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,11 @@ const context = require.context('../', true, /\.stories\.js$/);
const context = require.context('./locales', true, /\.json$/, 'lazy');
```

> The arguments passed to `require.context()` must be literals.
:::tip
Rspack uses static analysis to parse the parameters of `require.context` during compilation. Therefore, the parameters must be [literals](https://developer.mozilla.org/en-US/docs/Glossary/Literal).

For example, the value of `filter` cannot be a variable, nor can it be the value generated by `new RegExp()`. It can only be a regular expression literal.
:::

## Data URI Module

Expand Down
10 changes: 5 additions & 5 deletions website/docs/en/api/runtime-api/module-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ typeof import.meta.url

You can use `import.meta.webpackContext` in your code, and Rspack will parse and reference the matching modules during the build process.

:::tip
The return value of `import.meta.webpackContext` is the same as [import.meta.webpackContext](/api/runtime-api/module-variables#importmetawebpackcontext). We recommend using `import.meta.webpackContext`, which is more powerful.
:::

- **Type:**

```ts
Expand Down Expand Up @@ -200,7 +196,11 @@ const context = import.meta.webpackContext('./locales', {
});
```

> The first arguments passed to `import.meta.webpackContext()` must be literals.
:::tip
Rspack uses static analysis to parse the parameters of `import.meta.webpackContext()` during compilation. Therefore, the parameters must be [literals](https://developer.mozilla.org/en-US/docs/Glossary/Literal).

For example, the value of `regExp` cannot be a variable, nor can it be the value generated by `new RegExp()`. It can only be a regular expression literal.
:::

### context API

Expand Down
6 changes: 5 additions & 1 deletion website/docs/zh/api/runtime-api/module-methods.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,8 @@ const context = require.context('../', true, /\.stories\.js$/);
const context = require.context('./locales', true, /\.json$/, 'lazy');
```

> 传递给 `require.context()` 的参数必须是字面量。
:::tip
Rspack 在编译时,会通过静态分析来解析 `require.context` 的参数,因此参数必须是[字面量](https://developer.mozilla.org/en-US/docs/Glossary/Literal)

例如,`filter` 的值不允许传入一个变量,也不允许传入 `new RegExp()` 生成的值,只能是一个正则表达式字面量。
:::
6 changes: 5 additions & 1 deletion website/docs/zh/api/runtime-api/module-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ const context = import.meta.webpackContext('./locales', {
});
```

> 传递给 `import.meta.webpackContext()` 的第一个参数必须是字面量。
:::tip
Rspack 在编译时,会通过静态分析来解析 `import.meta.webpackContext()` 的参数,因此参数必须是[字面量](https://developer.mozilla.org/en-US/docs/Glossary/Literal)

例如,`regExp` 的值不允许传入一个变量,也不允许传入 `new RegExp()` 生成的值,只能是一个正则表达式字面量。
:::

#### context API

Expand Down

0 comments on commit 3ad52de

Please sign in to comment.