Skip to content
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

支持自定义页面和组件微生成器的模板 #9974

Merged
merged 6 commits into from
Dec 16, 2022

Conversation

mysteryven
Copy link
Contributor

support: 微生成器支持使用自定义的模板

新增功能:

  1. 支持页面/组件生成器使用自定义模板

个人考虑除这两个以外的其他模板,好像对自定义模板不是特别的需要,于是就没有添加。可能是我没有想到,如果有更好的建议,我可以继续修改。

@vercel
Copy link

vercel bot commented Dec 6, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
umi ✅ Ready (Inspect) Visit Preview Dec 12, 2022 at 0:58AM (UTC)

@codecov
Copy link

codecov bot commented Dec 6, 2022

Codecov Report

❗ No coverage uploaded for pull request base (master@835d353). Click here to learn what that means.
Patch coverage: 87.01% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #9974   +/-   ##
=========================================
  Coverage          ?   30.49%           
=========================================
  Files             ?      433           
  Lines             ?    12482           
  Branches          ?     2984           
=========================================
  Hits              ?     3807           
  Misses            ?     8103           
  Partials          ?      572           
Impacted Files Coverage Δ
...es/preset-umi/src/commands/generators/component.ts 57.89% <50.00%> (ø)
...ackages/preset-umi/src/commands/generators/page.ts 89.41% <75.00%> (ø)
...ckages/preset-umi/src/commands/generators/utils.ts 37.14% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@fz6m
Copy link
Contributor

fz6m commented Dec 6, 2022

  1. 我理解只要在 generateFile 最后写入的函数加个选项,开了就自动识别基于 ${baseDir}/templates/${templateSubPath} 有没有,有的话用这个模板就可以了,不需要大面积写类似的代码,也更好扩展。

  2. 用户很有可能存在 templates 这个文件夹,开启了这个功能最好有 cosnole 打印提示,你正在使用自定义的模板。


其他: 用户并不知道模板会获得哪些参数,我感觉可以新增一个 umi g page --eject 把模板写到 templates/* ,本质上也是 generateFile 的选项,判断是写文件还是写模板。

@mysteryven
Copy link
Contributor Author

  1. 我理解只要在 generateFile 最后写入的函数加个选项,开了就自动识别基于 ${baseDir}/templates/${templateSubPath} 有没有,有的话用这个模板就可以了,不需要大面积写类似的代码,也更好扩展。
  2. 用户很有可能存在 templates 这个文件夹,开启了这个功能最好有 cosnole 打印提示,你正在使用自定义的模板。

其他: 用户并不知道模板会获得哪些参数,我感觉可以新增一个 umi g page --eject 把模板写到 templates/* ,本质上也是 generateFile 的选项,判断是写文件还是写模板。

感谢指教,三个建议都很受用,我再更改一版。

@mysteryven
Copy link
Contributor Author

不知道这样修改后是不是比之前好一些,现在把逻辑都放到 processGenerateFile 中去了。

其中的 umi g template --eject 没有基于 generateFile,因为发现此方法对 .tpl 后缀有渲染模板的行为:

if (file.endsWith('.tpl')) {
this.copyTpl({
templatePath: absFile,
target: join(opts.target, file.replace(/\.tpl$/, '')),
context: opts.context,
});
} else {

有一个点没有想到好的解决方案:

在自定义模板生成成功或者 eject 模板成功后,会 log 日志,如果在测试的时候允许这个行为,控制台 log 会比较多,可能会影响其他同学测试。经过一些尝试后,最后选的是粗暴的 mock console.log,感觉有点笨,不知道有没有其他更好的方案。log 没有选择 logger.info 也是因为没有找到一个好的方法可以拦截引入的 logger

@mysteryven mysteryven marked this pull request as ready for review December 9, 2022 02:30
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/page.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
docs: update micro-generator docs

test: condition mock log in test
docs: update doc
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/page.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
docs/docs/guides/generator.md Outdated Show resolved Hide resolved
docs/docs/guides/generator.md Show resolved Hide resolved
docs/docs/guides/generator.md Outdated Show resolved Hide resolved
docs/docs/guides/generator.md Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
packages/preset-umi/src/commands/generators/utils.ts Outdated Show resolved Hide resolved
@sorrycc sorrycc merged commit 2928b11 into umijs:master Dec 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants