webpack4.0构建vue环境

从0开始配置Vue项目环境

需要的包

cnpm install webpack webpack-cli html-webpack-plugin mini-css-extract-plugin optimize-css-assets-webpack-plugin webpack-dev-server clean-webpack-plugin webpack-merge @babel/core @babel/polyfill @babel/preset-env autoprefixer babel-loader css-loader file-loader postcss-loader sass-loader url-loader vue vue-loader vuex axios vue-template-compiler -D

 

配置流程

  • 新建一个build目录

  • 在build文件夹下放置配置文件

  • 各自配置如下

 

  • webpack.common.js

  • const HtmlWebpackPlugin = require('html-webpack-plugin'),
        {CleanWebpackPlugin} = require('clean-webpack-plugin'),
        path = require('path'),
        VueLoaderPlugin = require('vue-loader/lib/plugin');
    
    module.exports = {
    
        entry: {
            filename: './src/main.js'
        },
    
        module: {
            rules: [
                {
                    test: /\.js$/,
                    use: [
                        {
                            loader: 'babel-loader',
                        }
                    ]
                },
                {
                    test: /\.(png|jpg|gif)$/,
                    use: [
                        {
                            loader: 'url-loader',
                            options: {
                                name: '[name]_[hash].[ext]',
                                outputPath: 'image/',
                                limit: 10240
                            }
                        }
                    ]
                },
                {
                    test: /\.(eot|ttf|svg)$/, //打包字体
                    use: {
                        loader: 'file-loader',
                    }
                },
            ]
        },
    
        plugins: [
            new HtmlWebpackPlugin({
                template: './src/index.html'
            }),
    
            new CleanWebpackPlugin(),
    
            new VueLoaderPlugin()
        ],
    
        output: {
            path: path.resolve(__dirname, '../dist'),
            filename: '[name].[contenthash].js',
            chunkFilename: '[name].[contenthash].js'
        }
    };

     

 

  • webpack.dev.js
  • const HtmlWebpackPlugin = require('html-webpack-plugin'),
        {CleanWebpackPlugin} = require('clean-webpack-plugin'),
        path = require('path'),
        VueLoaderPlugin = require('vue-loader/lib/plugin');
    
    
    module.exports = {
    
        entry: {
            filename: './src/main.js'
        },
    
        module: {
            rules: [
                {
                    test: /\.js$/,
                    use: [
                        {
                            loader: 'babel-loader',
                        }
                    ]
                },
                {
                    test: /\.(png|jpg|gif)$/,
                    use: [
                        {
                            loader: 'url-loader',
                            options: {
                                name: '[name]_[hash].[ext]',
                                outputPath: 'image/',
                                limit: 10240
                            }
                        }
                    ]
                },
                {
                    test: /\.(eot|ttf|svg)$/, //打包字体
                    use: {
                        loader: 'file-loader',
                    }
                },
            ]
        },
    
        plugins: [
            new HtmlWebpackPlugin({
                template: './src/index.html'
            }),
    
            new CleanWebpackPlugin(),
    
            new VueLoaderPlugin()
        ],
    
        output: {
            path: path.resolve(__dirname, '../dist'),
            filename: '[name].[contenthash].js',
            chunkFilename: '[name].[contenthash].js'
        }
    };

     

  • webpack.prod.js
  • const commonConfig = require('./webpck.common'),
        merge = require('webpack-merge'),
        webpack = require('webpack'),
        MiniCssExtractPlugin = require('mini-css-extract-plugin'),
        OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin'),
        path = require('path'),
    
        cssConfig = [
            MiniCssExtractPlugin.loader,
            'css-loader',
            'postcss-loader'
        ],
    
        scssConfig = [
            MiniCssExtractPlugin.loader,
            {
                loader: 'css-loader',
                options: {
                    importLoaders: 2,
                }
            },
            'sass-loader',
            'postcss-loader'
        ],
    
        prodConfig = {
            mode: 'production',
            devtool: 'cheap-module-eval-source-map',
            output: {
                path: path.resolve(__dirname, '../dist'),
                filename: '[name].[contentpath].js',
                chunkFilename: '[name].[contentpath].js',
            },
    
            module: {
                rules: [
                    {
                        test: /\.vue$/, //打包字体
                        use: {
                            loader: 'vue-loader',
                            options: {
                                loaders: {
                                    css: cssConfig,
                                    scss: scssConfig,
                                }
                            }
                        }
                    },
                    {
                        test: /\.css/,
                        use: cssConfig
                    },
                    {
                        test: /\.scss/,
                        use: scssConfig
                    }
                ]
            },
    
            optimization: {
                minimizer: [new OptimizeCssAssetsWebpackPlugin({})],
                runtimeChunk: {
                    name: 'manifest'
                },
                splitChunks: {
                    chunks: 'all', //配置了all就是相当于配置了下面的默认配置
                    cacheGroups: {
                        vendors: {
                            test: /[\\/]node_modules[\\/]/,
                            priority: -10,
                            filename: 'vendors.js',
                        },
                        // default: false
                    }
                    // minSize: 30000,
                    // maxSize: 0,
                    // minChunks: 1,
                    // maxAsyncRequests: 5,
                    // maxInitialRequests: 3,
                    // automaticNameDelimiter: '~',
                    // name: true,
                },
    
            },
    
            plugins: [
                new MiniCssExtractPlugin({
                    filename: '[name].css',
                    chunkFilename: '[id].chunk.css'
                }),
            ]
    
        };
    
    module.exports = merge(prodConfig, commonConfig);

     

  • 配置tree sharing的忽略项

  1. 在packge.json中配置 sideEffects

    "sideEffects": ["*.(c|sc)ss","*.vue"]

新建一个在src目录下的 .babelrc 文件 在.babelrc下配置 babel 的配置
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "useBuiltIns": "usage"
      }
    ]
  ]
}

 

在src目录下新建 一个 postcss.config.js 文件 在postcss.config.js中配置自动添加厂商前缀
module.exports = {
    plugins: [
        require('autoprefixer')
    ]
};

 

大功告成!

转载于:https://www.cnblogs.com/guiyuan123/p/11001928.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# URL: http://kongkong.fence.wmdev2.lsh123.com/ # jl-mis(接龙后台管理系统) 项目是基于vue-cli 3+ 构建的,请先将vue-cli升级到3+版本,vue-cli3 传送门(https://cli.vuejs.org/zh/)。 ## 技术栈 vue2 + vuex + vue-router + element-ui + webpack + ES6/7 + axios + less + flex + svg ### 项目图形化管理界面 ``` vue ui ``` ## 项目运行 #### 注意:由于涉及大量的 ES6/7 等新属性,node 需要 8.0 以上版本 ``` npm install npm run serve ``` ### 打包 ``` npm run build ``` ### 代码的 lint ``` npm run lint ``` # 项目布局 ``` . ├── public // HTML 和静态资源 │   ├── favicon.ico // 图标 │   ├── index.html // 入口html文件 ├── src // 源码目录 │   ├── assets // 静态资源 │   │   ├── images // 公共图片 │   ├── components // 组件 │   │   ├── common // 公共组件 │   │   ├── page // 页面 │   ├── libs // 封装库 │   │   ├── storage.js // 对cookie 和 localStorage 的封装 │   ├── plugins // 引用的插件 │   │   ├── axios.js // 对axios的的封装(拦截器相关) │   │   ├── element.js // 引入element-ui │   ├── router │   │   └── router.js // 路由配置 │   ├── service // 数据交互统一调配 │   │   ├── service.js // 获取数据的统一调配文件,对接口进行统一管理 │   ├── store // vuex的状态管理 │   │   ├── actions.js // 配置actions │   │   ├── getters.js // 配置getters │   │   ├── store.js // 引用vuex,创建store │   │   ├── modules // store模块 │   │   │   ├── urlGroups.js // 路由分组(权限相关) │   │   ├── mutation-types.js // 定义常量muations名 │   │   └── mutations.js // 配置mutations │   └── style // 样式字体相关 │   ├── fonts // 字体相关 │   ├── utility.less // 公共样式文件 │   ├── mixin.less // 样式配置文件 │   └── normalize.css // 样式重置 │   ├── App.vue // 页面入口文件 │   ├── main.js // 程序入口文件,加载各种公共组件 ├── vue.config.js // vue-cli 3+ 暴露出来的webpack相关的配置文件 . ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值