我正在尝试使用webpack构建一个umd库;无论我做什么都得到警告:
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
当我尝试要求(‘./ index.js’)生成的index.js时,我得到:
Error: Cannot find module “.”
为了完整性,这里是我的所有文件:
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
> npm -v = 3.7.5
奇怪的是,我的一位朋友说这对他们没有错误.虽然他在节点4.2.6上.如果我将模块更改为commonjs,它可以完美地工作,没有警告或错误.
解决方法
我认为你需要tsconfig中的“module”:“commonjs”,所以typescript编译会发出webpack可以理解的模块,你仍然可以从webpack获得umd输出
希望这可以帮助