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

【BUG】 微信小程序插件开发,comp 引入路径错误以及公共样式未引入 #16307

Closed
hangaoke1 opened this issue Aug 14, 2024 · 2 comments · Fixed by #16320 or #16945
Closed
Labels
F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x
Milestone

Comments

@hangaoke1
Copy link
Contributor

hangaoke1 commented Aug 14, 2024

相关平台

微信小程序

复现仓库

https://github.com/hangaoke1/taro-plugin-test.git
小程序基础库: 4.0.4
使用框架: React

复现步骤

拉取项目

npm i
npm run dev

期望结果

comp 引入路径正确
样式正常加载

实际结果

comp引入路径错误
样式未正常加载

环境信息

👽 Taro v4.0.4


  Taro CLI 4.0.4 environment info:
    System:
      OS: macOS 13.3.1
      Shell: 5.9 - /bin/zsh
    Binaries:
      Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
      Yarn: 1.22.5 - ~/.yarn/bin/yarn
      npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
    npmPackages:
      @tarojs/cli: 4.0.4 => 4.0.4 
      @tarojs/components: 4.0.4 => 4.0.4 
      @tarojs/helper: 4.0.4 => 4.0.4 
      @tarojs/plugin-framework-react: 4.0.4 => 4.0.4 
      @tarojs/plugin-platform-alipay: 4.0.4 => 4.0.4 
      @tarojs/plugin-platform-h5: 4.0.4 => 4.0.4 
      @tarojs/plugin-platform-jd: 4.0.4 => 4.0.4 
      @tarojs/plugin-platform-qq: 4.0.4 => 4.0.4 
      @tarojs/plugin-platform-swan: 4.0.4 => 4.0.4 
      @tarojs/plugin-platform-tt: 4.0.4 => 4.0.4 
      @tarojs/plugin-platform-weapp: 4.0.4 => 4.0.4 
      @tarojs/react: 4.0.4 => 4.0.4 
      @tarojs/runtime: 4.0.4 => 4.0.4 
      @tarojs/shared: 4.0.4 => 4.0.4 
      @tarojs/taro: 4.0.4 => 4.0.4 
      @tarojs/taro-loader: 4.0.4 => 4.0.4 
      @tarojs/webpack5-runner: 4.0.4 => 4.0.4 
      babel-preset-taro: 4.0.4 => 4.0.4 
      eslint-config-taro: 4.0.4 => 4.0.4 
      react: ^18.0.0 => 18.3.1 
@taro-bot2 taro-bot2 bot added F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x labels Aug 14, 2024
@hangaoke1
Copy link
Contributor Author

image
comp 组件引用层级错误

image
list、chat 引用了同一份样式文件,输出 app.wxss 但是小程序插件未引入

This was referenced Aug 17, 2024
@hangaoke1
Copy link
Contributor Author

hangaoke1 commented Nov 12, 2024

虽然 app.wxss 中引入了 common.wxss,但是小程序插件不同于小程序应用,是不会加载 app.wxss 样式文件的,所以导致插件编译后公共样式丢失
image

可以临时写一个 webpack 插件判断如果是插件编译模式则使用插件,并在每个页面及导出组件的样式文件中导入缺失的 common.wxss

const { Compilation } = require('webpack');
const Regexp = /(?<=(components\/public|pages))\/.+\.wxss/g;

class WxssImportPlugin {
  constructor(options) {
    this.options = options;
  }
  apply(compiler) {
    compiler.hooks.thisCompilation.tap('WxssImportPlugin', (compilation) => {
      compilation.hooks.processAssets.tapAsync(
        {
          name: 'WxssImportPlugin',
          stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
        },
        (assets, callback) => {
          for (const [name, source] of Object.entries(assets)) {
            if (Regexp.test(name)) {
              const times = name.split('/').length - 1;
              const firstLine = `@import '${`${Array(times).fill('../').join('')}common.wxss`}';\r\n`;
              assets[name] = new compiler.webpack.sources.ConcatSource(firstLine, source);
            }
          }
          callback();
        },
      );
    });
  }
}

module.exports = WxssImportPlugin;
import WxssImportPlugin from './WxssImportPlugin';


webpackChain(chain, webpack) {
      const isBuildPlugin = chain.output.get('path').includes('miniprogram/plugin');

      // 临时解决方案,解决 Taro 插件未引入公共样式问题(插件不会自动加载 app.wxss)
      if (isBuildPlugin) {
        chain.plugin('WxssImportPlugin').use(WxssImportPlugin, []);
      }
    },

@tutuxxx tutuxxx added this to the 4.0.8 milestone Nov 15, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in Mini Program Nov 15, 2024
tutuxxx added a commit that referenced this issue Nov 29, 2024
fix #16307

Co-authored-by: 韩高钶 <hangaoke@corp.netease.com>
Co-authored-by: Shijie Yu <yushijiechn@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x
Projects
Archived in project
2 participants