webpack 学习

1.拆分、合并

webpack-merge

devlopment

production

2.webpack-dev-serve

```

devServer:

{

    port

    contentBase

    progress

    open

    compress

    proxy:{

        'xxx':{

            target:'...',

            pathRewrite:{

                "^/api"

            },

            changeOrigin:true

        }

    }

}

```

3.处理样式

css

```

module:{

    rules:[

        {

            test:/\.scss$/,

            loader:[

                'style-loader',

                'css-loader',

                'postcss-loader'//兼容性

                'sass-loader'//css预处理器

            ]

        }

    ]

}

```

```

package.json

broswerlist:{}

```

4.处理图片

```

module:{

    rules:[

        {

            test:/\.(jpeg|png)$/,

            loader:'file-loader',//开发环境

        }

        {

            test:/\.(jpeg|png)$/,

            loader:'url-loader',//生产环境

            option:{

                limit:5 * 1024,

                outputPath:'/img/'

            }

        }

    ]

}

```

5.多入口

```

entry:{

    index:"./src/index.js"

    other:"./src/other.js"

}

```

```

output:{

    filename:'[name].js'

    path:path.resolve(__dirname:'../dist')

}

```

```

plugins:[

    new htmlWebpackPlugin({

        template:'./src/index.html'

        filename:'index.html'

        chunks:['index']

    }),

    new htmlWebpackPlugin({

        template:'./src/othe.html'

        filename:'other.html'

        chunks:['other']

    }),

]

```

6.抽离css

生产环境

```

module:{

    rules:[

        {

            test:/\.css$/,

            loader:[

                MiniCssExtractPlugin.loader,

                'css-loader',

                'postcss-loader'//兼容性

               

            ]

        }

    ]

}

```

```

plugins:[

    MiniCssExtractPlugin({

        filename:'css/main.[contentHash:8].css'

    })

]

```

优化

```

optimization:[

    minizer:[

        new TerserPlugin(),

        new OptimizeCssAssetsWebpackPlugin()

    ]

]

```

7.抽离公共代码

公共模块代码

不需要重复打包,单独抽离公共模块

第三方模块

单独抽离成模块

生产环境配置

```

plugins:[

    new htmlWebpackPlugin({

        template:'./src/index.html'

        filename:'index.html'

        chunks:['index','vendor','common']

    }),

    new htmlWebpackPlugin({

        template:'./src/othe.html'

        filename:'other.html'

        chunks:['other','common']

    }),

]

```

```

optimization:[

    minizer:[

        new TerserPlugin(),

        new OptimizeCssAssetsWebpackPlugin()

    ],

    splitChunks:{

        //all 同步异步都分割

        // async 异步代码分割 import('lodash')

        // initial 同步代码分割 import lodash from 'lodash'

        chunks:'all',

        cacheGroups:{

            vendor:{//第三方模块

                name:'vendor',

                priority:1,//优先级越高越先处理

                test:/node_modules/,

                minSize:0,

                minChunks:1

            },

            common:{//公共模块

                name:'common',

                priority:0,

                minSize:0,

                minChunks:2

            }

        }

    }

]

```

 

               

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值