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

fix: 更改/开头路径导入报错问题 #165

Closed
wants to merge 3 commits into from
Closed

fix: 更改/开头路径导入报错问题 #165

wants to merge 3 commits into from

Conversation

fahasikei
Copy link

@fahasikei fahasikei commented Sep 5, 2023

这个 PR 做了什么? (简要描述所做更改)

更改/开头路径导入报错问题

这个 PR 是什么类型? (至少选择一个)

  • 错误修复(Bugfix) issue: fix #
  • 新功能(Feature)
  • 代码重构(Refactor)
  • TypeScript 类型定义修改(Typings)
  • 文档修改(Docs)
  • 代码风格更新(Code style update)
  • 其他,请描述(Other, please describe):

这个 PR 涉及以下平台:

  • 所有小程序
  • 微信小程序
  • 支付宝小程序
  • 百度小程序
  • 字节跳动小程序
  • QQ 轻应用
  • 京东小程序
  • 快应用平台(QuickApp)
  • Web 平台(H5)
  • 移动端(React-Native)
  • 鸿蒙(harmony)

@@ -23,31 +23,38 @@ export function getPkgVersion (): string {
return require(path.join(getRootPath(), 'package.json')).version
}

function getRelativePath (rootPath: string, sourceFilePath: string, oriPath: string) {
function revertScriptPath (p: string, extArrs = SCRIPT_EXT): string {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

变量名不要用单个字符

if (relativePath.indexOf('.') !== 0) {
return './' + relativePath
const absSearchPath = revertScriptPath(path.resolve(sourceFilePath, '..' + oriPath))
let absolutePath = path.relative(path.dirname(sourceFilePath), absSearchPath)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是相对路径,absSearchPath变量名是否不合适

relativePath = promoteRelativePath(relativePath)
if (relativePath.indexOf('.') !== 0) {
return './' + relativePath
const absSearchPath = revertScriptPath(path.resolve(sourceFilePath, '..' + oriPath))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

返回为空字符串的情况有没有考虑

// 微信小程序中js文件的引用可不加后缀,需考虑
return `./${oriPath}.js`
const vpath = revertScriptPath(path.resolve(sourceFilePath, '..', oriPath))
const absolutePath = path.relative(path.dirname(sourceFilePath), vpath)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

相对路径为啥叫absolutePath

return `./${oriPath}.js`
const vpath = revertScriptPath(path.resolve(sourceFilePath, '..', oriPath))
const absolutePath = path.relative(path.dirname(sourceFilePath), vpath)
if (vpath) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果vpath是空字符串呢

@@ -23,31 +23,38 @@ export function getPkgVersion (): string {
return require(path.join(getRootPath(), 'package.json')).version
}

function getRelativePath (rootPath: string, sourceFilePath: string, oriPath: string) {
function revertScriptPath (p: string, extArrs = SCRIPT_EXT): string {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把SCRIPT_EXT的内容提取出来加个注释吧

} else if (fs.existsSync(`${p}${item}`)){
return `${p}${item}`
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

遍历如果可以的话,尽量用对象或数组自身的方法,这里可以用foreach,for循环中的索引命名也尽量有意义。

let absolutePath = path.relative(path.dirname(sourceFilePath), absSearchPath)
absolutePath = promoteRelativePath(absolutePath)
if (absolutePath.indexOf('.') !== 0) {
return './' + absolutePath

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多处的字符串拼接,尽量采取同一种形式,建议采用模板字符串继续字符串拼接,并且统一

@fahasikei fahasikei closed this Sep 5, 2023
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