taro项目里一般是es的语法,使用import,export,但是如果接入了第三方sdk时,可能会需要把第三方的sdk下载到项目里,而第三方的sdk有可能是commonjs模块的
此时会有es模块和commonjs模块混用的情况,需要使用@babel/plugin-transform-modules-commonjs
,进行转换。
插件是这么用
在babel.config.js里
module.exports = {
plugins: [
[
'@babel/plugin-transform-modules-commonjs',
{
allowTopLevelThis: true,
},
],
],
}
问题
这样子貌似会把node_modules里的代码也给转换了,转成commonjs的话,没法treeshaking了
所以需要对特定的代码进行转换
@babel/plugin-transform-modules-commonjs的官方文档里没有说明如何只对特定文件使用某个插件
解决
使用overrides字段对特定包使用
在babel.config.js里
module.exports = {
overrides: [
{
test: './src/某第三方混用的sdk',
plugins: ['@babel/plugin-transform-modules-commonjs'],
},
],
}