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

通过npm引入es6格式的module后编译的代码在微信小程序执行出错 #4182

Closed
icewind7030 opened this issue Aug 15, 2019 · 3 comments

Comments

@icewind7030
Copy link

icewind7030 commented Aug 15, 2019

问题描述
通过npm引入es6 模块写法的module,即模块中使用的是es6 的export导出模块时,export语法没有被正确转换为微信小程序能够识别的commonjs语法,导致小程序编译出错:

Uncaught SyntaxError: Unexpected token export

复现步骤

  1. 通过npm安装nw-toolkits
    npm i nw-toolkits --save
  2. 在page中引入模块
    import {format as formatTime} from 'nw-toolkits/es/time-format'
  3. 编译为微信小程序
  4. 小程序模拟器报错

被引入的模块的语法示例:

export var test = function test() {
  return 'test'
};

期望行为
将module发布为使用es6模块语法的npm包在webpack环境中也算比较常见了,希望taro在处理npm模块时也能增加这些处理。

报错信息

Uncaught SyntaxError: Unexpected token export

系统信息

System:
      OS: macOS 10.14.6
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 8.16.0 - ~/.nvm/versions/node/v8.16.0/bin/node
      Yarn: 1.16.0 - ~/.yarn/bin/yarn
      npm: 6.4.1 - ~/.nvm/versions/node/v8.16.0/bin/npm
    npmPackages:
      @tarojs/async-await: ^1.3.13 => 1.3.13 
      @tarojs/components: 1.3.12 => 1.3.12 
      @tarojs/plugin-babel: 1.3.12 => 1.3.12 
      @tarojs/plugin-csso: 1.3.12 => 1.3.12 
      @tarojs/plugin-sass: 1.3.12 => 1.3.12 
      @tarojs/plugin-uglifyjs: 1.3.12 => 1.3.12 
      @tarojs/router: 1.3.12 => 1.3.12 
      @tarojs/taro: 1.3.12 => 1.3.12 
      @tarojs/taro-alipay: 1.3.12 => 1.3.12 
      @tarojs/taro-h5: 1.3.12 => 1.3.12 
      @tarojs/taro-qq: 1.3.12 => 1.3.12 
      @tarojs/taro-quickapp: 1.3.12 => 1.3.12 
      @tarojs/taro-swan: 1.3.12 => 1.3.12 
      @tarojs/taro-tt: 1.3.12 => 1.3.12 
      @tarojs/taro-weapp: 1.3.12 => 1.3.12 
      @tarojs/webpack-runner: 1.3.12 => 1.3.12 
      eslint-config-taro: 1.3.12 => 1.3.12 
      eslint-plugin-taro: ^1.3.12 => 1.3.12 
      nerv-devtools: ^1.4.0 => 1.4.3 
      nervjs: ^1.4.0 => 1.4.3 
      stylelint-config-taro-rn: 1.3.12 => 1.3.12 
      stylelint-taro-rn: 1.3.12 => 1.3.12 

补充信息
我也看了下源码,resolve_npm_files.ts在拷贝npm文件时,也是有使用babel对ast进行一些处理,但是却没有处理export等情况,试了下想在里面直接修改有点不好处理。

@taro-bot
Copy link

taro-bot bot commented Aug 15, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@luckyadam
Copy link
Member

配置一下 config/index.jsweapp.compile.include ,该配置项是个数组,填包名,试一试

@icewind7030
Copy link
Author

@luckyadam 试了下,确实可以了,之前没有看出compileInclude对应的配置项,感谢。

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