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

[Question] 针对Vue cli加载插件模块的一点疑惑 #8

Closed
screetBloom opened this issue Feb 24, 2020 · 4 comments
Closed

[Question] 针对Vue cli加载插件模块的一点疑惑 #8

screetBloom opened this issue Feb 24, 2020 · 4 comments

Comments

@screetBloom
Copy link

首先感谢你的分享给我阅读源码时提供的帮助

Background

vue cli加载插件模块使用的是自建的loadModule,而不是直接使用require。
这个点在源代码中频繁出现,但是大家似乎都没分析过这个点,个人很好奇为什么vue cli一定要自己实现一遍
在cli-service中加载模块的代码示例
image

loadModule具体实现的源码路径
image

sodatea 提交 Pr的issue


Question

我个人初期看到的时候觉得没必要自己去实现require,包括去看了NodeJs Module模块createRequire、createRequireFromPath的说明,也并没有看到需要自己去实现的一个必要性,但是其实这个函数贯穿了插件模块使用的全流程,让人比较困惑

想咨询一下您对【loadModule】部分的看法,以及是否有必要自建require


建议

你可以在README中贴一下alipay或者wechat pay的地址,或许我可以请你喝杯咖啡



KuangPF added a commit that referenced this issue Feb 25, 2020
@KuangPF
Copy link
Owner

KuangPF commented Feb 25, 2020

这一点其实我也不是很熟悉,不过从代码中的信息可以看出应该有以下几个原因:

  • require.resolve 在不同上下中解析依赖的时候会存在一定的问题
  • createRequire 是从不同上下文解析模块的唯一语义正确的方法
  • 自定义的 createRequire 方法兼容了不同版本的 node 版本,并做对应的处理

如果有更好的答案,欢迎大佬来回答。

最后很感谢能够有想法请我喝一杯咖啡☕️5fa2b97

@screetBloom
Copy link
Author

好的,感谢你的回复
你的回答在思路上解答了我的一些问题,请喝咖啡是应该的
另外这个issue我暂时先不关,等确认之后或者这几天没结论我再来关

@screetBloom
Copy link
Author

screetBloom commented Feb 26, 2020

目前我个人的结论是,loadModule主要提供了一种相对于第二个参数输入的上下文进行依赖包解析的能力,比如其在@vue/cli-plugin-eslint/index.js这部分

image

利用loadModule相对引用这一特性,可以做到优先使用用户配置的eslint,如果业务没配置再使用内置的eslint

社区中关于这一点的详细pr

@KuangPF
Copy link
Owner

KuangPF commented Feb 26, 2020

@screetBloom 解析的到位

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

No branches or pull requests

2 participants