NervJS / taro

开放式跨端跨框架解决方案,支持使用 React/Vue/Nerv 等框架来开发微信/京东/百度/支付宝/字节跳动/ QQ 小程序/H5/React Native 等应用。 https://taro.zone/
https://docs.taro.zone/
Other
35.66k stars 4.8k forks source link

defineConstants 功能无效 #13924

Closed zhgh closed 1 year ago

zhgh commented 1 year ago

相关平台

微信小程序

复现仓库

git@github.com:zhgh/taro-defineConstants.git 小程序基础库: 2 使用框架: React

复现步骤

npx @tarojs/cli@3.6.7 init tApp 直接使用最新模板,然后增加自定义变量,增加全局ts声明。编译失败

期望结果

可以正常读取全局变量

实际结果

ReferenceError: APP_A is not defined at Object. (E:\testwork\tapp367\src\app.config.ts:26:13) at Module._compile (internal/modules/cjs/loader.js:1085:14) at requireFromString (E:\testwork\tapp367\node_modules\require-from-string\index.js:28:4) at requireWithEsbuild (E:\testwork\tapp367\node_modules\@tarojs\helper\src\esbuild\index.ts:50:27) at readConfig (E:\testwork\tapp367\node_modules\@tarojs\helper\src\utils.ts:604:34) at MiniPrebundle.getEntries (E:\testwork\tapp367\node_modules\@tarojs\webpack5-prebundle\src\prebundle\index.ts:137:35) at MiniPrebundle. (E:\testwork\tapp367\node_modules\@tarojs\webpack5-prebundle\src\mini.ts:224:36) at Generator.next () at E:\testwork\tapp367\node_modules\@tarojs\webpack5-prebundle\dist\mini.js:8:71 at new Promise () 依赖预编译失败,已经为您跳过预编译步骤,但是编译速度可能会受到影响。 (node:22596) UnhandledPromiseRejectionWarning: ReferenceError: APP_A is not defined at Object. (E:\testwork\tapp367\src\app.config.ts:26:13) at Module._compile (internal/modules/cjs/loader.js:1085:14) at requireFromString (E:\testwork\tapp367\node_modules\require-from-string\index.js:28:4) at requireWithEsbuild (E:\testwork\tapp367\node_modules\@tarojs\helper\src\esbuild\index.ts:50:27) at readConfig (E:\testwork\tapp367\node_modules\@tarojs\helper\src\utils.ts:604:34) at MiniSplitChunksPlugin.getSubpackageConfig (E:\testwork\tapp367\node_modules\@tarojs\webpack5-runner\src\plugins\MiniSplitChunksPlugin.ts:593:44) at MiniSplitChunksPlugin.apply (E:\testwork\tapp367\node_modules\@tarojs\webpack5-runner\src\plugins\MiniSplitChunksPlugin.ts:330:29) at createCompiler (E:\testwork\tapp367\node_modules\webpack\lib\webpack.js:73:12) at create (E:\testwork\tapp367\node_modules\webpack\lib\webpack.js:134:16) at webpack (E:\testwork\tapp367\node_modules\webpack\lib\webpack.js:158:32) (Use node --trace-warnings ... to show where the warning was created) (node:22596) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3) (node:22596) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

环境信息

👽 Taro v3.6.7

  Taro CLI 3.6.7 environment info:
    System:
      OS: Windows 10 10.0.19045
    Binaries:
      Node: 14.21.2 - C:\Program Files\nodejs\node.EXE
      Yarn: 1.22.19 - C:\Program Files\nodejs\yarn.CMD
      npm: 6.14.17 - C:\Program Files\nodejs\npm.CMD  
    npmPackages:
      @tarojs/cli: 3.6.7 => 3.6.7
      @tarojs/components: 3.6.7 => 3.6.7
      @tarojs/helper: 3.6.7 => 3.6.7
      @tarojs/plugin-framework-react: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-alipay: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-h5: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-jd: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-qq: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-swan: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-tt: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-weapp: 3.6.7 => 3.6.7
      @tarojs/react: 3.6.7 => 3.6.7
      @tarojs/runtime: 3.6.7 => 3.6.7
      @tarojs/shared: 3.6.7 => 3.6.7
      @tarojs/taro: 3.6.7 => 3.6.7
      @tarojs/webpack5-runner: 3.6.7 => 3.6.7
      babel-preset-taro: 3.6.7 => 3.6.7
      eslint-config-taro: 3.6.7 => 3.6.7
      react: ^18.0.0 => 18.2.0
      taro-ui: ^3.1.0-beta.2 => 3.1.0-beta.5
xuanzebin commented 1 year ago

看了一下,发现你在 app.config.ts 文件里面使用 defineConstants,这种用法是行不通的,原因是因为 config 文件目前是由 swc 来读取的,没有经过 webpack 编译处理,所以 defineConstants 配置没有办法在 config 文件里面生效。

Max0897 commented 1 year ago

想请问一下在调用defineConstants 中定义的常量时,编辑器报错找不到名称“XXX“,这个应该如何解决 image