shellscape / webpack-manifest-plugin

webpack plugin for generating asset manifests
MIT License
1.44k stars 184 forks source link

Add directory in key #283

Closed cnakh closed 2 years ago

cnakh commented 2 years ago

I use the plugin and it works well. But I noticed that, use with image, if the image have the same name even it in different directory it shows only one and especially want to read the content to access the key not easy because it show only name not include directory.

The structure of file in src directory

src
    - style.scss
    - index.js
    - image.jpeg
    - frontend
        - image_1.jpeg
        - image_2.jpeg

But after built it showed only filename

{
    "style.css": "[name].[contenthash].[ext]",
    "index.js": "[name].[contenthash].[ext]",
    "image.jpeg": "[name].[contenthash].[ext]",
    "image_1.jpeg": "[name].[contenthash].[ext]",
    "image_1.jpeg": "[name].[contenthash].[ext]",
}

Is it possible to have bellow structure?

{
    "style.css": "[name].[contenthash].[ext]",
    "index.js": "[name].[contenthash].[ext]",
    "image.jpeg": "[name].[contenthash].[ext]",
    "frontend/image_1.jpeg": "[name].[contenthash].[ext]",
    "frontend/image_1.jpeg": "[name].[contenthash].[ext]",
}

Here is the full webpack.config.js

const path = require('path')
const { WebpackManifestPlugin } = require('webpack-manifest-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts')

module.exports = {
  mode: 'development',
  // mode: 'production',
  entry: {
    index: './src/index.js',
    styles: [
      './src/style.scss',
      './src/style.css',
    ],
  },
  output: {
    filename: '[name].[contenthash].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    clean: true,
    publicPath: '',
    assetModuleFilename: '[name].[contenthash].[ext]',
  },
  module: {
    rules: [
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
        // loader: 'file-loader',
        /*options: {
          name: '[path][name].[contenthash].[ext]',
        },*/
      },
      {
        test: /\.(sa|sc|c)ss$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          // 'postcss-loader',
          'sass-loader',
        ],
      },
    ],
  },
  optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin({
        minimizerOptions: {
          preset: [
            'default',
            {
              discardComments: {
                removeAll: true,
              },
            },
          ],
        },
        minify: CssMinimizerPlugin.cleanCssMinify,
      }),
    ],
  },
  plugins: [
    new RemoveEmptyScriptsPlugin(),
    new WebpackManifestPlugin({
      basePath: 'dist/',
      fileName: 'manifest.json',
      isModuleAsset: true,
    }),
    new MiniCssExtractPlugin({
      filename: '[name].[contenthash].css',
    }),
  ],
}
shellscape commented 2 years ago

If you are sure this is a bug or documentation issue, please take the time to refile this issue using the issue template.

Hey there, thanks for opening an issue - Unfortunately you missed (or may have disregarded) the message about issue templates being required. Because our issue template was removed, we assume that this is a support request, and that's not something we offer here. Also, if you refile, please don't ignore Issue sections. They're in the template because they're useful to the maintainers of this project.