首頁 > web前端 > js教程 > 主體

在實戰專案中如何使用Installation插件

php中世界最好的语言
發布: 2018-06-09 11:23:06
原創
1382 人瀏覽過

這次帶給大家怎麼在實戰項目中使用Installation插件,怎樣在實戰項目中使用Installation插件的注意事項有哪些,以下就是實戰案例,一起來看一下。

這個外掛程式用來簡化建立服務 webpack bundle 的 HTML 文件,尤其是對於在文件名稱中包含了 hash 值,而這個值在每次編譯的時候都會改變的情況。你既可以讓這個外掛程式來幫助你自動產生 HTML 文件,也可以使用 lodash 範本載入產生的 bundles,或是自己載入這些 bundles。

怎麼在實戰專案中使用Installation外掛程式

使用npm 安裝這個外掛程式

$ npm install html-webpack-plugin@2 --save-dev
登入後複製

Basic Usage

#這個外掛可以幫助產生HTML 文件,在body 元素中,使用script 來包含所有你的webpack bundles,只需要在你的webpack 設定檔中如下設定:

var HtmlWebpackPlugin = require('html-webpack-plugin')
var webpackConfig = {
 entry: 'index.js',
 output: {
  path: 'dist',
  filename: 'index_bundle.js'
 },
 plugins: [new HtmlWebpackPlugin()]
}
登入後複製

這將會自動在dist目錄中產生一個名為index.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 元素中。

如果有任何的 CSS 資源包含在 webpack 輸出中(例如,使用 ExtractTextPlugin 提煉出的 css ),這些將會使用 link 包含在 HTML 頁面的 head 元素中。

Configuration

 可以進行一系列的配置,支援如下的設定資訊

  1. title: 用來產生頁面的title 元素

  2. filename: 輸出的HTML 檔名,預設是index.html, 也可以直接配置帶有子目錄。

  3. template: 範本檔案路徑,支援載入器,例如 html!./index.html

  4. inject: true | 'head' | 'body' | false  ,注入所有的資源到特定的template 或templateContent 中,如果設定為true 或body,所有的javascript 資源將被放置到body 元素的底部,'head' 將放置到head 元素中。

  5. favicon: 加入特定的 favicon 路徑到輸出的 HTML 檔案中。

  6. minify: {} | false , 傳遞html-minifier 選項給minify 輸出

  7. hash: true | false, 如果為true, 將新增一個唯一的webpack 編譯hash 到所有包含的腳本和CSS 文件,對於解除cache 很有用。

  8. cache: true | false,如果為 true, 這是預設值,僅在檔案修改之後才會發布檔案。

  9. showErrors: true | false, 如果為true, 這是預設值,錯誤訊息會寫入到HTML 頁面中

  10. chunks:允許只加入某些區塊(例如,僅僅unit test 區塊)

  11. chunksSortMode: 允許控制區塊在新增到頁面之前的排序方式,支援的值:'none' | 'default ' | {function}-default:'auto'

  12. excludeChunks: 允許跳過某些區塊,(例如,跳過單元測試的區塊)

下面的範例示範如何使用這些配置。

{
 entry: 'index.js',
 output: {
  path: 'dist',
  filename: 'index_bundle.js',
  hash: true
 },
 plugins: [
  new HtmlWebpackPlugin({
   title: 'My App',
   filename: 'assets/admin.html'
  })
 ]
}
登入後複製

產生多個 HTML 檔案

透過在設定檔中新增多次這個插件,來產生多個 HTML 檔案。

{
 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 檔案到標記中。

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 檔案

<!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 在模板中是在渲染时传递进来的数据,这个变量有如下的属性:

1、htmlWebpackPlugin: 这个插件的相关数据 ◦

htmlWebpackPlugin.files: 资源的块名,来自 webpack 的 stats 对象,包含来自 entry 的从 entry point name 到 bundle 文件名映射。

"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,将会反射正确的资源

htmlWebpackPlugin.options: 传递给插件的配置。

2、webpack: webpack 的 stats 对象。

3、webpackConfig: webpack 配置信息。

过滤块

可以使用 chunks 来限定特定的块。

plugins: [
 new HtmlWebpackPlugin({
  chunks: ['app']
 })
]
登入後複製

还可以使用 excludeChunks 来排除特定块。

plugins: [
 new HtmlWebpackPlugin({
  excludeChunks: ['dev-helper']
 })
]
登入後複製

事件

通过事件允许其它插件来扩展 HTML。

  1. html-webpack-plugin-before-html-processing

  2. html-webpack-plugin-after-html-processing

  3. html-webpack-plugin-after-emit

使用方式:

compilation.plugin('html-webpack-plugin-before-html-processing', function(htmlPluginData, callback) {
 htmlPluginData.html += 'The magic footer';
 callback();
});
登入後複製

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

vue-cli组件导入使用步骤详解

使用webpack做出ReactApp

以上是在實戰專案中如何使用Installation插件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!