Ash
Ash

Reputation: 896

Webpack conditionally include directory

I would like to be able to mock my endpoints, and am using a fixtures directory, which is currently an asset, so when I intercept calls to certain endpoints, I will instead send a GET to fixtures/user1.json for example. I'd like to be able to just import this json file instead, but don't want any extra files included in my bundle in production mode. Is there a way to conditionally include the fixtures into my bundle via webpack?

Any advice much appreciated

Upvotes: 0

Views: 384

Answers (1)

Devin Olsen
Devin Olsen

Reputation: 862

If you are willing to update your config file you can condition builds like this:

const config = {
  entry: {
    assets: ...
  },
  output: {
    ...
  },
  module: {
    rules: []
  },
  plugins: [ ... ]
};


module.exports = (env, argv) => {
  if (argv.mode === 'production') {
    // add or change config object for prod builds here
  }

  if (argv.mode === 'development') {
    // add or change config object for dev builds here
  }

  return config;
};

Next, have a look at: Webpack Exclude a specific file on how to use Webpack's rules.exclude option. You should be able to then config.module.rules.push({...exclude stuff...}) while in production's condition to finally exclude said file(s).

Hope this help!

Upvotes: 1

Related Questions