使用webpack的Proxyquire不能编译

时间:2017-04-10 10:55:57

标签: reactjs typescript webpack proxyquire

我已经安装了proxyquire,我的ajax.test.tsx文件包含以下代码,只有2行

import * as proxyquire from 'proxyquire';
proxyquire.noCallThru();

我的webpack代码如下

module.exports = {
   entry: {
       "book.service": './src/book.service.ts',
       "ajax.test": './src/ajax.test.tsx'
   },
   output: {
       filename: "[name].js",
       path: __dirname + "/dist"
   },
   devtool: "source-map",
   watch: true,
   resolve: {
       extensions: [".tsx", ".tsx", ".js", ".json", ".ts", '.d.ts']
   },
   module: {
       rules: [
           { test: /\.tsx?$/, loader: "awesome-typescript-loader" },
           { enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
       ]
   },
   externals: {
       'react/lib/ExecutionEnvironment': true,
       'react/addons': true,
       'react/lib/ReactContext': 'window'
   }
};

我安装了proxyquire& @类型/ proxyquire。当我运行webpack命令时,它会引发以下错误

 WARNING in ./~/proxyquire/lib/proxyquire.js
 require.extensions is not supported by webpack. Use a loader instead.
 @ ./~/proxyquire/index.js 3:17-44
 @ ./src/ajax.test.tsx

 ERROR in ./~/proxyquire/lib/proxyquire.js
 Module not found: Error: Can't resolve 'module' in '***\node_modules\pr
 oxyquire\lib'

3 个答案:

答案 0 :(得分:3)

proxyquire与webpack不兼容 - https://github.com/thlorenz/proxyquire/issues/62

<强>问题

Proxyquire使用webpack不支持的require.extensions。

替代方案

答案 1 :(得分:1)

Proxyquire是为“node.js模块系统”而构建的,而不是webpack的。 你必须使用与“frontend”兼容的东西 - inject-loader或webpack-rewire。

如果您不尊重* -loaders的简单性,您可以尝试rewiremock。它具有类似proxyquire的语法,可以在node.js和webpack环境中工作。

答案 2 :(得分:0)

在您的webpack.config.js中更改以下行:

{ test: /\.tsx?$/, loader: "awesome-typescript-loader" }

为:

{ test: /\.tsx?$/, loader: "awesome-typescript-loader", exclude: /node_modules/ }

那应该解决你的问题。