美文网首页
webpack基本配置

webpack基本配置

作者: Splendid飞羽 | 来源:发表于2021-09-12 20:57 被阅读0次
1、webpack2.x的配置
// webpack.config.js
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
    entry: {
        boundle: __dirname + '/src/main.js'
        // 此处可以配置多入口
    }, 
    output: {
        path: __dirname + '/build/',    // 存放打包后文件的路径
        filename: '[name]?[hash].js'   // 打包后文件名
    },
    devtool: 'source-map', // 配置生成的source-map
    devServer: {
        port: '9000',
        inline: true,
        historyApiFallback: true,
        open: true,
        hot: true // 热加载
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/ // 编译打包时排除node_modules文件夹
            },
            {
                test: /\.css$/,
                // 此处结构需要做一下修改
                use: [
                    'style-loader', 'css-loader?modules&importLoaders=1',
                    {
                        loader: 'postcss-loader',
                        options: {
                            plugins: [
                                require('autoprefixer')
                            ]
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
             //注意传的参数是一个对象
            filename:'index-[hash].html',
            template:'index.html'   //传一个模板,就是根目录下的index.html
        }),
        new webpack.HotModuleReplacementPlugin() // 热加载插件
    ]
};
2、package.json的配置
// package.json
{
  "name": "webpack-cli",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack",
    "dev": "webpack-dev-server --devtool eval --progress --content-base build --hot",
    "build": "webpack --config ./webpack.prod.config.js --progress"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "autoprefixer": "^7.1.6",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-es2015": "^6.24.1",
    "css-loader": "^0.28.7",
    "html-webpack-plugin": "^2.30.1",
    "postcss-loader": "^2.0.8",
    "style-loader": "^0.19.0",
    "webpack": "^3.8.1",
    "webpack-dev-server": "^2.9.3"
  }
}

参考文章
https://blog.csdn.net/zjsfdx/article/details/78357837
https://blog.csdn.net/luo_tianhong/article/details/79710669

相关文章

网友评论

      本文标题:webpack基本配置

      本文链接:https://www.haomeiwen.com/subject/wamtgltx.html