简体   繁体   English

由Babel / webpack导出的Tyepscript / es6模块以_esModule的形式具有默认属性

[英]Tyepscript/es6 module exported by babel/webpack as _esModule with default property

I've converted a javascript application into typescript, yet now that I've converted my code to typescript/es6-style exports, babel/webpack aren't exporting them correctly. 我已经将一个javascript应用程序转换为打字稿,但是现在我已将代码转换为打字稿/ es6样式的导出,而babel / webpack无法正确导出它们。

Since I now use the export default... syntax, babel turns it into an esModule object with the class actually attached to a default property. 由于我现在使用export default...语法,因此babel将其转换为esModule对象,该类实际上已附加到default属性。

Naturally, this object format is no longer valid for anyone actually using it. 自然,此对象格式对于实际使用它的任何人都不再有效。

I've tried adding the babel-plugin-add-module-exports plugin to the mix but it hasn't changed anything. 我尝试将babel-plugin-add-module-exports插件添加到混合中,但它没有做任何更改。 I'm wondering if typescript being in the mix has caused an issue. 我想知道打字稿中是否出现了问题。

.babelrc: .babelrc:

{
    "plugins": [
        "add-module-exports",
        "lodash"
    ]
}

tsconfig.json: tsconfig.json:

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs"
  }
}

webpack.config.js: webpack.config.js:

entry: {
    'myfile.ts'
},
output: {
    filename: 'myfile.js',
    library: 'MyApp',
    libraryTarget: 'umd'
},
resolve: {
    extensions: ['', '.ts', '.js']
},
module: {
    loaders: [{
        test: /\.ts$/, loader: 'babel!ts-loader'
    }]
}

myfile.ts just exports a class: myfile.ts只是导出一个类:

export default class MyApp {...

You could convert the bundle output to a global variable . 您可以将包输出转换为全局变量 Currently libraryTarget points to UMD which supports Commonjs and AMD 当前libraryTarget指向支持CommonjsAMD的 UMD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM