【发布时间】: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,但没有任何内容返回给全局对象。
有什么帮助吗?
【问题讨论】: