资讯专栏INFORMATION COLUMN

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

jokester / 1324人阅读

摘要:上文我们对的实例进行了遍历分析,讲解了几个常用属性以及自定义属性的添加,本文,我们继续深入他的配置选项的探讨一选项这个属性非常有用,可以指定某个页面加载哪些如文件我们可以用他做多个页面模板的生成比如,我们在实际开发中,做一个博客网站,一

上文我们对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文件,下次要维护的时候,只要修改这个配置文件,再次打包就可以了,是不是很方便

二、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改成内嵌方式

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/91933.html

相关文章

  • [js高手]深入浅出webpack教程系列5-插件使用html-webpack-plugin

    摘要:上文我们讲到了的配置和获取数据的方式,本文,我们继续深入的配置一插件中的除了自己定义了一些基本配置外,我们是可以任意的添加自定义的数据文件,就是当前文件所在的绝对路径输出路径,要用绝对路径打包之后输出的文件名教你学我们在中新增了个 上文我们讲到了options的配置和获取数据的方式,本文,我们继续深入options的配置 一、html-webpack-plugin插件中的options...

    lentoo 评论0 收藏0
  • [js高手]深入浅出webpack教程系列4-插件使用html-webpack-plugin

    摘要:还记得我们上文中的文件吗那里面的标签还是写死的文件,那么怎么把他们变成动态的文件,这个动态生成的文件会动态引入我们打包后生成的文件呢,我们可以使用插件,首先安装这个插件,好的,接下来就开始用这个插件了官方参考文档插件通用用法 还记得我们上文中的index.html文件吗? 那里面的script标签还是写死的index.bundle.js文件,那么怎么把他们变成动态的index.html...

    qpal 评论0 收藏0
  • [js高手]深入浅出webpack教程系列1-安装与基本打包用法和命令参数

    摘要:,我想大家应该都知道或者听过,是前端一个工具可以让各个模块进行加载预处理再进行打包。 webpack,我想大家应该都知道或者听过,Webpack是前端一个工具,可以让各个模块进行加载,预处理,再进行打包。现代的前端开发很多环境都依赖webpack构建,比如vue官方就推荐使用webpack.废话不多说,我们赶紧开始吧. 第一步、安装webpack 新建文件夹webpack->再在web...

    pubdreamcc 评论0 收藏0
  • [js高手]深入浅出webpack教程系列2-置文件webpack.config.js详解(上

    摘要:接着上文,重新在文件夹下面新建一个项目文件夹,然后用初始化项目的配置文件,然后安装,然后创建基本的项目文件夹结构,好了,我们的又一个基本项目结构就搭建好了第一开始通过文件配置我们的项目首先在项目文件夹下面,新建一个文件,这个文件可 接着上文,重新在webpack文件夹下面新建一个项目文件夹demo2,然后用npm init --yes初始化项目的package.json配置文件,然后安...

    moven_j 评论0 收藏0
  • [js高手]深入浅出webpack教程系列3-置文件webpack.config.js详解(下

    摘要:本文继续接着上文,继续写下的其他配置用法一把两个文件打包成一个,怎么配置在上文中的中,用数组配置文件代码,就是当前文件所在的绝对路径输出路径,要用绝对路径打包之后输出的文件名然后在目录下面新建一个文件,代码如下之前的文件的代码告诉你怎么学习 本文继续接着上文,继续写下webpack.config.js的其他配置用法. 一、把两个文件打包成一个,entry怎么配置? 在上文中的webpa...

    xiangchaobin 评论0 收藏0

发表评论

0条评论

jokester

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<