Proxyquire with webpack don't compile
Asked Answered
V

3

14

I have installed proxyquire and my ajax.test.tsx file contains the following code, just 2 lines

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

My webpack code is the following

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'
   }
};

I've installed proxyquire & @types/proxyquire. When I run the webpack command it throws up the following error

 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'
Vein answered 10/4, 2017 at 10:55 Comment(0)
L
3

proxyquire is not compatible with webpack - https://github.com/thlorenz/proxyquire/issues/62

Problem

Proxyquire uses require.extensions which is not supported by webpack.

Alternatives

Leeannaleeanne answered 4/5, 2017 at 11:16 Comment(0)
P
1

Proxyquire is built for "node.js module system", not the webpack's one. You have to use something compatible with "frontend" - inject-loader or webpack-rewire.

If you don't respect the simplicity of *-loaders - you can try rewiremock. It has proxyquire-like syntax and can work both in node.js and webpack environment.

Phospholipide answered 22/10, 2017 at 23:37 Comment(0)
P
0

In your webpack.config.js change the following line:

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

to:

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

That should fix your problem.

Podgy answered 26/4, 2017 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.