webpack 常用配置

Posted 洛阳之晨,譬如临安初雨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了webpack 常用配置相关的知识,希望对你有一定的参考价值。

webpack.config.js

const path = require(‘path‘);
const webpack = require(‘webpack‘);
const htmlWebpackPlugin = require(‘html-webpack-plugin‘);
const vueLoaderPlugin = require(‘vue-loader/lib/plugin‘); //引入这行

module.exports = {
    entry: ‘./src/main.js‘,
    output: {
        path: path.resolve(__dirname, ‘./dist‘),
        filename: ‘boundle.js‘
    },
    mode: ‘development‘,
    devServer: {
        open: true,//自动打开浏览器
        port: 3000,//运行端口号
        contentBase: ‘src‘,//指定跟目录
        hot: true,//启用热更新
        openPage: ‘index.html‘//设置默认启动页面
    },
    plugins: [
        //配置插件的节点
        new webpack.HotModuleReplacementPlugin(),//热更新
        new vueLoaderPlugin(),
        new htmlWebpackPlugin({
            //创建一个内存中生成HTML页面的插件
            //并且把打包的boundle.js自动注入到html页面中
            template: path.join(__dirname, ‘./src/index.html‘),
            filename: ‘index.html‘
        })
    ],
    module: {
        rules: [
            //第三方模块匹配规则
            {
                test: /.css$/,
                use: [
                    ‘style-loader‘,
                    {
                        loader: ‘css-loader‘,
                        options: {
                            sourceMap: true
                        }
                    },
                    {
                        loader: ‘postcss-loader‘,
                        options: {
                            ident: ‘postcss‘,
                            sourceMap: true,
                            plugins: (loader) => {
                                require(‘autoprefixer‘)({ overrideBrowserslist: [‘> 0.15% in CN‘] })
                            }
                        }
                    }
                ]
            },
            {
                //处理图片路径的loader
                test: /.(jpg|png|gif|bmp|jpeg)$/,
                use: {
                    loader: ‘url-loader‘,
                    options: {
                        limit: 8501,
                        //哈希-图片原名称。图片原后缀
                        name: ‘[hash:8]-[name].[ext]‘
                    }
                }
            },
            {
                test: /.(ttf|eot|svg|woff|woff2)$/,
                use:[‘url-loader‘]
                // use: {
                //     loader: ‘url-loader‘,
                //     options: {

                //     }
                // }
            },
            // test:/.(ttf|eot|woff|woff2|svg)$/,
            // use:[‘file-loader‘]//1.把你的资源移动到输出目录2.返回最终引入资源的url
            {
                test: /.js$/,
                exclude: /(node_modules|bower_components)/,//排除这两个里面的JS不编译
                use: {
                    loader: ‘babel-loader‘,
                }
            },
            {
                test: /.vue$/,
                use: [
                    {
                        loader: ‘vue-loader‘,
                    }
                ]
            },
            {
                test: /.scss$/,
                use: ["style-loader", "css-loader", "sass-loader"]
            }
        ]
    }
};

package.json(VUE流派)

{
  "name": "webpack-test",
  "version": "1.0.0",
  "description": "test",
  "main": "main.js",
  "dependencies": {
    "jquery": "^3.4.1",
    "node-sass": "^4.12.0",
    "vue": "^2.6.10",
    "webpack-dev-server": "^3.8.0"
  },
  "devDependencies": {
    "autoprefixer": "^9.6.1",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-stage-0": "^6.24.1",
    "css-loader": "^3.2.0",
    "file-loader": "^4.2.0",
    "html-webpack-plugin": "^3.2.0",
    "postcss-loader": "^3.0.0",
    "sass-loader": "^8.0.0",
    "style-loader": "^1.0.0",
    "url-loader": "^2.1.0",
    "vue-loader": "^15.7.1",
    "vue-router": "^3.1.3",
    "vue-template-compiler": "^2.6.10",
    "vuex": "^3.1.1",
    "webpack": "^4.39.3",
    "webpack-cli": "^3.3.8"
  },
  "scripts": {
    "dev2": "webpack-dev-server --open --port 3000 --contentBase src --hot",
    "dev": "webpack-dev-server",
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

 

以上是关于webpack 常用配置的主要内容,如果未能解决你的问题,请参考以下文章

vue+vux+es6+webpack移动端常用配置步骤

webpack常用配置

webpack基础+webpack配置文件常用配置项介绍+webpack-dev-server

webpack基础+webpack配置文件常用配置项介绍+webpack-dev-server

webpack 常用配置

配置 VScode 编辑器 (前端篇)