cnpm / cnpmcore

Private NPM Registry for Enterprise
https://npmmirror.com
MIT License
573 stars 78 forks source link

✨[Feature request] Support for unpkg-like auto-redirection capabilities #674

Closed chilingling closed 1 month ago

chilingling commented 2 months ago

【特性描述】

支持类似 unpkg 的自动重定向能力。 如:

https://unpkg.com/@opentiny/vue-theme@~3.11/theme-tool -> https://unpkg.com/@opentiny/vue-theme@3.11.6/theme-tool.js https://unpkg.com/@opentiny/vue-theme@~3.11/theme -> https://unpkg.com/@opentiny/vue-theme@3.11.6/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/vue-theme@3.11.6/theme-tool.js
import { tinySmbTheme } from '@opentiny/vue-theme/theme'

即支持 MDN 中 import map 提到的路径前缀 并自动重定向能力。

image

感谢 ❤

github-actions[bot] commented 2 months ago

我们已经看到你的反馈,如果是功能缺陷,可以提供一下重现该问题的方式;如果是新功能需求,我们会尽快加入讨论。同时我们非常期待你可以加入我们的贡献者行列,让项目可以长期可持续发展。

fengmk2 commented 2 months ago

https://www.yuque.com/egg/cnpm/files 目前支持的 files api,可以看看。 如果不满足需求,欢迎来提交 pr 实现。

atian25 commented 2 months ago
$ 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>.%   

看起来是支持的

chilingling commented 2 months ago
$ 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

atian25 commented 2 months ago

importmap 是配置在前端,由浏览器来支持的吧?

chilingling commented 2 months ago

我做了个简单的 复现demo 在 demo 可以发现,theme-tool 和 theme 的两个请求挂了。 unpkg 的 对比demo ,请求正常,渲染成功

atian25 commented 2 months ago

image image

看了下,应该不是 importmap,这是在浏览器侧就支持转换了,更像是后缀名的自动补齐:

https://unpkg.com/@opentiny/vue-theme@3.14.1/theme-toolhttps://unpkg.com/@opentiny/vue-theme@3.14.1/theme-tool.js

atian25 commented 2 months ago

去看下 unpkg 的文档是不是有类似的约定?

chilingling commented 2 months ago

去看下 unpkg 的文档是不是有类似的约定?

有的,我看他们有相关的单元测试:

chilingling commented 2 months ago

unpkg 具体实现的逻辑应该是这一块:https://github.com/mjackson/unpkg/blob/af8c8db00fdacd77961ab2a8c3edb45a27d3a6a3/modules/middleware/findEntry.js#L176-L190

chilingling commented 2 months ago

没理解错的话,cnpmcore 应该在这个函数内尝试增加规则匹配:

然后在这附近 查看查找到的文件与当前 path 是否一致,如果不一致,则302到匹配的路径

atian25 commented 2 months ago

@chilingling 直接来 PR 吧~

chilingling commented 2 months ago

@chilingling 直接来 PR 吧~

我试试

fengmk2 commented 1 month ago

@chilingling 感谢开源贡献!已经成功

image