【问题标题】:No exported types in UMD module using WebPack, Babel and ES2015使用 WebPack、Babel 和 ES2015 的 UMD 模块中没有导出类型
【发布时间】:2016-11-29 12:58:11
【问题描述】:

我在使用 UMD 模块库的 WebPack 和导出类方面遇到问题。问题是当我尝试将打包的包加载到浏览器中时,导出的对象是空的(没有与导出对象匹配的属性)。

我为此创建了简单的测试项目:

项目结构

~/Playground/webpack-exports-test tree -I node_modules
.
├── build
│   ├── test.html
│   ├── testlib.js
│   └── testlib.js.map
├── bundle.js
├── package.json
├── src
│   ├── a.js
│   └── b.js
└── webpack.config.js

webpack.config.js

let path = require('path');
let libraryName = 'testlib';
let bundleName = libraryName + '.js';

module.exports = {
    context: __dirname,
    entry: './bundle.js',
    output: {
        path: path.join(__dirname, 'build'),
        filename: bundleName,
        library: libraryName,
        libraryTarget: 'umd',
        umdNamedDefine: true
    },
    devtool: 'source-map',
    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015']
                }
            }
        ]
    }
};

package.json

{
    "name": "webpack-exports-test",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "devDependencies": {
        "babel-core": "^6.18.2",
        "babel-loader": "^6.2.8",
        "babel-preset-es2015": "^6.18.0",
        "webpack": "^1.13.3"
    }

}

src/a.js

export class A {
}

src/b.js

import {A} from './a.js'

export class B extends A {
}

bundle.js

import {A} from './src/a.js'
import {B} from './src/b.js'

当我尝试调试 WebPack 生成的代码时,似乎已正确创建类对象,并传递给 exports,但没有任何内容返回给全局对象。

有什么帮助吗?

【问题讨论】:

    标签: webpack babeljs


    【解决方案1】:

    我找到了答案。问题是条目bundle.js 没有导出任何符号。

    这使它起作用

    import {A} from './src/a.js'
    import {B} from './src/b.js'
    export {A as A}
    export {B as B}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-24
      • 2016-04-01
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 2016-02-04
      相关资源
      最近更新 更多