Webpack UMD:关键依赖...无法静态提取 [英] Webpack UMD: Critical dependency... cannot be statically extracted
问题描述
我正在尝试使用 webpack 构建一个 umd 库;无论我做什么都会收到警告:
I'm trying to build a umd library with webpack; regardless of what I do get a warning:
D:/Code/Node/sample.io/source/index.ts 中的警告3:24 关键依赖:require 函数的使用方式无法静态提取依赖
WARNING in D:/Code/Node/sample.io/source/index.ts 3:24 Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
当我尝试 require('./index.js')
生成的 index.js 我得到:
when I try to require('./index.js')
the generated index.js I get:
错误:找不到模块."
为了完整起见,这里是我所有的文件:
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.0npm -v
= 3.7.5node -v
= v6.3.0npm -v
= 3.7.5
奇怪的是,我的一个朋友说这对他们来说没有错误.尽管他在 node 4.2.6
上.如果我将模块更改为 commonjs,它就可以正常工作而不会出现警告或错误.
oddly, a friend of mine says this works without error for them. Although he is on node 4.2.6
. If I change the module to commonjs it works perfectly fine without warnings or errors.
推荐答案
我认为你需要在 tsconfig 中使用 "module": "commonjs"
所以 typescript 编译会发出 webpack 可以理解的模块,你仍然会从 webpack 获得 umd 输出
I think you need "module": "commonjs"
in tsconfig so typescript compilation will emit modules that are understandable to webpack, you will still get umd output from webpack
希望能帮到你
这篇关于Webpack UMD:关键依赖...无法静态提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!