自己配置vue项目

webpack.dev.js

javascript 复制代码
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

javascript 复制代码
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',
}
相关推荐
kyriewen7 小时前
Anthropic 估值逼近万亿美元,Claude Sonnet 5 + Claude Science 一天两连发
前端·ai编程·claude
小徐_23339 小时前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app
山河木马10 小时前
矩阵专题3-怎么创建投影矩阵(uProjectionMatrix)
javascript·webgl·计算机图形学
天蓝色的鱼鱼11 小时前
关于 CSS 你可能不知道的属性,但关键时刻很有用
前端·css
泯泷12 小时前
第 2 篇:设计第一套字节码:Opcode、Instruction 与 Constant Pool
前端·javascript·安全
妙码生花12 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十五):优化细节、网络请求封装
前端·后端·ai编程
泯泷12 小时前
第 1 篇:从 1 + 2 开始:亲手写出第一台 JSVM
前端·javascript·安全
团团崽_七分甜12 小时前
Spring Boot 核心知识点总结
前端
lichenyang45312 小时前
从一个按钮开始,理解 ASCF 框架到底在做什么
前端