自己配置vue项目

webpack.dev.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {
    VueLoaderPlugin } = require('vue-loader');
const ESLintPlugin = require('eslint-webpack-plugin');
function getStyle(pre) {
   
    return [
        'vue-style-loader',
        'css-loader',
        {
   
          loader: 'postcss-loader',
          options: {
   
            postcssOptions: {
   
              plugins: [
                [
                  'postcss-preset-env',
                  {
   
                    // 其他选项
                  },
                ],
              ],
            },
          },
        },
        pre
    ].filter((item) => !!item)
}
module.exports = {
   
    entry: './src/main.js',
    output: {
   
        path: path.resolve(__dirname, '../dist'),
        filename: 'static/js/[name].[contenthash:10].js',
        chunkFilename: 'static/js/[name].[contenthash:10].chunk.js',
        assetModuleFilename: 'static/media/[hash][ext][query]'
    },
    module: {
   
        rules: [
            {
   
                test: /\.css$/i,
                use: getStyle(),
            },
            {
   
                test: /\.less$/i,
                use: getStyle('less-loader')
            },
            {
   
                test: /\.s[ac]ss$/i,
                use: getStyle('sass-loader')
            },
            {
   
                test: /\.styl$/,
                use: getStyle('stylus-loader')
            },
            // 其他资源
            {
   
                test: /\.(woff2?|ttf|mp3|mp4)/,
                type: 'asset/resource',
            },
            // 图片资源
            {
   
                test: /\.(jpe?g|png|svg)/,
                type: 'asset',
                parser: {
   
                    dataUrlCondition: {
   
                        maxSize: 10 * 1024 // 10kb
                    }
                }
            },
            {
   
                test: /\.m?js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
   
                    loader: 'babel-loader',
                    options: {
   
                        cacheDirectory: true,
                        cacheCompression: false
                    }
                }
            },
            {
   
                test: /\.vue$/,
                loader: 'vue-loader'
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
    template: './public/index.html' }),
        new VueLoaderPlugin(),
        new ESLintPlugin({
   
            context: path.resolve(__dirname, '../src'),
            exclude: 'node_modules'
        })
    ],
    optimization: {
   
        splitChunks: {
   
          // include all types of chunks
          chunks: 'all',
        },
    },
    mode: 'development',
    devtool: 'eval-source-map',
    devServer: {
   
        port: 3000,
        historyApiFallback: true,
        hot: true,
        open: true
    }
}

webpack.prod.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {
    VueLoaderPlugin } = require('vue-loader');
const ESLintPlugin = require('eslint-webpack-plugin');
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserPlugin = require("terser-webpack-plugin");
function getStyle(pre) {
   
    return [
        MiniCssExtractPlugin.loader,
        'css-loader',
        {
   
          loader: 'postcss-loader',
          options: {
   
            postcssOptions: {
   
              plugins: [
                [
                  'postcss-preset-env',
                  {
   
                    // 其他选项
                  },
                ],
              ],
            },
          },
        },
        pre
    ].filter((item) => !!item)
}
module.exports = {
   
    entry: './src/main.js',
    output: {
   
        path: path.resolve(__dirname, '../dist'),
        filename: 'static/js/[name].[contenthash:10].js',
        chunkFilename: 'static/js/[name].[contenthash:10].chunk.js',
        assetModuleFilename: 'static/media/[hash][ext][query]'
    },
    module: {
   
        rules: [
            {
   
                test: /\.css$/i,
                use: getStyle(),
            },
            {
   
                test: /\.less$/i,
                use: getStyle('less-loader')
            },
            {
   
                test: /\.s[ac]ss$/i,
                use: getStyle('sass-loader')
            },
            {
   
                test: /\.styl$/,
                use: getStyle('stylus-loader')
            },
            // 其他资源
            {
   
                test: /\.(woff2?|ttf|mp3|mp4)/,
                type: 'asset/resource',
            },
            // 图片资源
            {
   
                test: /\.(jpe?g|png|svg)/,
                type: 'asset',
                parser: {
   
                    dataUrlCondition: {
   
                        maxSize: 10 * 1024 // 10kb
                    }
                }
            },
            {
   
                test: /\.m?js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
   
                    loader: 'babel-loader',
                    options: {
   
                        cacheDirectory: true,
                        cacheCompression: false
                    }
                }
            },
            {
   
                test: /\.vue$/,
                loader: 'vue-loader'
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
    template: './public/index.html' }),
        new VueLoaderPlugin(),
        new ESLintPlugin({
   
            context: path.resolve(__dirname, '../src'),
            exclude: 'node_modules'
        }),
        new MiniCssExtractPlugin({
   
            filename: 'static/style/[name].[contenthash:10].css',
            chunkFilename: 'static/js/[name].[contenthash:10].chunk.css',
        })
    ],
    optimization: {
   
        minimizer: [
            // 在 webpack@5 中,你可以使用 `...` 语法来扩展现有的 minimizer(即 `terser-webpack-plugin`),将下一行取消注释
            // `...`,
            new CssMinimizerPlugin(),
            new TerserPlugin()
        ],
        splitChunks: {
   
          // include all types of chunks
          chunks: 'all',
        },
    },
    mode: 'production',
    devtool: 'source-map',
}

相关推荐

  1. 自己配置vue项目

    2024-01-20 08:20:02       53 阅读
  2. 配置vue3+vite+eslint+prettierrc项目

    2024-01-20 08:20:02       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-20 08:20:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-20 08:20:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-20 08:20:02       87 阅读
  4. Python语言-面向对象

    2024-01-20 08:20:02       96 阅读

热门阅读

  1. Spring AOP学习

    2024-01-20 08:20:02       38 阅读
  2. springboot如何创建全局线程池

    2024-01-20 08:20:02       53 阅读
  3. python入门,文件的编码,读取,写入,追加

    2024-01-20 08:20:02       60 阅读
  4. django 中group by 以及sum count

    2024-01-20 08:20:02       58 阅读
  5. Webpack5入门到原理13:开发服务器&自动化

    2024-01-20 08:20:02       60 阅读
  6. Linux 常用命令分享与示例

    2024-01-20 08:20:02       34 阅读
  7. 解决更新Xcode 15.2后,下载 iOS_17 Simulator失败

    2024-01-20 08:20:02       56 阅读
  8. Apache Flink 1.15正式发布

    2024-01-20 08:20:02       49 阅读
  9. 【LeetCode2744】最大字符串配对数目

    2024-01-20 08:20:02       61 阅读
  10. 获取mac地址,内网ip,当前ip位置信息

    2024-01-20 08:20:02       45 阅读