current position:Home>Record webpackdemo configuration

Record webpackdemo configuration

2022-06-24 08:39:40swag_ Special actor

effect

In the production environment : All files are put under the corresponding folder according to the suffix .

 Insert picture description here

Start

webpack The official tutorial

  1. install webpack-merge

npm install --save-dev webpack-merge

  1. New at root 3 individual webpack Configuration file for
     Insert picture description here
    webpack.common.js

Be careful : You need to install url-loader and file-loader Two development dependencies . Otherwise, an error will be reported .
npm install --save-dev url-loader
npm install --save-dev file-loader
The development dependency packages are as follows :

 Insert picture description here

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    
  entry: {
    
    app: './src/index.js',
  },
  plugins: [
    new HtmlWebpackPlugin({
    
      title: 'Production',
      template: './index.html',
    }),
  ],
  module: {
    
    rules: [
      {
    
        test: /\.css$/i,
        use: ['style-loader', 'css-loader'],
      },
      {
    
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/i,
        include: [path.resolve('src')], //  stay src The lookup 
        loader: 'url-loader',
        options: {
    
          limit: 10000,
          name: 'img/[name].[hash:7].[ext]'
        }
      },
      {
    
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i,
        include: [path.resolve('src')],
        loader: 'url-loader',
        options: {
    
          limit: 10000,
          name: 'media/[name].[hash:7].[ext]'
        }
      },
      {
    
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/i,
        include: [path.resolve('src')],
        type: 'asset/resource',
        generator: {
    
          filename: 'fonts/[name].[hash:7].[ext]'
        },
        parser: {
    
          dataUrlCondition: {
    
            maxSize: 10 * 1024,
          },
        },
      }
    ]
  },
  output: {
    
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    clean: true,
  },
  optimization: {
    
    runtimeChunk: 'single', //  Single entry 
  },
  performance: {
    
    hints: false //  Don't warn 
  }
};


webpack.prod.js

const {
     merge } = require('webpack-merge');
const common = require('./webpack.common.js');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const productionGzipExtensions = ["js", "css"];

module.exports = merge(common, {
    
  mode: 'production',
  output: {
    
    publicPath: '/webpackDemo/', //  This is http The root path for the server to run ( For example, we are online www.baidu.com/indexbeta This is the root path , It needs to be changed to '/indexbeta/')
    filename: 'js/[name].[chunkhash].js', //  take js With [ original file name ].[ Hash value ].js name , And put it on js Under the folder 
    chunkFilename: 'js/[id].[chunkhash].js' //  Package entry file name [id].[ Hash value ].js name , And put it on js Under the folder 
  },
  plugins: [
    new CopyWebpackPlugin({
    
      //  Copy folder plugin 
      patterns: [
        {
    
          from: path.resolve(__dirname, './static'),
          to: 'static',
        }
      ]
    }),
    new CompressionWebpackPlugin({
    
      //  Turn on the compression plug-in 
      filename: "[path][base].gz",
      algorithm: "gzip",
      test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"), //  Match file name 
      threshold: 10240, //  Yes 10K Compress the above data 
      minRatio: 0.8,
      deleteOriginalAssets: false //  Delete source file or not 
    })
  ]
});

webpack.dev.js

const {
     merge } = require('webpack-merge');
const common = require('./webpack.common.js');

module.exports = merge(common, {
    
  mode: 'development',
  devtool: 'inline-source-map',
  devServer: {
    
    host: 'localhost',
    port: 9917,
    static: './dist',
  },
});

  1. Start command settings
     Insert picture description here

copyright notice
author[swag_ Special actor],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/175/202206240615219464.html

Random recommended