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

2.0 无法打包通过 npm 引入的 wpy 文件 #2554

Open
fudiwei opened this issue Mar 31, 2020 · 9 comments
Open

2.0 无法打包通过 npm 引入的 wpy 文件 #2554

fudiwei opened this issue Mar 31, 2020 · 9 comments
Assignees

Comments

@fudiwei
Copy link
Contributor

fudiwei commented Mar 31, 2020

1.7.X 版本是可以引用并正常打包通过 npm 引入的 wpy 组件的。

2.0 可以通过 npm 引入原生组件、但引入 wpy 组件却不能正常打包(具体现象是不能在输出目录中创建 $vendor 目录)。

示例代码如下:

/* 
  @wepy/cli: 2.0.0-alpha.25
  @wepy/core: 2.0.0-alpha.14
*/
<config>
{
    usingComponents: {
        // 原生组件,即导出的是 js+json+wxml+wxss 可以正常打包;文档中给出的示例里的 miniprogram-slide-view 就是原生组件
        'com1': 'module:my-weapp-component',
        // wepy2 组件,即导出的是 wpy 文件,无法打包
        'com2': 'module:my-wepy2-component'
    }
}
</config>

目前发现只需要改动 @wepy/cli/core/plugins/parser/wpy.js 中的第 25 行:

let context = {
    file,
    type,
    npm: type === 'module'
    component: true
};

修改为:

let context = {
    file,
    type,
    npm: type === 'module' || comp.npm // 此处
    component: true
};

即可正常打包 wpy 组件,还未发现有其他副作用。

麻烦确认是否是 Bug。

@fudiwei
Copy link
Contributor Author

fudiwei commented Jun 19, 2020

@Gcaufy @deepfunc 我尝试定位了一下错误原因。

对于 .wpy 文件,其编译的相关代码:

exports = module.exports = function() {
this.register('wepy-parser-wpy', function(comp) {
let sfc, flow;
let file = comp.path;
let type = comp.type;
let context = {
file,
type,
npm: type === 'module',
component: true
};

发现其判断是否是 npm 引入的方式是判断其 type 是否值为 'module'

而编译组件时触发上面这个钩子的相关代码:

parsedComponents.forEach(com => {
if (com.type === 'wepy' && !components.includes(com.path)) {
// wepy 组件
tasks.push(this.hookUnique('wepy-parser-wpy', com));
components.push(com.path);
} else if (com.type === 'weapp' && !originalComponents.includes(com.path)) {
// 原生组件
tasks.push(this.hookUnique('wepy-parser-component', com));
originalComponents.push(com.path);
}
});

传入的 type 要么为 'wepy',要么为 'weapp',永远不可能是 'module',所以明明是 npm 引入的文件并不会被识别为 npm 引入。看上去 'module' 值是为 npm 引入的 .js 文件准备的,而不是为 .wpy 准备的。

正确的做法应该是直接读取 com.npm 的值:

let context = {
    file,
    type,
    npm: !!comp.npm // 此处
    component: true
};

@Gcaufy
Copy link
Collaborator

Gcaufy commented Jun 21, 2020

此处的属性设计在一开始没有做好,导致会出现修复了这种场景却导致另外一种场景出现问题。

看看这里是否可以用 test cases 来保证这个场景的正确性。

@fudiwei
Copy link
Contributor Author

fudiwei commented Jun 22, 2020

@Gcaufy 这些属性都分别代表什么意思?我现在看起来其实是比较乱的,也没有注释,只能根据上下文和变量名尽量去猜。

另外目前都有哪些场景?我应该看源码的哪个地方可以确定他们应该有的值?

测试用例我可以来写。

@lvan-zhang
Copy link

顶一个,遇到同样的问题,希望尽快解决

@lxf-soul
Copy link

lxf-soul commented Dec 7, 2021

请问现在wepy2开发插件可以成功吗

@lxf-soul
Copy link

lxf-soul commented Dec 7, 2021

顶一个,遇到同样的问题,希望尽快解决

请问你的问题解决了码

@fudiwei
Copy link
Contributor Author

fudiwei commented Dec 7, 2021

@lxf-soul 我弃坑了,改用 uni-app 了,现在还活着的跨端小程序框架也只剩这一个了

@lvan-zhang
Copy link

我用的原生写的包,然后再wpy里引入

@lxf-soul
Copy link

lxf-soul commented Dec 7, 2021

@lxf-soul 我弃坑了,改用 uni-app 了,现在还活着的跨端小程序框架也只剩这一个了

看我也要跑了,这个框架坑太大了

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

5 participants