[js高手之路]深入浅出webpack教程系列6-插件使用之html-webpack-plugin配置(下)

news/2024/7/1 22:20:45

上文我们对html-webpack-plugin的实例htmlWebpackPlugin进行了遍历分析,讲解了几个常用属性( inject, minify )以及自定义属性的添加,本文,我们继续深入他的配置选项的探讨.

一、chunks选项

这个属性非常有用,可以指定某个页面加载哪些chunk( 如:js文件 )

我们可以用他做多个页面模板的生成. 比如,我们在实际开发中,做一个博客网站,一般来说有首页,文章列表页,文章详情页等等,这些页面都有一个特点,都要引入一些公共的js文件以及该页面特有的js文件,比如:

首页( index.html ) 引入 main.js, index.js

文章列表页( list.html ) 引入 main.js, list.js

文章详情页( detail.html ) 引入 main.js, detail.js

传统方式,一个个的打开文件,拷贝修改,如果后期维护,又是一堆文件中,查找,拷贝,修改。很容易出错,而且效率低下,我们看下webpack是如何提高效率,开启前端工业化开发革命道路

webpack.dev.config.js文件代码:

var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
    entry : {
        main : './src/js/main.js',
        index : './src/js/index.js',
        list : './src/js/list.js',
        detail : './src/js/detail.js'
    },
    output : {
        //__dirname,就是当前webpack.config.js文件所在的绝对路径
        path : __dirname + '/dist', //输出路径,要用绝对路径
        filename : 'js/[name]-[hash].bundle.js', //打包之后输出的文件名
    },
    plugins: [
        new HtmlWebpackPlugin({
            template : './index.html',
            title : '博客首页-by ghostwu',
            filename : 'index.html',
            inject : true,
            chunks : ['main', 'index']
        }),
        new HtmlWebpackPlugin({
            template : './index.html',
            title : '列表页-by ghostwu',
            filename : 'list.html',
            inject : true,
            chunks : ['main', 'list']
        }),
        new HtmlWebpackPlugin({
            template : './index.html',
            title : '文章详情页-by ghostwu',
            filename : 'detail.html',
            inject : true,
            chunks : ['main', 'detail']
        })
    ]
};

然后在src的js目录下面,创建main.js, index.js,list.js,detail.js文件,执行打包( npm run d )就会在dist下面生成3个文件,各自引入到各自的js文件,下次要维护的时候,只要修改这个配置文件,再次打包就可以了,是不是很方便

clipboard.png

二、excludeChunks选项

这个很好理解,就是有很多chunks,排除不要加载的

webpack.dev.config.js文件代码:

var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
    entry : {
        main : './src/js/main.js',
        index : './src/js/index.js',
        list : './src/js/list.js',
        detail : './src/js/detail.js'
    },
    output : {
        //__dirname,就是当前webpack.config.js文件所在的绝对路径
        path : __dirname + '/dist', //输出路径,要用绝对路径
        filename : 'js/[name]-[hash].bundle.js', //打包之后输出的文件名
    },
    plugins: [
        new HtmlWebpackPlugin({
            template : './index.html',
            title : '博客首页-by ghostwu',
            filename : 'index.html',
            inject : true,
            excludeChunks : ['list','detail']
        }),
        new HtmlWebpackPlugin({
            template : './index.html',
            title : '列表页-by ghostwu',
            filename : 'list.html',
            inject : true,
            excludeChunks : ['index','detail']
        }),
        new HtmlWebpackPlugin({
            template : './index.html',
            title : '文章详情页-by ghostwu',
            filename : 'detail.html',
            inject : true,
            excludeChunks : ['list','index']
        })
    ]
};

把配置文件修改之后,再用npm run d执行一次打包,跟使用chunks的效果是一样的

三,把页面src引入文件的方式,改成用script标签嵌入的方式,减少http请求( 提高加载性能)

要达到这个目的,我们再安装一个插件html-webpack-inline-source-plugin

安装:npm install --save-dev html-webpack-inline-source-plugin

webpack.dev.config.js文件代码:

var HtmlWebpackPlugin = require('html-webpack-plugin');
var HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
module.exports = {
    entry : {
        main : './src/js/main.js',
        index : './src/js/index.js',
        list : './src/js/list.js',
        detail : './src/js/detail.js'
    },
    output : {
        //__dirname,就是当前webpack.config.js文件所在的绝对路径
        path : __dirname + '/dist', //输出路径,要用绝对路径
        filename : 'js/[name]-[hash].bundle.js', //打包之后输出的文件名
    },
    plugins: [
        new HtmlWebpackPlugin({
            template : './index.html',
            title : '博客首页-by ghostwu',
            filename : 'index.html',
            inject : true,
            excludeChunks : ['list','detail'],
            inlineSource : '.(js|css)$' //全部内嵌
        }),
        new HtmlWebpackInlineSourcePlugin(),
        new HtmlWebpackPlugin({
            template : './index.html',
            title : '列表页-by ghostwu',
            filename : 'list.html',
            inject : true,
            excludeChunks : ['index','detail']
        }),
        new HtmlWebpackPlugin({
            template : './index.html',
            title : '文章详情页-by ghostwu',
            filename : 'detail.html',
            inject : true,
            excludeChunks : ['list','index']
        })
    ]
};

执行npm run d打包命令之后,就会把dist/index.html文件的js和css改成内嵌方式


http://www.niftyadmin.cn/n/2896005.html

相关文章

为什么要有Spring AOP?

一、Web开发演进到一定阶段的痛点 我们在初学习Java Web的时候,应该都经历了以下的阶段: (1)一个主函数main中包含了所有的方法; (2)将主函数中的方法进行拆分封装,抽取为一个个的方…

[Linux应用]通过sysfs在用户空间使用GPIO中断

通过使用sysfs,Linux GPIO可以支持在用户空间进行GPIO的控制或获取状态。这样可以使用简单的工具,比如“echo”来设置输出GPIO的电平或使用“cat”来读取输入GPIO的当前值。1、配置内核中sysfs下的GPIO支持要想在用户空间访问GPIO,需要在sysf…

Zabbix自动添加Mysql多实例监控

1、定义每个实例的端口 cat mysql_port 3306 3307 3308 3309 33103311 3312 3313 2、给所有实例创建一个zabbix用户只允许本地访问,生产环境具体权限各位看官自己看着给吧O(∩_∩)O GRANT ALL PRIVILEGES ON *.* TO zabbix127.0.0.1 IDENTIFIED BY zabbix; 3、创建…

Linux文件系统初步

(一)准备工作 1,虚拟机网卡选择桥接网络,同时在虚拟网络编辑器中设置桥接接口 (二)远程连接Linux Linux支持远程连接,它是基于ssh/secure shell协议进行的 1,首先输入命令…

Wireshark获取邮件内容

收发邮件的协议客户端为smtp、pop,web浏览器为http,这里过滤http包,发现main.jsp(管理后台的页面,可能是index.php、main.php、manage.php、guanli.php) 在readhtml.jsp中得到答案 请求中有accept-encoding…

为故障而设计:AWS S3云存储故障给我们的启示

如今 2017 年,云是重要业务技术选型的最好选择。使用云也为管理基础设施带来了很多益处,包括提升灵活性、可扩展性,同时降低了 IT 成本。但是上周我们目睹了 AWS S3 停机故障 [1],看来即使是最可靠的服务提供商也可能遇到倒霉的一…

baseValidate

转载于:https://www.cnblogs.com/finddata/p/10930740.html

Java中间缓存变量

Java中间缓存变量,在看《Java面试宝典》时,看到面试例题,关于Java缓存变量,一直一脸茫然,查了部分资料,在这里和大家分享一下。题目: public class Test {public static void main(String[] arg…