feat: stack resolve 全面插件化——alias/relative 拆为内置插件,alias 补 tsconfig paths #42

Merged
arnold.cui merged 2 commits from feat/resolve-plugins into main 2026-11-11 08:15:39 +00:00
Owner

背景

resolve 链此前是半插件化:包查找已由 pkgResolve 插件承接(package.json → node_modules 上溯 → 内存缓存),但 alias 与相对路径两级兜底仍是 engine 内建——applyAlias 前缀替换发生在插件链之前、相对/绝对 probe 与裸说明符 root 兜底发生在插件链之后,用户插件无法插手这三段,也无法替换或禁用。另外 alias 只认 config.alias,不读 tsconfig paths;且 applyAlias 的 startsWith 无边界检查(alias @utils 会误伤 @utils-extra)。

变更内容

  • engine.resolveDemand 瘦身为纯插件链:applyAlias、相对/绝对 probe、裸说明符 root 兜底三段内建逻辑全部撤出,链空即 external(入口报错单点留 resolveEntry);顺带修 alias 无边界误伤,新匹配要求 exact 或 / 边界。
  • ResolveContext 增 resolve 再派发:插件命中后把映射目标交还剩余插件链。alias 插件因此零 probe——./x 走 relative、裸名走 pkg/root 兜底,react→preact 型包别名依旧成立。
  • 新增 aliasResolve:config.alias 前缀映射 + tsconfig compilerOptions.paths(锚定 root,baseUrl 缺省即 tsconfig 目录;exact 优先、最长前缀星模式、多目标按序试解);命中后全走剩余链,全失败响亮报错不静默外化(承接原 aliased-miss throw 语义)。
  • 新增 relativeResolve:./ ../ / 相对与绝对 probe + 裸说明符 root 兜底——入口 src/app.ts 这类根相对路径依赖此路径,不可删只可搬;相对 miss 报错文案与原 engine 逐字一致。
  • 新增 tsconfigJson 叶子:JSONC 剥离状态机(字符串感知,吃注释/尾逗号/BOM)。esbuild json loader 拒注释与尾逗号,故自写;aliasResolve 不再依赖 esbuild-wasm。
  • 默认装配下沉 core/defaults.ts:withDefaultHooks 默认链 [imports, …用户 hooks, alias, pkg, relative],build() 与 CLI 共用同一套链(CLI withDefaults 只剩 fs 注入)。链序三保:vendor(用户钩)先于 alias/pkg 保住 vendor 优先 node_modules;pkg 先于 relative 保住 browser-field 对相对导入的 remap;relative 收尾。
  • builtins 导出:aliasResolve / relativeResolve / withDefaultHooks,pkgResolve 未动(三张内存缓存即 stack 的解析缓存,vendor 预包作为用户钩继续先于它)。

自测结果

  • 全量测试 72/72 绿:66 旧例零改动全过(行为保持),新增 resolvePlugins.spec 6 例——alias 边界不误伤+未命中外化、tsconfig paths 锚定 root、JSONC 容错、包名再派发+边界、alias miss 响亮报错、裸说明符 root 兜底。
  • typecheck / biome / strip-comments(0 条注释)全净。
  • stack-demo 真实工程构建冒烟:@/ alias 与 esm/min/bundle 三份产物照常落位。
## 背景 resolve 链此前是半插件化:包查找已由 pkgResolve 插件承接(package.json → node_modules 上溯 → 内存缓存),但 alias 与相对路径两级兜底仍是 engine 内建——applyAlias 前缀替换发生在插件链之前、相对/绝对 probe 与裸说明符 root 兜底发生在插件链之后,用户插件无法插手这三段,也无法替换或禁用。另外 alias 只认 config.alias,不读 tsconfig paths;且 applyAlias 的 startsWith 无边界检查(alias `@utils` 会误伤 `@utils-extra`)。 ## 变更内容 - **engine.resolveDemand 瘦身为纯插件链**:applyAlias、相对/绝对 probe、裸说明符 root 兜底三段内建逻辑全部撤出,链空即 external(入口报错单点留 resolveEntry);顺带修 alias 无边界误伤,新匹配要求 exact 或 `/` 边界。 - **ResolveContext 增 resolve 再派发**:插件命中后把映射目标交还剩余插件链。alias 插件因此零 probe——`./x` 走 relative、裸名走 pkg/root 兜底,`react→preact` 型包别名依旧成立。 - **新增 aliasResolve**:config.alias 前缀映射 + tsconfig `compilerOptions.paths`(锚定 root,baseUrl 缺省即 tsconfig 目录;exact 优先、最长前缀星模式、多目标按序试解);命中后全走剩余链,全失败响亮报错不静默外化(承接原 aliased-miss throw 语义)。 - **新增 relativeResolve**:`./ ../ /` 相对与绝对 probe + 裸说明符 root 兜底——入口 `src/app.ts` 这类根相对路径依赖此路径,不可删只可搬;相对 miss 报错文案与原 engine 逐字一致。 - **新增 tsconfigJson 叶子**:JSONC 剥离状态机(字符串感知,吃注释/尾逗号/BOM)。esbuild json loader 拒注释与尾逗号,故自写;aliasResolve 不再依赖 esbuild-wasm。 - **默认装配下沉 core/defaults.ts**:withDefaultHooks 默认链 `[imports, …用户 hooks, alias, pkg, relative]`,build() 与 CLI 共用同一套链(CLI withDefaults 只剩 fs 注入)。链序三保:vendor(用户钩)先于 alias/pkg 保住 vendor 优先 node_modules;pkg 先于 relative 保住 browser-field 对相对导入的 remap;relative 收尾。 - **builtins 导出**:aliasResolve / relativeResolve / withDefaultHooks,pkgResolve 未动(三张内存缓存即 stack 的解析缓存,vendor 预包作为用户钩继续先于它)。 ## 自测结果 - 全量测试 72/72 绿:66 旧例零改动全过(行为保持),新增 resolvePlugins.spec 6 例——alias 边界不误伤+未命中外化、tsconfig paths 锚定 root、JSONC 容错、包名再派发+边界、alias miss 响亮报错、裸说明符 root 兜底。 - typecheck / biome / strip-comments(0 条注释)全净。 - stack-demo 真实工程构建冒烟:`@/` alias 与 esm/min/bundle 三份产物照常落位。
arnold.cui added 1 commit 2026-11-11 07:57:20 +00:00
engine.resolveDemand 瘦身为纯插件链:applyAlias 前缀替换、相对/绝对 probe、裸说明符 root 兜底三段全部撤出,链空即 external(入口报错单点留 resolveEntry);顺带修 applyAlias 无边界 startsWith 误伤(alias @utils 会吞 @utils-extra,新匹配要求 exact 或边界 /)。

ResolveContext 增 resolve 再派发:插件命中后把映射目标交还剩余插件链,alias 插件因此零 probe——./x 走 relative、裸名走 pkg/root 兜底、react→preact 型包别名依旧成立。链序 defaults [imports, …用户 hooks, alias, pkg, relative]:vendor(用户钩)先于 alias/pkg 保住 vendor 优先 node_modules;pkg 先于 relative 保住 browser-field 对相对导入的 remap;relative 收尾,相对 miss 报错文案与原 engine 逐字一致。

新增 aliasResolve:config.alias 前缀映射 + tsconfig compilerOptions.paths(锚定 root,baseUrl 缺省即 tsconfig 目录;exact 优先、最长前缀星模式、多目标按序试解);命中后全走剩余链,全失败响亮报错不静默外化(承接原 aliased-miss throw)。tsconfig 文本解析落 tsconfigJson 叶子:JSONC 剥离注释/尾逗号/BOM 的字符串感知状态机(esbuild json loader 拒注释与尾逗号,故自写,aliasResolve 不再依赖 esbuild-wasm)。

新增 relativeResolve:./ ../ / 相对与绝对 probe + 裸说明符 root 兜底——入口 src/app.ts 这类根相对路径依赖此路径,不可删只可搬。

默认装配下沉 core/defaults.ts withDefaultHooks,build() 与 CLI 共用同一套链(CLI withDefaults 只剩 fs 注入);builtins 导出 aliasResolve / relativeResolve / withDefaultHooks。

测试 resolvePlugins.spec 六例:alias 边界不误伤+未命中外化、tsconfig paths 锚定 root、JSONC 容错、包名再派发+边界、alias miss 响亮报错、裸说明符 root 兜底;全量 72 绿,stack-demo 真实工程构建冒烟通过(@/ alias 与三份产物照常)。
@@ -0,0 +57,4 @@
}
const unresolved = (specifier: string, importer: string, ctx: ResolveContext): never => {
throw new Error(`[stack] 无法解析导入 "${specifier}"(来自 ${importer},root=${ctx.root})`)
Author
Owner

用英文

用英文
Author
Owner

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。
@@ -15,7 +16,7 @@ export async function build(config: StackConfig): Promise<BuildResult> {
}
if ((config.inputs ?? []).length === 0) throw new Error('[stack] config.inputs 至少需要一个入口')
Author
Owner

换成英文

换成英文
Author
Owner

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。
@@ -120,3 +101,3 @@
const resolution = await this.resolveDemand(input.path, `${this.root}/`, { entry: true })
const resolution = await this.resolveDemand(input.path, `${this.root}/`)
if (resolution.kind === 'external') {
throw new Error(`[stack] 无法解析入口 "${input.path}"(root=${this.root})`)
Author
Owner

换成英文

换成英文
Author
Owner

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。
@@ -0,0 +19,4 @@
: joinPath(dirnamePath(importer), specifier)
const id = await probeFirst(ctx.fs, moduleCandidates(base))
if (id !== null) return { id }
throw new Error(`[stack] 无法解析导入 "${specifier}"(来自 ${importer})`)
Author
Owner

用英文

用英文
Author
Owner

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。
@@ -0,0 +12,4 @@
const file = output.files.find((candidate) => candidate.path === path)
assert.ok(
file !== undefined,
`缺少产物文件 ${path}(现有:${output.files.map((f) => f.path).join('、')})`,
Author
Owner

用英文

用英文
Author
Owner

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。
@@ -0,0 +17,4 @@
return file.code
}
test('config.alias 前缀替换走插件:边界不误伤,未命中别名外部化为原说明符', async () => {
Author
Owner

用英文

用英文
Author
Owner

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。
@@ -0,0 +41,4 @@
assert.match(app, /from "@utils-extra\/pkg"/)
})
test('tsconfig paths:无 config.alias 时按 paths 映射,锚定项目根', async () => {
Author
Owner

用英文

用英文
Author
Owner

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。
@@ -0,0 +61,4 @@
assert.match(app, /from "\.\/nested\/inner\.js"/)
})
test('tsconfig 含注释与尾逗号时按 JSONC 剥离解析,paths 依然生效', async () => {
Author
Owner

用英文

用英文
Author
Owner

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。
@@ -0,0 +83,4 @@
assert.match(codeOf(result, 'app.js'), /from "\.\/info\.js"/)
})
test('alias 目标为包名时再派发插件链:react 换 preact 落 node_modules,边界不吞 react-dom', async () => {
Author
Owner

用英文

用英文
Author
Owner

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。
Author
Owner

代码中存在比较多的中文,调整为英文。

代码中存在比较多的中文,调整为英文。
arnold.cui added 1 commit 2026-11-11 08:05:47 +00:00
arnold.cui 评审(PR #42 评论 670-686、688):代码中存在较多中文,调整为英文。engine/build/aliasResolve/relativeResolve 四个源文件的 throw 文案与 resolvePlugins.spec 六个用例名、断言消息全部换英文([stack] 前缀与 specifier/importer/root 定位信息保留);scenarios.ts 两处 error 断言随文案联动(无法解析导入 → cannot resolve import)。scenario 与旧 spec 用例名保持中文,未在评审点名范围。
Author
Owner

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。

已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。
arnold.cui merged commit 20728a4039 into main 2026-11-11 08:15:39 +00:00
arnold.cui deleted branch feat/resolve-plugins 2026-11-11 08:15:39 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: idealjs/stack#42