Excluding folder while building for production not working

250 Views Asked by At

I have a folder with some mock data in "src/api/mock" folder which contains some ts and JSON files. I want Webpack to exclude them when it builds for production. I tried using the below rule but its not working. Any suggestions?

  module: {
    rules: [
      {
        test: /\.(ts|json)$/i,
        exclude: ['/src/api/mock/'],
      },
    ],
  },
2

There are 2 best solutions below

2
Mallikarjun M G On

Try this if it works

const path = require('path');

module.exports = {
  ...others,
  module: {
    rules: [
      {
        test: /\.(ts|json)$/,
        exclude: path.resolve(__dirname, 'src/api/mock'),
        use: {
          loader: 'babel-loader',
        },
      },
    ],
  },
};
0
Yilmaz On

since you are using typescript you should use tsconfig.json. when you init this file you should have this already

 "exclude": ["node_modules"]

Just add the correct path for this '/src/api/mock/' to the array. I think ts-loader follows this file's configuration