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

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

Closed
chilingling opened this issue Apr 29, 2024 · 14 comments · Fixed by #675
Closed

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

chilingling opened this issue Apr 29, 2024 · 14 comments · Fixed by #675
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@chilingling
Copy link
Contributor

【特性描述】

支持类似 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

感谢 ❤

Copy link

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

@fengmk2
Copy link
Member

fengmk2 commented Apr 29, 2024

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

@fengmk2 fengmk2 added good first issue Good for newcomers help wanted Extra attention is needed labels Apr 29, 2024
@atian25
Copy link
Contributor

atian25 commented Apr 30, 2024

$ 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
Copy link
Contributor Author

$ 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
Copy link
Contributor

atian25 commented Apr 30, 2024

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

@chilingling
Copy link
Contributor Author

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

@atian25
Copy link
Contributor

atian25 commented Apr 30, 2024

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
Copy link
Contributor

atian25 commented Apr 30, 2024

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

@chilingling
Copy link
Contributor Author

@chilingling
Copy link
Contributor Author

@chilingling
Copy link
Contributor Author

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

  • /dir/index -> 全匹配失败的话,尝试匹配 /dir/index.js 、/dir/index.json
  • /dir/fileName -> 全匹配失败的话,尝试匹配 /dir/fileName.js

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

@atian25
Copy link
Contributor

atian25 commented May 1, 2024

@chilingling 直接来 PR 吧~

@chilingling
Copy link
Contributor Author

@chilingling 直接来 PR 吧~

我试试

chilingling added a commit to chilingling/cnpmcore that referenced this issue May 8, 2024
chilingling added a commit to chilingling/cnpmcore that referenced this issue May 8, 2024
@fengmk2 fengmk2 linked a pull request May 9, 2024 that will close this issue
chilingling added a commit to chilingling/cnpmcore that referenced this issue May 14, 2024
chilingling added a commit to chilingling/cnpmcore that referenced this issue May 15, 2024
fengmk2 pushed a commit that referenced this issue May 16, 2024
closes #674

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Implemented a feature to handle compatibility with unpkg by searching
for and redirecting to possible file entries if the requested file is
not found.

- **Tests**
- Added a new test case to ensure the redirection to possible file
entries functions correctly.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
fengmk2 pushed a commit that referenced this issue May 16, 2024
[skip ci]

## [3.58.0](v3.57.0...v3.58.0) (2024-05-16)

### Features

* support unpkg alias path access entry file [#674](#674) ([#675](#675)) ([a51891d](a51891d))
@fengmk2
Copy link
Member

fengmk2 commented May 16, 2024

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

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants