Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proxyquire with webpack don't compile

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'
like image 535
hendrixchord Avatar asked Apr 10 '17 10:04

hendrixchord


2 Answers

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

  • inject loader

  • babel-plugin-rewire

like image 148
Edward Avatar answered Oct 30 '22 10:10

Edward


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.

like image 45
Anton Korzunov Avatar answered Oct 30 '22 10:10

Anton Korzunov