自己配置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',
}
相关推荐
正在学习前端的---小方同学2 小时前
vue-easy-tree树状结构
前端·javascript·vue.js
毕业设计制作和分享3 小时前
springboot150基于springboot的贸易行业crm系统
java·vue.js·spring boot·后端·毕业设计·mybatis
键盘不能没有CV键5 小时前
【图片处理】✈️HTML转图片字体异常处理
前端·javascript·html
yantuguiguziPGJ6 小时前
WPF 联合 Web 开发调试流程梳理(基于 Microsoft.Web.WebView2)
前端·microsoft·wpf
大飞记Python6 小时前
部门管理|“编辑部门”功能实现(Django5零基础Web平台)
前端·数据库·python·django
tsumikistep7 小时前
【前端】前端运行环境的结构
前端
你的人类朋友7 小时前
【Node】认识multer库
前端·javascript·后端
Aitter7 小时前
PDF和Word文件转换为Markdown的技术实现
前端·ai编程
mapbar_front8 小时前
面试问题—上家公司的离职原因
前端·面试
昔人'9 小时前
css使用 :where() 来简化大型 CSS 选择器列表
前端·css