Reputation: 12855
In our project, it is possible to run the application in either fake-backend mode or real-backend mode. The idea is that the developer can develop locally with functions that implement backend API but return mock data.
I'd like to be able to do something like:
webpack --config webpack-config-fake.js
Then in the code I would to do something like that:
var mockSuffix = webpackConfig.options.isFake ? "-fake" : "";
var backendApi = require('backend-api'+mockSuffix+'.js')
Questions:
Upvotes: 3
Views: 2634
Reputation: 36418
In your "fake" config, you could replace required modules via NormalModuleReplacementPlugin
. In the following example, require('backend-api')
yields backend-api-mock
.
plugins: [
new webpack.NormalModuleReplacementPlugin(/backend-api/, function(result) {
result.request = result.request.replace(/(backend-api)/, '$1-mock');
}),
],
You could also indicate that your mock folder takes precedence when resolving requests. See resolve.root
. In the following example, require('backend-api')
will first look into mockPath
and fall back to dependenciesPath
if no corresponding module was found.
resolve: {
root: [mockPath, dependenciesPath],
},
Upvotes: 14