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

Taro构建小程序报错:“enum is a reserved word” #7404

Open
walter-wang314 opened this issue Aug 21, 2020 · 11 comments
Open

Taro构建小程序报错:“enum is a reserved word” #7404

walter-wang314 opened this issue Aug 21, 2020 · 11 comments
Labels
F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x

Comments

@walter-wang314
Copy link

walter-wang314 commented Aug 21, 2020

相关平台

微信小程序

小程序基础库: 2.11.2
使用框架: React

复现步骤

  1. 使用TS新建一个工具库,test-xxx,在库中使用TS的枚举 enum 来声明变量;
  2. 复制一个同样的库,taro-xxx;
  3. 使用taro init 新建一个项目,在package.json中通过git https引入这两个库;
  4. 在项目中引入taro-xxx,执行npm run build,构建会通过;
  5. 在项目中引入test-xxx,执行npm run build,构建失败;出现 Typescript enum编译错误“enum is a reserved word” 的报错;

期望结果

在项目中引入test-xxx,执行npm run build,构建成功。

实际结果

在项目中引入test-xxx,执行npm run build,构建失败;出现 Typescript enum编译错误“enum is a reserved word” 的报错;

module parse failed: The keyword 'enum' is reserved (4:6)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|  * 检测数据默认枚举
|  */
> enum TrackingTypes {
|   /**
|    * 小程序启动

环境信息

Taro CLI 3.0.7 environment info:
    System:
      OS: macOS 10.15.6
      Shell: 5.7.1 - /bin/zsh
    Binaries:
      Node: 12.18.3 - ~/.nvm/versions/node/v12.18.3/bin/node
      npm: 6.14.6 - ~/.nvm/versions/node/v12.18.3/bin/npm
    npmPackages:
      @tarojs/components: 3.0.7 => 3.0.7
      @tarojs/mini-runner: 3.0.7 => 3.0.7
      @tarojs/react: 3.0.7 => 3.0.7
      @tarojs/runtime: 3.0.7 => 3.0.7
      @tarojs/taro: 3.0.7 => 3.0.7
      babel-preset-taro: 3.0.7 => 3.0.7
      eslint-config-taro: 3.0.7 => 3.0.7
      react: ^16.10.0 => 16.13.1
@taro-bot2 taro-bot2 bot added F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x labels Aug 21, 2020
@walter-wang314 walter-wang314 changed the title Taro构建小程序报错:Typescript enum编译错误“enum is a reserved word” Taro构建小程序报错:“enum is a reserved word” Aug 21, 2020
@rottenpen
Copy link
Contributor

提供一下代码吧... 你这样描述没法给你debug的

@walter-wang314
Copy link
Author

walter-wang314 commented Aug 24, 2020 via email

@cncolder
Copy link
Contributor

cncolder commented Aug 24, 2020

@wzc314159 这不是 taro 的问题, 这是语法错误. enum 是 Javascript 保留字, 不能用作变量名.

大部分 Javascript 关键字和保留字会优先触发 SyntaxError: Unexpected token. enum 不是语句, 所以你看到的是 SyntaxError: Unexpected reserved word.

你可以在浏览器控制台里输入 var enum = 1 试一下.

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Reserved_words

@walter-wang314
Copy link
Author

walter-wang314 commented Aug 25, 2020 via email

@cncolder
Copy link
Contributor

@wzc314159 需要一个明确的例子, 你的 enum 出现在什么位置.
babel 转换 .ts 文件, var enum = 1 会原封不动的输出, 可这仍然是语法错误啊!

@walter-wang314
Copy link
Author

walter-wang314 commented Aug 31, 2020 via email

@zlyyyy
Copy link

zlyyyy commented Sep 21, 2020

我也遇到了类似的问题,H5 Module parse failed: The keyword 'interface' is reserved 老哥你是咋解决的

@walter-wang314
Copy link
Author

walter-wang314 commented Sep 21, 2020 via email

@zlyyyy
Copy link

zlyyyy commented Sep 21, 2020

你试一下把包名改成 taro- 看行不行 2020年9月21日 上午10:56,Layyy <[email protected]mailto:[email protected]> 写道: 我也遇到了类似的问题,H5 Module parse failed: The keyword 'interface' is reserved 老哥你是咋解决的 — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub<#7404 (comment)>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AE6QE3WHNZBCZKSJVOW7WLLSG26FJANCNFSM4QG7CDRA.

不行。。

@walter-wang314
Copy link
Author

walter-wang314 commented Sep 21, 2020 via email

@pansinm
Copy link

pansinm commented May 19, 2022

恰好也遇到这个问题,看了下源码。taro 项目的 babel-loader 配置中,默认 exclude 规则是 /node_modules/.test(filename) && !/taro/.test(filename)。路径包含 taro 的文件都会进行编译,node_modules下且不包含taro字符串的文件不编译。所以可行的方法有:

  1. 修改配置,通过 webpackChain 修改 exclude
  2. 包名包含 taro 字符串
  3. 项目放到 包含 taro 的路径下(所有代码都会编译,不推荐)

PS: 为啥要排除包含taro的文件呢?

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
None yet
Development

No branches or pull requests

5 participants