timarney / react-app-rewired

Override create-react-app webpack configs without ejecting
MIT License
9.77k stars 425 forks source link

Need help: remove random numbers from media files (pdf) #590

Closed evgeniya-osmakova closed 2 years ago

evgeniya-osmakova commented 2 years ago

I need to remove generated random numbers from all pdf files names in my project. (for example license.pdf instead of license.3402bc5d.pdf)

I tried two ways:

file config-overrides.js:

const path = require('path');

module.exports = function override(config, env) {
  config.module = {
   config.module.rules = config.module.rules.concat([
    {
      include: [path.resolve(__dirname, 'src/doc')],
      loader: 'file-loader',
      options: { name: "static/media/[name].pdf"}
    }
  ]);
  return config;
};

and

module.exports = function override(config, env) {
   config.module.rules = config.module.rules.concat([
    {
      test:/\.pdf$/,
      loader: 'file-loader',
      options: { name: "static/media/[name].pdf"}
    }
  ]);
  return config;
};

But it didn't work. In build I receive 2 files: with number and without. But I need only one

evgeniya-osmakova commented 2 years ago

I've found the solution:

module.exports = function override(config, env) {
  config.module.rules[1].oneOf = config.module.rules[1].oneOf.map((one) => {
    if (one.options && one.options.name && one.exclude) {
      one.exclude = [/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/, /\.pdf$/];
    }
    return one;
  }).concat([
    {
      test:/\.pdf$/,
      loader: 'file-loader',
      options: { name: "/docs/[name].[ext]"}
    }
  ]);

  return config;
};