webpack4 化繁为简(二)

发布时间:2019-08-15 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了webpack4 化繁为简(二)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

书接上文,继续干,配置一些常用的插件使支持

  1. uglifyjs js压缩插件
    webpack默认已经有uglifyjs,所以只需要引入就可以使用.
    在webpack.config.js中配置:
const path=require('path');
const webpacKDEvServer=require('webpack-dev-server');
const extractTextWebpackPlugin=require('extract-text-webpack-plugin');
const uglify=require('uglifyjs-webpack-plugin');
module.exports={
    mode:"development",
    entry:[
        path.join(__dirname,'./src/entry.js'),
        path.join(__dirname,'./src/entry1.js'),
    ],
    output:{
        path:path.join(__dirname,'dist'),
        filename:'[name].js'
    },
    module:{
        rules:[
            {
                test:/.css$/,
                use:extractTextWebpackPlugin.extract({
                    fallback:"style-loader",
                    use: ['css-loader',]
                })
            }
        ]
    },
    plugins:[
        new extractTextWebpackPlugin({
            filename:'index.css'
        }),
        new uglify()
    ],
    devServer:{
        contentBase:path.join(__dirname,'dist'),
        host:'localhost',
        comPress:true,
        port:8888
    }
}

webpack4 化繁为简(二)

2.htML-webpack-plugin 生成HTML

将dist的目录下面的index.html移入src目录,并且删除script 以及css 的引入标签,然    

后安装html-webpack-plugin包。

cnpm install --save-dev html-webpack-plugin

在webpack.config.js中修改如下

const path=require('path');
const webpackDevServer=require('webpack-dev-server');
const extractTextWebpackPlugin=require('extract-text-webpack-plugin');
const uglify=require('uglifyjs-webpack-plugin');
const htmlWebpackPlugin=require('html-webpack-plugin')
module.exports={
    mode:"development",
    entry:[
        path.join(__dirname,'./src/entry.js'),
        path.join(__dirname,'./src/entry1.js'),
    ],
    output:{
        path:path.join(__dirname,'dist'),
        filename:'[name].js'
    },
    module:{
        rules:[
            {
                test:/.css$/,
                use:extractTextWebpackPlugin.extract({
                    fallback:"style-loader",
                    use: ['css-loader',]
                })
            }
        ]
    },
    plugins:[
        new extractTextWebpackPlugin({
            filename:'index.css'
        }),
        new uglify(),
        new htmlWebpackPlugin({
            minify:{
                removeAttributeQuotes:true
            },
            hash:true,
            template:path.join(__dirname,'./src/index.html')
        })
    ],
    devServer:{
        contentBase:path.join(__dirname,'dist'),
        host:'localhost',
        comPRess:true,
        port:8888
    }
}

webpack4 化繁为简(二)

  1. file-loader、url-loader html-wIThimg-loader图片处理以及打包
    在src下面建一个images的文件夹放入一张图片
    分别在css和html 的img标签中引入
cnpm install --save-dev file-loader url-loader
cnpm install --save html-withimg-loader

webpack.config.js修改如下:

const path=require('path');
const webpackDevServer=require('webpack-dev-server');
const extractTextWebpackPlugin=require('extract-text-webpack-plugin');
const uglify=require('uglifyjs-webpack-plugin');
const htmlWebpackPlugin=require('html-webpack-plugin');
const webset={
    publicPath:'192.168.0.175:8888/'
}
module.exports={
    mode:"development",
    entry:[
        path.join(__dirname,'./src/entry.js'),
        path.join(__dirname,'./src/entry1.js'),
    ],
    output:{
        path:path.join(__dirname,'dist'),
        filename:'[name].js'
    },
    module:{
        rules:[
            {
                test:/.css$/,
                use:extractTextWebpackPlugin.extract({
                    fallback:"style-loader",
                    use: ['css-loader',]
                })
            },
            {
                test:/.(png|jpg|gif)$/,
                use:[{
                    loader:'url-loader',
                    options:{
                        limit:8192,
                        outputPath:'images/'
                    }
                }]
            },
            {
                test:/.(htm|html)$/i,
                use:['html-withimg-loader']
            }
        ]
    },
    plugins:[
        new extractTextWebpackPlugin({
            filename:'index.css'
        }),
        new uglify(),
        new htmlWebpackPlugin({
            minify:{
                removeAttributeQuotes:true
            },
            hash:true,
            template:path.join(__dirname,'./src/index.html')
        })
    ],
    devServer:{
        contentBase:path.join(__dirname,'dist'),
        host:'localhost',
        compress:true,
        port:8888
    }
}

webpack4 化繁为简(二)

4.配置babel(很关键,可以让浏览器支持es6语法。)

cnpm install babel-loader babel-core babel-preset-env

在webpack.config.js中添加loader:

webpack4 化繁为简(二)

后续抽空补上打包jquery以及第三方插件的webpack的配置。。。。。。

脚本宝典总结

以上是脚本宝典为你收集整理的webpack4 化繁为简(二)全部内容,希望文章能够帮你解决webpack4 化繁为简(二)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。