laravel-mix/laravel-mix

Assets file name collision

Aberta

#3.061 aberto em 11 de ago. de 2021

 (9 comentários) (0 reação) (0 responsável)JavaScript (828 forks)batch import
enhancementhelp wanted

Métricas do repositório

Stars
 (5.221 estrelas)
Métricas de merge de PR
 (Nenhuma PRs mesclada em 30d)

Description

I want to open this issue again with one more case https://github.com/JeffreyWay/laravel-mix/issues/2910 We have vue components and each component can have its own assets inside the components folder.

It was a matter of time when we come to a situation when two different components have two different images with the same name. Rename images? It's not a solution anyway.

For now, we use this temp workaround:

mix.extend('addHashToImagesFileNames', (webpackConfig) => {
  const imagesRule = webpackConfig.module.rules.find((rule) => {
    const { test } = rule;
    return test && typeof test.test === 'function' && test.test('.png');
  });

  if (!imagesRule) return;

  imagesRule.use.forEach(({ options }) => {
    if (options.name && typeof options.name === 'function') {
      options.name = (path) => {
        if (!/node_modules|bower_components/.test(path)) {
          return 'assets/images/[name].[hash:8].[ext]';
        }

        return (
          'assets/images/vendor/' +
          path
            .replace(/\\/g, '/')
            .replace(
              /((.*(node_modules|bower_components))|images|image|img|assets)\//g,
              ''
            ) +
          '?[hash]'
        );
      }
    }
  });
});
mix.addHashToImagesFileNames();

I have read the argument here https://github.com/JeffreyWay/laravel-mix/pull/2911#issuecomment-811079865 But why not do it optional? Just add addHashToFilenames option with default false value.

Guia do colaborador