Mei*_*hes 5 typescript webpack umd
我正在尝试使用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'] }
]
}
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "umd"
},
"exclude": [
"node_modules"
]
}
Run Code Online (Sandbox Code Playgroud)
的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"
}
}
Run Code Online (Sandbox Code Playgroud)
index.ts:
export function MyFunc(params) {
console.log("hello world");
}
Run Code Online (Sandbox Code Playgroud)
node -v = v6.3.0npm -v = 3.7.5奇怪的是,我的一位朋友说这对他们没有错误.虽然他在node 4.2.6.如果我将模块更改为commonjs,它可以完美地工作,没有警告或错误.
小智 15
我认为你需要"module": "commonjs"在tsconfig中,所以typescript编译将发出webpack可以理解的模块,你仍然可以从webpack获得umd输出
希望这可以帮助
如果您无法将modulefrom更改umd为commonjs因为您只是在使用它,并且您无权访问源,则可以将其用作解决umd-compat-loader方法。将以下内容添加rule到您的webpack.config.js:
module: {
rules: [
// other rules come here.
{
test: /node_modules[\\|/](name-of-the-umd-package)/,
use: { loader: 'umd-compat-loader' }
}
]
},
Run Code Online (Sandbox Code Playgroud)
我已在此线程中添加了更多详细信息。
| 归档时间: |
|
| 查看次数: |
3527 次 |
| 最近记录: |