如果默認生成的 HTML 文件不適合需求,可以創建/使用自定義模板。
一是通過 inject 選項,然后傳遞給定制的 HTML 文件。html-webpack-plugin 將會自動注入所有需要的 CSS, js, manifest 和 favicon 文件到標記中。
plugins: [
new HtmlWebpackPlugin({
title: 'Custom template',
template: 'myIndex.html', // Load a custom template
inject: 'body' // Inject all scripts into the body
})
]
自定義的myIndex.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>
參考:http://www.cnblogs.com/haogj/p/5160821.html
另一個是配置html-webpack-plugin
使之直接為項目生成一個或多個HTML文件(HTML文件個數由插件實例的個數決定),
並將webpack打包后輸出的所有腳本文件自動添加到插件生成的HTML文件中。
以下例子將通過配置,可以將根目錄下用戶自定義的HTML文件作為插件生成HTML文件的模板。另外,還可以通過向插件傳遞參數,控制HTML文件的輸出。
1.在項目根目錄下安裝插件
cnpm install html-webpack-plugin --save-dev
2.在webpack配置文件頭部require html-webpack-plugin模塊,並保存引用至htmlWebpackPlugin[變量]。
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = { entry: { main:'./src/script/main.js' }, output: { path: './dist', filename: 'js/[name].bundle.js' }, plugins:[ new htmlWebpackPlugin() ] }
4.配置參數。為新建的對象實例傳入一個對象字面量參數,初始化對象實例的屬性
plugins: [ new webpack.DefinePlugin({ 'process.env': require('../config/dev.env') }), new webpack.HotModuleReplacementPlugin(), new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update. new webpack.NoEmitOnErrorsPlugin(), // https://github.com/ampedandwired/html-webpack-plugin new HtmlWebpackPlugin({ filename: 'index.html', template: 'index.html', inject: true, favicon: resolve('favicon.ico'), title: 'vue-element-admin', path: config.dev.assetsPublicPath + config.dev.assetsSubDirectory }), ]
5.在HTML中使用
<body> <script src=<%= htmlWebpackPlugin.options.path %>/tinymce4.7.5/tinymce.min.js></script> <div id="app"></div> <!-- built files will be auto injected --> </body>
參考:
https://www.jianshu.com/p/89414e89c563