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

引用微信官方拓展组件webpack报错 #41

Open
ludejun opened this issue Mar 28, 2021 · 1 comment
Open

引用微信官方拓展组件webpack报错 #41

ludejun opened this issue Mar 28, 2021 · 1 comment

Comments

@ludejun
Copy link

ludejun commented Mar 28, 2021

wxapp-webpack-plugin 使用最新的0.19.0
在页面中使用官方拓展组件:yarn add @miniprogram-component-plus/tabs
.json:
{ "usingComponents": { "user": "../../components/user/user", "mp-tabs": "@miniprogram-component-plus/tabs" }, "navigationStyle": "custom", "backgroundColor": "#fff" }

打包会报错:TypeError: Cannot read property 'replace' of undefined
我看有几个类似问题,在wxapp-webpack-plugin库中也有

@hydra1983
Copy link

hydra1983 commented May 6, 2021

源码里有这样的逻辑

			if (relativeComponent.indexOf('plugin://') === 0) continue;

所以可以采用如下临时解决方案
1 修改 .json

{
  "usingComponents": {
    "mp-searchbar": "plugin://weui-miniprogram/searchbar/searchbar"
  }
}

2 自定义 json loader

// wx-json-loader.js

import * as _ from 'lodash';

const pluginPrefix = 'plugin://';
export default source => {
  const obj = JSON.parse(source);
  if (!_.isNil(obj.usingComponents) && _.isPlainObject(obj.usingComponents)) {
    _.forEach(obj.usingComponents, value => {
      if (value.indexOf(pluginPrefix) === 0) {
        source = source.replace(value, value.substr(pluginPrefix.length));
      }
    });
  }
  return source;
};

3 配置 webpack,主要代码如下

const ruleJson = {
  test: /\.(json)$/,
  include: /src/,
  use: [relativeFileLoader(), 'wx-json-loader'],
};

return {
  ...
  resolveLoader: {
    alias: {
      'wx-json-loader': resolve('webpack/wx-json-loader.js')
    }
  },
  module: {
   rules: [
     ...,
     ruleJson
  ]
 }
 ...
}

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