From 3ad52deabddff605a46876c5a078752c8d6febd9 Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 6 Sep 2024 10:18:42 +0800 Subject: [PATCH] docs: add tip for import.meta.webpackContext literal (#7815) --- website/docs/en/api/runtime-api/module-methods.mdx | 6 +++++- website/docs/en/api/runtime-api/module-variables.mdx | 10 +++++----- website/docs/zh/api/runtime-api/module-methods.mdx | 6 +++++- website/docs/zh/api/runtime-api/module-variables.mdx | 6 +++++- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/website/docs/en/api/runtime-api/module-methods.mdx b/website/docs/en/api/runtime-api/module-methods.mdx index 19971efae7a..2eb48b238eb 100644 --- a/website/docs/en/api/runtime-api/module-methods.mdx +++ b/website/docs/en/api/runtime-api/module-methods.mdx @@ -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 diff --git a/website/docs/en/api/runtime-api/module-variables.mdx b/website/docs/en/api/runtime-api/module-variables.mdx index 107e76f8dd2..8a6ffa81254 100644 --- a/website/docs/en/api/runtime-api/module-variables.mdx +++ b/website/docs/en/api/runtime-api/module-variables.mdx @@ -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 @@ -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 diff --git a/website/docs/zh/api/runtime-api/module-methods.mdx b/website/docs/zh/api/runtime-api/module-methods.mdx index 4ad517cb0d4..73b003d7adf 100644 --- a/website/docs/zh/api/runtime-api/module-methods.mdx +++ b/website/docs/zh/api/runtime-api/module-methods.mdx @@ -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()` 生成的值,只能是一个正则表达式字面量。 +::: diff --git a/website/docs/zh/api/runtime-api/module-variables.mdx b/website/docs/zh/api/runtime-api/module-variables.mdx index f8621dd675f..74c23e141eb 100644 --- a/website/docs/zh/api/runtime-api/module-variables.mdx +++ b/website/docs/zh/api/runtime-api/module-variables.mdx @@ -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