ali-sdk / ali-oss

Aliyun OSS(Object Storage Service) JavaScript SDK for the Browser and Node.js
https://www.alibabacloud.com/help/doc-detail/52834.htm
MIT License
1.95k stars 576 forks source link

使用webpack+TS打包此库有问题 #911

Open ktangels opened 3 years ago

ktangels commented 3 years ago

版本号: webpack 4.42.0 ts-loader 6.2.1 ali-oss 6.12.0

详细描述: 在配置wepack的打包模式为node之后,用import oss from 'ali-oss' 引入库之后,在打包后,运行时会出现如下错误 Error: Module parse failed: Unexpected token (1:36) 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

export const checkBucketName = (name: string, createBucket = false): void => { | const bucketRegex = createBucket ? /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/ : /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9_]$/; | if (!bucketRegex.test(name)) { at Object../node_modules/ali-oss/lib/common/utils/checkBucketName.ts (/mnt/d/intop/dist/dev/server/main.js:9563:7) at __webpack_require__ (/mnt/d/intop/dist/dev/server/main.js:20:30)

从错误信息来看,是因为wepack尝试把一个ts文件打入js文件,所以导致运行出错 我与其他库做了对比之后,发现其他库中是正常打入js的

临时解决方案: 如果我们去掉wepack配置中 rules: [ { test: /.tsx?$/, use: 'ts-loader', exclude: /node_modules/ } 的exclude一行,那么可以做到正常运行,但从wepack推荐流程来说,库中的ts文件应该是提前编译过的(事实alioss当中也的确存在编译好的js),去掉这一行的意思是,把库里的ts文件当成我们自己的代码重新打包一次。 虽然这样能解决问题,但我还是希望他能像其他库一样,正确找到需要打包的js文件

beajer commented 3 years ago

检查webpack中的resolve.extensions顺序,确保.js在.ts前