自己配置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',
}
相关推荐
支支დ5 小时前
VO by Vercel 前端特定优势:为什么它是构建 AI 应用的新范式
前端·人工智能
香芋芋圆5 小时前
AI 冲击内卷之下,普通前端如何破局?WebGIS—— 低门槛突围赛道
前端·javascript·人工智能·学习·职场发展
INS_KF6 小时前
【编程笔记】成员函数中两个 const 的区别(const Data &getData() const;)
前端·javascript·笔记
宿6746 小时前
vue3-async
前端·javascript·vue.js
YXWik67 小时前
记录前端请求接口在浏览器请求响应的Preview和Response展示的一样的问题
前端
2501_928996227 小时前
GPT-4o换DeepSeek迁移成本多少?中科热备解析API聚合平台技术账本
前端·数据库·人工智能
东风破_7 小时前
从跨域到 WebSocket:前端跨域方案、SSE 与双向实时通信详解
前端·后端
原则猫7 小时前
TS 类型工具
前端
excel7 小时前
Nuxt + Twin CSS 中宽度超过屏幕时底部出现空白的原因与解决方案
前端·javascript
计算机魔术师9 小时前
美国司法部正式站队 OpenAI,AI 训练的版权账要这么算了
前端