-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: require context #1390
feat: require context #1390
Conversation
Walkthrough此次变更主要引入了一个新的 Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Compiler
participant RequireContextPlugin
participant JavaScript Module
User->>Compiler: 编译请求
Compiler->>RequireContextPlugin: 检查 experimental.requireContext
RequireContextPlugin->>JavaScript Module: 加载和转换
JavaScript Module->>RequireContextPlugin: 模块结果
RequireContextPlugin-->>Compiler: 转换完成
Compiler-->>User: 编译结束
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
Outside diff range and nitpick comments (9)
e2e/fixtures/javascript.require-context/sync-case.js (4)
3-3
: 修正拼写错误
directorie
应为directories
。- it("sync: no sub directorie", () => { + it("sync: no sub directories", () => {
28-28
: 修正拼写错误
directorie
应为directories
。- it("sync: with sub directorie", () => { + it("sync: with sub directories", () => {
40-42
: 修正拼写错误
convetion
应为convention
。- it(" follows swebpacl id convetion", () => { + it(" follows webpack id convention", () => {Tools
GitHub Check: Spell Check
[warning] 40-40:
"convetion" should be "convention".
45-49
: 修正拼写错误
unknow
应为unknown
。- it("throws when resolve unknow request", () => { + it("throws when resolve unknown request", () => {e2e/fixtures/javascript.require-context/lazy-case.js (5)
8-8
: 修正拼写错误
directorie
应为directories
。- it("lazy: no sub directorie", () => { + it("lazy: no sub directories", () => {
28-28
: 修正拼写错误
directorie
应为directories
。- it("lazy: with sub directorie", () => { + it("lazy: with sub directories", () => {
40-42
: 修正拼写错误
convetion
应为convention
。- it(" follows swebpacl id convetion", () => { + it(" follows webpack id convention", () => {Tools
GitHub Check: Spell Check
[warning] 40-40:
"convetion" should be "convention".
45-49
: 修正拼写错误
unknow
应为unknown
。- it("throws when resolve unknow request", () => { + it("throws when resolve unknown request", () => {
51-55
: 修正拼写错误
unknow
应为unknown
。- it("rejects when require unknow request", () => { + it("rejects when require unknown request", () => {
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (25)
- crates/mako/src/build/parse.rs (1 hunks)
- crates/mako/src/compiler.rs (1 hunks)
- crates/mako/src/config/config.rs (2 hunks)
- crates/mako/src/plugins/mod.rs (1 hunks)
- crates/mako/src/plugins/require_context.rs (1 hunks)
- crates/mako/src/plugins/require_context/param.rs (1 hunks)
- crates/mako/src/plugins/require_context/render.rs (1 hunks)
- crates/mako/src/plugins/require_context/visitor.rs (1 hunks)
- e2e/fixtures/javascript.require-context/context/a.js (1 hunks)
- e2e/fixtures/javascript.require-context/context/b.js (1 hunks)
- e2e/fixtures/javascript.require-context/context/dir/c.js (1 hunks)
- e2e/fixtures/javascript.require-context/context/dir/d.js (1 hunks)
- e2e/fixtures/javascript.require-context/context/dir/index.js (1 hunks)
- e2e/fixtures/javascript.require-context/context/index.js (1 hunks)
- e2e/fixtures/javascript.require-context/expect.js (1 hunks)
- e2e/fixtures/javascript.require-context/index.js (1 hunks)
- e2e/fixtures/javascript.require-context/lazy-case.js (1 hunks)
- e2e/fixtures/javascript.require-context/lazy-context/a.js (1 hunks)
- e2e/fixtures/javascript.require-context/lazy-context/b.js (1 hunks)
- e2e/fixtures/javascript.require-context/lazy-context/dir/c.js (1 hunks)
- e2e/fixtures/javascript.require-context/lazy-context/dir/d.js (1 hunks)
- e2e/fixtures/javascript.require-context/lazy-context/dir/index.js (1 hunks)
- e2e/fixtures/javascript.require-context/lazy-context/index.js (1 hunks)
- e2e/fixtures/javascript.require-context/mako.config.json (1 hunks)
- e2e/fixtures/javascript.require-context/sync-case.js (1 hunks)
Files skipped from review due to trivial changes (12)
- e2e/fixtures/javascript.require-context/context/a.js
- e2e/fixtures/javascript.require-context/context/b.js
- e2e/fixtures/javascript.require-context/context/dir/c.js
- e2e/fixtures/javascript.require-context/context/dir/d.js
- e2e/fixtures/javascript.require-context/index.js
- e2e/fixtures/javascript.require-context/lazy-context/a.js
- e2e/fixtures/javascript.require-context/lazy-context/b.js
- e2e/fixtures/javascript.require-context/lazy-context/dir/c.js
- e2e/fixtures/javascript.require-context/lazy-context/dir/d.js
- e2e/fixtures/javascript.require-context/lazy-context/dir/index.js
- e2e/fixtures/javascript.require-context/lazy-context/index.js
- e2e/fixtures/javascript.require-context/mako.config.json
Additional context used
GitHub Check: Spell Check
e2e/fixtures/javascript.require-context/context/index.js
[warning] 1-1:
"conext" should be "context" or "connect" or "connects".e2e/fixtures/javascript.require-context/context/dir/index.js
[warning] 1-1:
"conext" should be "context" or "connect" or "connects".e2e/fixtures/javascript.require-context/sync-case.js
[warning] 40-40:
"convetion" should be "convention".e2e/fixtures/javascript.require-context/lazy-case.js
[warning] 40-40:
"convetion" should be "convention".
Additional comments not posted (46)
e2e/fixtures/javascript.require-context/expect.js (1)
1-13
: 代码通过审查测试代码结构正确,功能正常。
crates/mako/src/plugins/mod.rs (1)
13-13
: 代码通过审查新模块
require_context
的添加符合 PR 目标。e2e/fixtures/javascript.require-context/sync-case.js (8)
4-6
: 测试通过,代码正确这个测试用例检查了
context.keys()
的返回值,代码逻辑正确。
8-13
: 测试通过,代码正确这个测试用例验证了
context.resolve
和require
的行为,代码逻辑正确。
15-19
: 测试通过,代码正确这个测试用例直接通过
context
来 require 模块,代码逻辑正确。
21-23
: 测试通过,代码正确这个测试用例检查
context.id
的值,代码逻辑正确。
29-37
: 测试通过,代码正确这个测试用例检查了
context2.keys()
的返回值,代码逻辑正确。
45-49
: 测试通过,代码正确这个测试用例检查了
context.resolve
在请求不存在的模块时抛出异常的行为,代码逻辑正确。
26-43
: 测试通过,代码正确这个测试用例检查了
context2.keys()
的返回值,代码逻辑正确。Tools
GitHub Check: Spell Check
[warning] 40-40:
"convetion" should be "convention".
45-49
: 测试通过,代码正确这个测试用例检查了
context.resolve
在请求不存在的模块时抛出异常的行为,代码逻辑正确。e2e/fixtures/javascript.require-context/lazy-case.js (6)
9-11
: 测试通过,代码正确这个测试用例检查了
context.keys()
的返回值,代码逻辑正确。
13-19
: 测试通过,代码正确这个测试用例验证了
context.resolve
和require
的行为,代码逻辑正确。
21-23
: 测试通过,代码正确这个测试用例检查
context.id
的值,代码逻辑正确。
29-37
: 测试通过,代码正确这个测试用例检查了
context2.keys()
的返回值,代码逻辑正确。
45-49
: 测试通过,代码正确这个测试用例检查了
context.resolve
在请求不存在的模块时抛出异常的行为,代码逻辑正确。
51-55
: 测试通过,代码正确这个测试用例检查了
context
在请求不存在的模块时抛出异常的行为,代码逻辑正确。crates/mako/src/plugins/require_context.rs (3)
21-23
: 测试通过,代码正确这个函数返回插件的名称,代码逻辑正确。
25-54
: 测试通过,代码正确这个函数处理虚拟上下文模块的加载,代码逻辑正确。
56-72
: 测试通过,代码正确这个函数使用访问者模式处理 JavaScript 代码转换,代码逻辑正确。
crates/mako/src/build/parse.rs (2)
32-33
: 测试通过,代码正确新的
InvalidExpression
错误变体包含message
和path
字段,代码逻辑正确。
Line range hint
38-179
: 测试通过,代码正确
Parse
结构体的parse
方法逻辑正确,处理了不同类型的内容和错误情况。crates/mako/src/plugins/require_context/visitor.rs (10)
25-38
: 方法is_require_context
看起来不错,但请确保处理边界情况。该方法检查给定的调用表达式是否为
require.context
调用。请验证它是否能正确处理所有边界情况。
40-48
: 方法to_context_param
看起来不错,但请确保处理无效参数。该方法使用构建器模式将
require.context
调用表达式转换为ContextParam
对象。请验证构建器是否能正确处理无效参数。
50-62
: 方法is_valid_args
看起来不错,但请确保覆盖所有可能的参数类型和边界情况。该方法检查
require.context
调用的参数是否有效。请验证它是否涵盖所有可能的参数类型和边界情况。
66-96
: 方法visit_mut_expr
看起来不错,但请确保转换逻辑的健壮性。该方法访问并可能转换 JavaScript 表达式,特别是处理
require.context
调用。请验证转换逻辑的健壮性,并确保处理所有预期情况。
108-126
: 方法transform_code
看起来不错,但请确保测试覆盖率。该方法是一个使用
RequireContextVisitor
转换 JavaScript 代码的测试助手。请验证它是否涵盖各种测试场景。
128-137
: 测试normal_sync
看起来不错,但请确保覆盖同步调用的边界情况。该测试检查同步
require.context
调用的转换。请验证它是否涵盖同步调用的边界情况。
139-148
: 测试normal_sync_with_sub_directories
看起来不错,但请确保覆盖子目录处理的边界情况。该测试检查带有子目录的同步
require.context
调用的转换。请验证它是否涵盖子目录处理的边界情况。
150-162
: 测试normal_sync_ignore_case_sensitive
看起来不错,但请确保覆盖不区分大小写处理的边界情况。该测试检查带有不区分大小写正则表达式的同步
require.context
调用的转换。请验证它是否涵盖不区分大小写处理的边界情况。
164-176
: 测试all_default_value
看起来不错,但请确保覆盖默认值的边界情况。该测试检查带有默认值的
require.context
调用的转换。请验证它是否涵盖默认值的边界情况。
178-188
: 测试invalid_require_context
看起来不错,但请确保覆盖各种无效场景并最终启用测试。该测试标记为忽略,检查无效
require.context
调用的转换。请验证它是否涵盖各种无效场景并最终启用测试。crates/mako/src/plugins/require_context/param.rs (6)
25-43
: 方法ContextParam::to_context_id
看起来不错,但请确保处理所有边界情况和无效输入。该方法根据各种参数生成上下文 ID 字符串。请验证它是否能正确处理所有边界情况和无效输入。
101-117
: 方法ContextParamBuilder::relative_path
看起来不错,但请确保处理无效参数。该方法为上下文参数构建器设置相对路径。请验证它是否能正确处理无效参数。
119-137
: 方法ContextParamBuilder::sub_directories
看起来不错,但请确保处理无效参数。该方法为上下文参数构建器设置子目录标志。请验证它是否能正确处理无效参数。
139-165
: 方法ContextParamBuilder::mode
看起来不错,但请确保处理无效参数。该方法为上下文参数构建器设置加载模式。请验证它是否能正确处理无效参数。
167-186
: 方法ContextParamBuilder::reg_expr
看起来不错,但请确保处理无效参数。该方法为上下文参数构建器设置正则表达式。请验证它是否能正确处理无效参数。
188-200
: 方法ContextParamBuilder::build
看起来不错,但请确保处理所有边界情况和无效输入。该方法在所有参数有效时构建
ContextParam
对象。请验证它是否能正确处理所有边界情况和无效输入。crates/mako/src/config/config.rs (2)
429-429
: 新增字段enable_require_context
在
ExperimentalConfig
结构体中新增了enable_require_context
字段,并默认设置为true
。此更改符合 PR 的目标,确保此字段的添加不会对现有配置产生负面影响。
701-701
: 更新默认配置以包含enableRequireContext
设置默认配置已更新,在
experimental
部分中包含"enableRequireContext": true
。此更改确保新功能默认启用。请验证默认配置的更新是否正确,并考虑其对现有配置的影响。crates/mako/src/plugins/require_context/render.rs (6)
101-145
: 路径验证和错误处理在
matched_files
方法中,已经包含了对无效路径的错误处理,这是一个很好的实践。建议确保所有可能的错误情况都能被覆盖,并在必要时添加更多详细的错误信息。
147-160
: 方法实现良好
module_id_map
方法实现良好,能够正确生成模块 ID 映射的 JavaScript 字符串。
162-164
: 方法实现良好
module_import
方法实现简洁明了,正确调用render_module_import
生成导入字符串。
166-168
: 方法实现良好
module_context
方法实现简洁明了,正确调用ContextLoadMode::module_context
生成上下文字符串。
170-201
: 方法实现良好
render
方法实现良好,能够正确组合各个部分生成最终的 JavaScript 字符串。
204-239
: 方法实现良好
try_from
方法实现良好,能够正确处理无效输入并解析必要字段。crates/mako/src/compiler.rs (1)
245-247
: 条件逻辑实现良好根据
enable_require_context
设置条件性地将RequireContextPlugin
插件推入plugins
向量中的逻辑实现良好。
fn id(&self) -> String { | ||
// TODO align with webpack | ||
format!( | ||
"./{}/ {} {}{}/", | ||
self.root, | ||
self.mode, | ||
if self.sub_directories { | ||
"" | ||
} else { | ||
"nonrecursive " | ||
}, | ||
&self.reg, | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
待办事项:与 webpack 对齐
在 id
方法中,有一个待办事项注释,提示需要与 webpack 对齐。建议尽快处理此待办事项,以确保功能的一致性和兼容性。
您是否需要帮助来处理这一待办事项?
对性能的影响是? |
文档需要补下。 |
如果没有使用 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (3)
- e2e/fixtures/javascript.require-context/context/index.js (1 hunks)
- e2e/fixtures/javascript.require-context/lazy-case.js (1 hunks)
- e2e/fixtures/javascript.require-context/sync-case.js (1 hunks)
Files skipped from review as they are similar to previous changes (3)
- e2e/fixtures/javascript.require-context/context/index.js
- e2e/fixtures/javascript.require-context/lazy-case.js
- e2e/fixtures/javascript.require-context/sync-case.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
0e29cb2
to
0882523
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (2)
- docs/config.md (1 hunks)
- e2e/fixtures/javascript.require-context/context/dir/index.js (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- e2e/fixtures/javascript.require-context/context/dir/index.js
Additional comments not posted (1)
docs/config.md (1)
227-242
: 新增的配置项experimental.enableRequireContext
新增的配置项
experimental.enableRequireContext
提供了类型定义、默认值、描述和示例用法,文档清晰易懂,符合现有文档风格。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- crates/mako/src/plugins/require_context/visitor.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- crates/mako/src/plugins/require_context/visitor.rs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (3)
- crates/mako/src/compiler.rs (1 hunks)
- crates/mako/src/config/config.rs (2 hunks)
- docs/config.md (1 hunks)
Files skipped from review as they are similar to previous changes (3)
- crates/mako/src/compiler.rs
- crates/mako/src/config/config.rs
- docs/config.md
补齐部分对齐 webpack require.context API
Summary by CodeRabbit
require.context
实验特性,为基于配置设置加载和转换模块提供支持。experimental.requireContext
,默认设置为true
。RequireContextPlugin
。docs/config.md
中新增了experimental.requireContext
的配置说明。require.context
功能引入了一系列新的测试用例,确保功能正确性和完整性。