我正在尝试使用webpack构建一个umd库;无论我做什么都会得到一个警告:
警告:D:/Code/Node/sample.io/source/index.ts 3:24 严重依赖项:require函数的使用方式不支持静态提取依赖项
当我尝试require('./index.js')
生成的index.js时,会出现以下错误:
错误:找不到模块“。”
为了完整起见,这里是所有我的文件:
webpack.config.js:
module.exports = {
entry: {
index: __dirname + '/index'
},
output: {
filename: 'index.js',
library: 'mylib',
libraryTarget: 'umd',
umdNamedDefine: true
},
resolve: {
root: __dirname,
extensions: ['', '.ts', '.js'],
},
module: {
loaders: [
{ test: /\.ts$/, loaders: ['awesome-typescript-loader'] }
]
}
}
tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "umd"
},
"exclude": [
"node_modules"
]
}
package.json:
{
"name": "foo",
"version": "0.1.0",
"devDependencies": {
"awesome-typescript-loader": "^2.0.2",
"typescript": "^2.0.0",
"webpack": "^2.1.0-beta.17"
}
}
index.ts:
export function MyFunc(params) {
console.log("hello world");
}
node -v
= v6.3.0(Node.js版本号)npm -v
= 3.7.5(npm包管理器版本号)
奇怪的是,我的一个朋友说这对他们来说能够正常工作而没有错误。尽管他使用的是node 4.2.6
。如果我将模块更改为commonjs,它完全可以正常运行而没有警告或错误。
webpack.config.js
文件的output
块中添加libraryTarget: 'umd'
。在 webpack 文档 中有描述。 - knee-cola