cnpmcore
cnpmcore copied to clipboard
✨[Feature request] Support for unpkg-like auto-redirection capabilities
【特性描述】
支持类似 unpkg 的自动重定向能力。 如:
https://unpkg.com/@opentiny/vue-theme@~3.11/theme-tool -> https://unpkg.com/@opentiny/[email protected]/theme-tool.js https://unpkg.com/@opentiny/vue-theme@~3.11/theme -> https://unpkg.com/@opentiny/[email protected]/theme/index.js
【使用场景】
在 import map 相关的场景中使用。比如:
importmap:
{
"@opentiny/vue-theme/theme-tool": "https://unpkg.com/@opentiny/vue-theme@~3.11/theme-tool",
"@opentiny/vue-theme/theme": "https://unpkg.com/@opentiny/vue-theme@~3.11/theme"
}
使用:
import TinyThemeTool from '@opentiny/vue-theme/theme-tool' // 会自动请求 https://unpkg.com/@opentiny/vue-theme@~3.11/theme-tool ,然后重定向到 https://unpkg.com/@opentiny/[email protected]/theme-tool.js
import { tinySmbTheme } from '@opentiny/vue-theme/theme'
即支持 MDN 中 import map 提到的路径前缀 并自动重定向能力。
感谢 ❤
我们已经看到你的反馈,如果是功能缺陷,可以提供一下重现该问题的方式;如果是新功能需求,我们会尽快加入讨论。同时我们非常期待你可以加入我们的贡献者行列,让项目可以长期可持续发展。
https://www.yuque.com/egg/cnpm/files 目前支持的 files api,可以看看。 如果不满足需求,欢迎来提交 pr 实现。
$ curl https://registry.npmmirror.com/@opentiny/vue-theme/~3.11/files/theme-tool.js
Redirecting to <a href="/@opentiny/vue-theme/3.11.6/files/theme-tool.js">/@opentiny/vue-theme/3.11.6/files/theme-tool.js</a>.%
看起来是支持的
$ curl https://registry.npmmirror.com/@opentiny/vue-theme/~3.11/files/theme-tool.js Redirecting to <a href="/@opentiny/vue-theme/3.11.6/files/theme-tool.js">/@opentiny/vue-theme/3.11.6/files/theme-tool.js</a>.%
看起来是支持的
是请求 https://registry.npmmirror.com/@opentiny/vue-theme/~3.11/files/theme-tool 的时候,重定向到 https://registry.npmmirror.com/@opentiny/vue-theme/~3.11/files/theme-tool.js
importmap 是配置在前端,由浏览器来支持的吧?
看了下,应该不是 importmap,这是在浏览器侧就支持转换了,更像是后缀名的自动补齐:
https://unpkg.com/@opentiny/[email protected]/theme-tool → https://unpkg.com/@opentiny/[email protected]/theme-tool.js
去看下 unpkg 的文档是不是有类似的约定?
去看下 unpkg 的文档是不是有类似的约定?
有的,我看他们有相关的单元测试:
- https://github.com/mjackson/unpkg/blob/af8c8db00fdacd77961ab2a8c3edb45a27d3a6a3/modules/tests/directoryRedirect-test.js#L11
- https://github.com/mjackson/unpkg/blob/af8c8db00fdacd77961ab2a8c3edb45a27d3a6a3/modules/tests/directoryRedirect-test.js#L27
unpkg 具体实现的逻辑应该是这一块:https://github.com/mjackson/unpkg/blob/af8c8db00fdacd77961ab2a8c3edb45a27d3a6a3/modules/middleware/findEntry.js#L176-L190
没理解错的话,cnpmcore 应该在这个函数内尝试增加规则匹配:
- /dir/index -> 全匹配失败的话,尝试匹配 /dir/index.js 、/dir/index.json
- /dir/fileName -> 全匹配失败的话,尝试匹配 /dir/fileName.js
然后在这附近 查看查找到的文件与当前 path 是否一致,如果不一致,则302到匹配的路径
@chilingling 直接来 PR 吧~
@chilingling 直接来 PR 吧~
我试试
@chilingling 感谢开源贡献!已经成功