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

defineConstants 在H5模式下失效 #13286

Closed
SuperSaiyr opened this issue Feb 13, 2023 · 1 comment · Fixed by #13292 or #13361
Closed

defineConstants 在H5模式下失效 #13286

SuperSaiyr opened this issue Feb 13, 2023 · 1 comment · Fixed by #13292 or #13361
Labels
F-react Framework - React T-h5 Target - 编译到 H5 V-3 Version - 3.x
Milestone

Comments

@SuperSaiyr
Copy link

相关平台

H5

浏览器版本: chrome 版本 110.0.5481.77(正式版本) (arm64)
使用框架: React

复现步骤

直接初始化项目:
在config中添加
{
...
defineConstants: {
TEST: '"TEST"',
},
}

在项目中使用该变量如下

const Index = () => {
const [text] = useState(TEST);

return {text};
};

运行项目
npm run build:h5 -- --watch

期望结果

和小程序编译的结果显示一样, 能够正常显示

实际结果

ReferenceError
TEST is not defined

环境信息

  Taro CLI 3.6.1 environment info:
    System:
      OS: macOS 13.0.1
      Shell: 5.8.1 - /bin/zsh
    Binaries:
      Node: 14.20.0 - ~/.nvm/versions/node/v14.20.0/bin/node
      Yarn: 1.22.17 - /opt/homebrew/bin/yarn
      npm: 6.14.17 - ~/.nvm/versions/node/v14.20.0/bin/npm
    npmPackages:
      @tarojs/cli: 3.6.1 => 3.6.1 
      @tarojs/components: 3.6.1 => 3.6.1 
      @tarojs/helper: 3.6.1 => 3.6.1 
      @tarojs/plugin-framework-react: 3.6.1 => 3.6.1 
      @tarojs/plugin-platform-alipay: 3.6.1 => 3.6.1 
      @tarojs/plugin-platform-h5: 3.6.1 => 3.6.1 
      @tarojs/plugin-platform-jd: 3.6.1 => 3.6.1 
      @tarojs/plugin-platform-qq: 3.6.1 => 3.6.1 
      @tarojs/plugin-platform-swan: 3.6.1 => 3.6.1 
      @tarojs/plugin-platform-tt: 3.6.1 => 3.6.1 
      @tarojs/plugin-platform-weapp: 3.6.1 => 3.6.1 
      @tarojs/react: 3.6.1 => 3.6.1 
      @tarojs/runtime: 3.6.1 => 3.6.1 
      @tarojs/shared: 3.6.1 => 3.6.1 
      @tarojs/taro: 3.6.1 => 3.6.1 
      @tarojs/webpack5-runner: 3.6.1 => 3.6.1 
      babel-preset-taro: 3.6.1 => 3.6.1 
      eslint-config-taro: 3.6.1 => 3.6.1 
      react: ^18.0.0 => 18.2.0 
    npmGlobalPackages:
      typescript: 4.9.4
@taro-bot2 taro-bot2 bot added F-react Framework - React T-h5 Target - 编译到 H5 V-3 Version - 3.x labels Feb 13, 2023
@SuperSaiyr SuperSaiyr changed the title defineConstants 失效 defineConstants 在H5模式下失效 Feb 13, 2023
@SuperSaiyr
Copy link
Author

原因是 @tarojs/plugin-platform-h5@3.6.1 中, 本来应该是merge操作, 变成了覆盖操作, 导致自定义的defineConstants都失效了

   // node_modules/@tarojs/plugin-platform-h5/dist/index.js
   // line 124
    ctx.modifyRunnerOpts(({ opts }) => {
        opts.defineConstants = {
            USE_HTML_COMPONENTS: JSON.stringify(!!opts.useHtmlComponents),
        };
        // TODO 为 postcss-html-transform 更新组件转换列表
    });

@ZakaryCode ZakaryCode added this to the 3.6.2 milestone Feb 14, 2023
heiazu added a commit that referenced this issue Feb 24, 2023
* fix(h5): custom define-constants fix #13286
---------

Co-authored-by: AzuFF <47267778+heiazu@users.noreply.github.com>
Co-authored-by: zhuminghui2 <zhuminghui2@jd.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-h5 Target - 编译到 H5 V-3 Version - 3.x
Projects
Archived in project
2 participants