feat/resolve-plugins
main
resolve 链此前是半插件化:包查找已由 pkgResolve 插件承接(package.json → node_modules 上溯 → 内存缓存),但 alias 与相对路径两级兜底仍是 engine 内建——applyAlias 前缀替换发生在插件链之前、相对/绝对 probe 与裸说明符 root 兜底发生在插件链之后,用户插件无法插手这三段,也无法替换或禁用。另外 alias 只认 config.alias,不读 tsconfig paths;且 applyAlias 的 startsWith 无边界检查(alias @utils 会误伤 @utils-extra)。
@utils
@utils-extra
/
./x
react→preact
compilerOptions.paths
./ ../ /
src/app.ts
[imports, …用户 hooks, alias, pkg, relative]
@/
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})`)
用英文
已在 b24b832 换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。
b24b832
@@ -15,7 +16,7 @@ export async function build(config: StackConfig): Promise<BuildResult> {
if ((config.inputs ?? []).length === 0) throw new Error('[stack] config.inputs 至少需要一个入口')
换成英文
@@ -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})`)
@@ -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})`)
@@ -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('、')})`,
@@ -0,0 +17,4 @@
return file.code
test('config.alias 前缀替换走插件:边界不误伤,未命中别名外部化为原说明符', async () => {
@@ -0,0 +41,4 @@
assert.match(app, /from "@utils-extra\/pkg"/)
})
test('tsconfig paths:无 config.alias 时按 paths 映射,锚定项目根', async () => {
@@ -0,0 +61,4 @@
assert.match(app, /from "\.\/nested\/inner\.js"/)
test('tsconfig 含注释与尾逗号时按 JSONC 剥离解析,paths 依然生效', async () => {
@@ -0,0 +83,4 @@
assert.match(codeOf(result, 'app.js'), /from "\.\/info\.js"/)
test('alias 目标为包名时再派发插件链:react 换 preact 落 node_modules,边界不吞 react-dom', async () => {
代码中存在比较多的中文,调整为英文。
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 用例名保持中文,未在评审点名范围。
No dependencies set.
The note is not visible to the blocked user.
背景
resolve 链此前是半插件化:包查找已由 pkgResolve 插件承接(package.json → node_modules 上溯 → 内存缓存),但 alias 与相对路径两级兜底仍是 engine 内建——applyAlias 前缀替换发生在插件链之前、相对/绝对 probe 与裸说明符 root 兜底发生在插件链之后,用户插件无法插手这三段,也无法替换或禁用。另外 alias 只认 config.alias,不读 tsconfig paths;且 applyAlias 的 startsWith 无边界检查(alias
@utils会误伤@utils-extra)。变更内容
/边界。./x走 relative、裸名走 pkg/root 兜底,react→preact型包别名依旧成立。compilerOptions.paths(锚定 root,baseUrl 缺省即 tsconfig 目录;exact 优先、最长前缀星模式、多目标按序试解);命中后全走剩余链,全失败响亮报错不静默外化(承接原 aliased-miss throw 语义)。./ ../ /相对与绝对 probe + 裸说明符 root 兜底——入口src/app.ts这类根相对路径依赖此路径,不可删只可搬;相对 miss 报错文案与原 engine 逐字一致。[imports, …用户 hooks, alias, pkg, relative],build() 与 CLI 共用同一套链(CLI withDefaults 只剩 fs 注入)。链序三保:vendor(用户钩)先于 alias/pkg 保住 vendor 优先 node_modules;pkg 先于 relative 保住 browser-field 对相对导入的 remap;relative 收尾。自测结果
@/alias 与 esm/min/bundle 三份产物照常落位。@@ -0,0 +57,4 @@}const unresolved = (specifier: string, importer: string, ctx: ResolveContext): never => {throw new Error(`[stack] 无法解析导入 "${specifier}"(来自 ${importer},root=${ctx.root})`)用英文
已在
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 至少需要一个入口')换成英文
已在
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})`)换成英文
已在
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})`)用英文
已在
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('、')})`,用英文
已在
b24b832换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。@@ -0,0 +17,4 @@return file.code}test('config.alias 前缀替换走插件:边界不误伤,未命中别名外部化为原说明符', async () => {用英文
已在
b24b832换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。@@ -0,0 +41,4 @@assert.match(app, /from "@utils-extra\/pkg"/)})test('tsconfig paths:无 config.alias 时按 paths 映射,锚定项目根', async () => {用英文
已在
b24b832换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。@@ -0,0 +61,4 @@assert.match(app, /from "\.\/nested\/inner\.js"/)})test('tsconfig 含注释与尾逗号时按 JSONC 剥离解析,paths 依然生效', async () => {用英文
已在
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 () => {用英文
已在
b24b832换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。代码中存在比较多的中文,调整为英文。
已在
b24b832换英文:源码 throw 文案与测试用例名、断言消息均已英文化,scenarios 两处 error 断言随文案联动,全量 72 绿。