webpack-contrib / file-loader

File Loader
MIT License
1.86k stars 257 forks source link

Feature: compiler output options support for hash #313

Closed aarefiev closed 5 years ago

aarefiev commented 5 years ago

Hi!.

Thanks for great loader. It's really save my time :).

Some days ago I worked on configuring webpack for my own project and I found that I can't use the same output filename settings for all loaders. Especially, for file-loader.

Here is the part of webpack.config.js:

output: {
  path: outputPath,
  filename: '[name].[contenthash].js',
  publicPath: `${publicPath}/`,
  hashDigestLength: 8
},
module: {
  rules: [
    {
      test: /\.(png|jpg|gif|svg|cur)(\?v=\d+\.\d+.\d+)?$/,
      include: paths.src,
      use: [
        {
          loader: 'file-loader',
          options: {
            name: '[name].[hash].[ext]',
            publicPath: `${publicPath}/`
          }
        }
      ]
    }
  ]
},
plugins: [
  htmlWebpackPlugin,
  new MiniCssExtractPlugin({
    filename: '[name].[contenthash].css'
  })
]

After compilation I found that the hash part of name of extracted css and bundled files are truncated, but all other assets that I uploaded as is with file-loader are not. As solution, I should pass maxLength to options.name (in format [name].[hash:8].[ext]) and it will work as I expected.

I think it is not good because I should pass the same settings for each loader. Maybe is better to support compiler settings? I know that you using loader-utils for process files, that's why I submitted my request there (https://github.com/webpack/loader-utils/pull/120).

I will be glad to hear what you think about it. Thanks :')

alexander-akait commented 5 years ago

@aarefiev please use issue template in future, issues without template was closed and ignored

Yep, it is make sense, we need fix it on loader-utils for better future integration, so it is not related to file-loader, because other loaders also use this library for hashes and have same problem. Please create issue in loader-utils and attach link to the PR. Thanks!