E
E
Evtera2021-04-26 03:09:30
webpack
Evtera, 2021-04-26 03:09:30

Why does webpack create duplicates?

Hello. There is the following webpack config. The problem is that if I use some files in css / scss, for example, images in background-image / fonts in font-face, then in the dist folder, webpack creates copies of files with a hash in the root folder, and does not refer to the copied assets folder with the same files. What could be the reason?

const path = require("path");
const HTMLWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCssAssetWebpackPlugin = require("optimize-css-assets-webpack-plugin");
const TerserWebpackPlugin = require("terser-webpack-plugin");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");

const isDev = process.env.NODE_ENV === "development";
const isProd = !isDev;

const optimization = () => {
  const config = {
    splitChunks: {
      chunks: "all",
    },
  };

  if (isProd) {
    config.minimizer = [new OptimizeCssAssetWebpackPlugin(), new TerserWebpackPlugin()];
  }

  return config;
};

const cssLoaders = (extra) => {
  const loaders = [
    {
      loader: MiniCssExtractPlugin.loader,
    },
    "css-loader",
  ];

  if (extra) {
    loaders.push(extra);
  }

  return loaders;
};

const plugins = () => {
  const base = [
    new HTMLWebpackPlugin({
      template: "./index.html",
    }),
    new CleanWebpackPlugin(),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, "src/assets/"),
          to: path.resolve(__dirname, "dist/assets/"),
        },
      ],
    }),
    new MiniCssExtractPlugin({
      filename: "./styles/[name].[contenthash].css",
    }),
  ];

  if (isProd) {
    base.push(new BundleAnalyzerPlugin());
  }

  return base;
};

module.exports = {
  context: path.resolve(__dirname, "src"),
  mode: "development",
  entry: ["@babel/polyfill", "./js/index.js"],
  output: {
    filename: "./js/[name].[contenthash].js",
    path: path.resolve(__dirname, "dist"),
  },
  resolve: {
    /* список расширений при импорте */
    // extensions: [""],

    /* упрощение вложенности. В import ./ меняется на @ */
    alias: {
      "@js": path.resolve(__dirname, "src/js/"),
      "@": path.resolve(__dirname, "src"),
    },
  },
  optimization: optimization(),
  devServer: {
    port: 5500,
    open: true,
    hot: isDev,
  },
  devtool: isDev ? "source-map" : false,
  plugins: plugins(),
  module: {
    rules: [
      {
        test: /\.css$/,
        use: cssLoaders(),
      },
      {
        test: /\.scss$/,
        use: cssLoaders("sass-loader"),
      },
      { test: /\.(png|jpg|svg|jpeg)$/, use: ["file-loader"] },
      { test: /\.(ttf|woff|woff2)$/, use: ["file-loader"] },
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env"],
          },
        },
      },
    ],
  },
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VyacheslavWebDev, 2022-02-02
@VyacheslavWebDev

https://v4.webpack.js.org/loaders/file-loader/

{ 
    test: /\.(png|jpg|svg|jpeg)$/, 
    use: ["file-loader"], 
    options: {
        outputPath: 'Название папки для файлов'
    } 
},
{ 
    test: /\.(ttf|woff|woff2)$/, 
    use: ["file-loader"], 
    options: {
        outputPath: 'Название папки для файлов'
    }
 }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question