[js高手之路]深入浅出webpack教程系列索引目录:

上文我们对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改成内嵌方式

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

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

    上文我们讲到了options的配置和获取数据的方式,本文,我们继续深入options的配置 一.html-webpack-plugin插件中的options除了自己定义了一些基本配置外,我们是可以任意 ...

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

    还记得我们上文中的index.html文件吗? 那里面的script标签还是写死的index.bundle.js文件,那么怎么把他们变成动态的index.html文件,这个动态生成的index.htm ...

  3. [js高手之路]深入浅出webpack教程系列3-配置文件webpack.config.js详解(下)

    本文继续接着上文,继续写下webpack.config.js的其他配置用法. 一.把两个文件打包成一个,entry怎么配置? 在上文中的webpack.dev.config.js中,用数组配置entr ...

  4. [js高手之路]深入浅出webpack教程系列7-( babel-loader,css-loader,style-loader)的用法

    什么是loader呢,官方解释为文件的预处理器,通俗点说webpack在处理静态资源的时候,需要加载各种loader,比如,html文件,要用html-loader, css文件要用css-loade ...

  5. [js高手之路]深入浅出webpack教程系列9-打包图片(file-loader)用法

    我们还是接着上文继续,本文我们要讲的是图片资源的打包,图片在静态排版中,经常出现的两个位置是css通过background引入背景,还有一种就是在html模板文件中用img标签引入的方式,如果要在we ...

  6. [js高手之路]深入浅出webpack教程系列8-(postcss-loader,autoprefixer,html-loader,less-loader,ejs-loader)用法

    我们接着上文,那么在上篇文章的最后,写到了css-loader的用法,如果你用心发现,就能看到我在style.css样式文件中写了一个这样的样式: div { transition: all ease ...

  7. [js高手之路]深入浅出webpack教程系列1-安装与基本打包用法和命令参数

    [js高手之路]深入浅出webpack教程系列索引目录: [js高手之路]深入浅出webpack教程系列1-安装与基本打包用法和命令参数 [js高手之路]深入浅出webpack教程系列2-配置文件we ...

  8. [js高手之路]深入浅出webpack教程系列2-配置文件webpack.config.js详解(上)

    [js高手之路]深入浅出webpack教程系列索引目录: [js高手之路]深入浅出webpack教程系列1-安装与基本打包用法和命令参数 [js高手之路]深入浅出webpack教程系列2-配置文件we ...

  9. [js高手之路]深入浅出webpack系列2-配置文件webpack.config.js详解

    接着上文,重新在webpack文件夹下面新建一个项目文件夹demo2,然后用npm init --yes初始化项目的package.json配置文件,然后安装webpack( npm install ...

随机推荐

  1. 【分享】01. Eclipse for PHP + phpStudy 搭建php开发环境

    配置php编译器 配置phpStudy服务器项目发布目录 修改hosts文件127.0.0.1      www.350zx.cn 新建项目 启动的你的phpStudy  

  2. 记Angular与Django REST框架的一次合作(1):分离 or 不分离,it's the question

    前言:本次尝试源于我们内部的一个项目,由于前端逻辑比较复杂,就打算将前后端分开来开发.由于之前用Django开发过软件,对Angular.js(Angular 1.0版)也有一定的了解,因此就将技术路 ...

  3. 53. leetcode557. Reverse Words in a String III

    557. Reverse Words in a String III Given a string, you need to reverse the order of characters in ea ...

  4. grunt 的安装和简单使用

    安装Grunt命令行 npm install -g grunt-cli 创建package.json,如果有package.json包,可以直接npm install加载依赖组件 npm init 安 ...

  5. Linux - 简明Shell编程13 - 用户输入(UserInput)

    脚本地址 https://github.com/anliven/L-Shell/tree/master/Shell-Basics 示例脚本及注释 1 - arguments #!/bin/bash i ...

  6. 局部加权回归LOWESS

    1. LOWESS 用kNN做平均回归: \[ \hat{f(x)} = Ave(y_i | x_i \in N_k(x)) \] 其中,\(N_k(x)\)为距离点x最近k个点组成的邻域集合(nei ...

  7. Redis sentinel 哨兵模式集群方案配置

    第一个方案是创建 redis cluster,第二种方案就是用哨兵模式来进行主从替换以及故障恢复.兵模式集群方案配置 一.sentinel介绍 Sentinel作用: 1):Master状态检测 2) ...

  8. redux深入理解之中间件(middleware)

    理解reduce函数 reduce() 方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值. arr.reduce([callback, initi ...

  9. Weex和React Native框架对比与选择

    工作原理 大致基本类同,JS-Native桥和前端渲染框架,只是使用框架技术不一样: Weex 阿里内部早期研发的一个通过 JSON 数据描述 native 渲染的项目WeApp以及Vue.js这款优 ...

  10. 200行的Node爬虫花了半天的时间把网易云上的30万首歌曲信息都抓取回来了

    早两天在网易云听歌看评论的时候,突然想把网易云上所有歌曲都抓取下来然后按照评论数进行一次排名,把评论数超过10万的歌曲都听一次,于是便有了这个项目. 因为只是一个小前端,所以使用了Node来写这个爬虫 ...