plugins: [ new webpack.optimize.UglifyJsPlugin({ // 压缩webpack 后生成的代码较长时间,一般推到生产环境中才使用 compress:{ warnings: false } }), new htmlWebpackPlugin({ // webpack 指定目录(package内设置)生成静态HTML文件 title: "自动生成网页标题", filename: "test.html", template: "temIndex.html", hash: true, // true | false。若是是true,会给全部包含的script和css添加一个惟一的webpack编译hash值。这对于缓存清除很是有用。 inject: true, // | 'head' | 'body' | false ,注入全部的资源到特定的 template 或者 templateContent 中,若是设置为 true 或者 body,全部的 javascript 资源将被放置到 body 元素的底部,'head' 将放置到 head 元素中。 chunks: ["app"] // 使用chunks 须要指定entry 入口文件中的哪个模块 }) ]
使用 npm 安装这个插件javascript
$ npm install html-webpack-plugin@2 --save-dev
这个插件能够帮助生成 HTML 文件,在 body 元素中,使用 script 来包含全部你的 webpack bundles,只须要在你的 webpack 配置文件中以下配置:css
var HtmlWebpackPlugin = require('html-webpack-plugin') var webpackConfig = { entry: 'index.js', output: { path: 'dist', filename: 'index_bundle.js' }, plugins: [new HtmlWebpackPlugin()] }
这将会自动在 dist 目录中生成一个名为 index.html 的文件,内容以下:html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Webpack App</title> </head> <body> <script src="index_bundle.js"></script> </body> </html>
若是你有多个 webpack 入口点,它们都会被包含在生成的 script 元素中。vue
若是有任何的 CSS 资源包含在 webpack 输出中(例如,使用 ExtractTextPlugin 提炼出的 css ),这些将会使用 link 包含在 HTML 页面的 head 元素中。java
能够进行一系列的配置,支持以下的配置信息node
下面的示例演示了如何使用这些配置。webpack
{ entry: 'index.js', output: { path: 'dist', filename: 'index_bundle.js', hash: true }, plugins: [ new HtmlWebpackPlugin({ title: 'My App', filename: 'assets/admin.html' }) ] }
经过在配置文件中添加屡次这个插件,来生成多个 HTML 文件。git
{ entry: 'index.js', output: { path: 'dist', filename: 'index_bundle.js' }, plugins: [ new HtmlWebpackPlugin(), // Generates default index.html new HtmlWebpackPlugin({ // Also generate a test.html filename: 'test.html', template: 'src/assets/test.html' }) ] }
若是默认生成的 HTML 文件不适合你的须要看,能够建立本身定义的模板。方便的方式是经过 inject 选项,而后传递给定制的 HTML 文件。html-webpack-plugin 将会自动注入全部须要的 CSS, js, manifest 和 favicon 文件到标记中。github
plugins: [ new HtmlWebpackPlugin({ title: 'Custom template', template: 'my-index.html', // Load a custom template inject: 'body' // Inject all scripts into the body }) ]
my-index.html 文件web
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"/> <title><%= htmlWebpackPlugin.options.title %></title> </head> <body> </body> </html>
若是你有模板加载器,可使用它来解析这个模板。
module: { loaders: [ { test: /\.hbs$/, loader: "handlebars" } ] }, plugins: [ new HtmlWebpackPlugin({ title: 'Custom template using Handlebars', template: 'my-index.hbs', inject: 'body' }) ]
另外,若是你的模式是一个字符串,可使用 templateContent 传递它。
plugins: [ new HtmlWebpackPlugin({ inject: true, templateContent: templateContentString }) ]
若是 inject 特性不适合你的须要,你但愿彻底控制资源放置。 能够直接使用 lodash 语法,使用 default template 做为起点建立本身的模板。
templateContent 选项也能够是一个函数,以便使用其它语言,好比 jade:
plugins: [ new HtmlWebpackPlugin({ templateContent: function(templateParams, compilation) { // Return your template content synchronously here return '..'; } }) ]
或者异步版本
plugins: [ new HtmlWebpackPlugin({ templateContent: function(templateParams, compilation, callback) { // Return your template content asynchronously here callback(null, '..'); } }) ]
注意,若是同时使用 template 和 templateContent ,插件会抛出错误。
变量 o 在模板中是在渲染时传递进来的数据,这个变量有以下的属性:
"htmlWebpackPlugin": { "files": { "css": [ "main.css" ], "js": [ "assets/head_bundle.js", "assets/main_bundle.js"], "chunks": { "head": { "entry": "assets/head_bundle.js", "css": [ "main.css" ] }, "main": { "entry": "assets/main_bundle.js", "css": [] }, } } }
若是在 webpack 配置文件中,你配置了 publicPath,将会反射正确的资源
可使用 chunks 来限定特定的块。
plugins: [ new HtmlWebpackPlugin({ chunks: ['app'] }) ]
还可使用 excludeChunks 来排除特定块。
plugins: [ new HtmlWebpackPlugin({ excludeChunks: ['dev-helper'] }) ]
经过事件容许其它插件来扩展 HTML。
html-webpack-plugin-before-html-processing
html-webpack-plugin-after-html-processing
html-webpack-plugin-after-emit
使用方式:
compilation.plugin('html-webpack-plugin-before-html-processing', function(htmlPluginData, callback) { htmlPluginData.html += 'The magic footer'; callback(); });
下面是一个在使用的webpack配置文件
————————————————————————————————————————————————
/** * Created by Administrator on 2016/8/14. */ var webpack = require("webpack") var htmlWebpackPlugin = require("html-webpack-plugin") module.exports = { entry: { app: "./lib/main.js" }, output: { path: "./", filename: "bundle.js" }, module: { loaders: [ { test: /\.vue$/, exclude: /node_modules/, loader: 'vue' }, { test: /\.js$/, exclude: /node_modules/, loader: 'babel', query: { presets: ['es2015'] // babelrc 文件必定不要忘了 } } ] }, plugins: [ new webpack.optimize.UglifyJsPlugin({ // 压缩webpack 后生成的代码较长时间,一般推到生产环境中才使用 compress:{ warnings: false } }), new htmlWebpackPlugin({ // webpack 指定目录(package内设置)生成静态HTML文件 title: "自动生成网页标题", filename: "test.html", template: "temIndex.html", hash: true, // true | false。若是是true,会给全部包含的script和css添加一个惟一的webpack编译hash值。这对于缓存清除很是有用。 inject: true, // | 'head' | 'body' | false ,注入全部的资源到特定的 template 或者 templateContent 中,若是设置为 true 或者 body,全部的 javascript 资源将被放置到 body 元素的底部,'head' 将放置到 head 元素中。 chunks: ["app"] // 使用chunks 须要指定entry 入口文件中的哪个模块 }) ] }
.babelrc
{ "presets": ["es2015","stage-0"], "plugins": ["transform-runtime"] }