當(dāng)前位置:首頁 >  站長 >  編程技術(shù) >  正文

詳解html-webpack-plugin使用

 2020-10-26 11:22  來源: 腳本之家   我來投稿 撤稿糾錯

  域名預(yù)訂/競價,好“米”不錯過

這篇文章主要介紹了html-webpack-plugin使用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

最近在react項目中初次用到了html-webapck-plugin插件,用到該插件的兩個主要作用:

為html文件中引入的外部資源如script、link動態(tài)添加每次compile后的hash,防止引用緩存的外部文件問題

可以生成創(chuàng)建html入口文件,比如單頁面可以生成一個html文件入口,配置N個html-webpack-plugin可以生成N個頁面入口

1、安裝

cnpm i webpack-plugin -D

2、在webpack.config.json中引用

const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin') //第一步
module.exports = {
    entry: path.join(__dirname, './src/main.js'),
    output: {
        path: path.join(__dirname, './dist'),
        filename: 'bundle.js',
    },
    mode: 'development',
    devServer: {
        open: true,
        port: 8080,
        hot: true,
        contentBase: 'src'
    },
    plugins: [
        new htmlWebpackPlugin({ //第二步
        template: path.join(__dirname, './src/index.html'), //指定生成模板的路徑
        filename: 'index.html' //指定生成頁面的名稱
        })
    ]
}

3、html-webpack-plugin的作用

一、在內(nèi)存中生成一個指定模板的文件,在訪問時速度更快

二、自動為指定模板文件添加bundle.js文件

總結(jié)

到此這篇關(guān)于詳解html-webpack-plugin使用的文章就介紹到這了,更多相關(guān)html-webpack-plugin使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

來源:腳本之家

鏈接:https://www.jb51.net/web/745178.html

申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)標(biāo)簽
heml
webpower

相關(guān)文章

熱門排行

信息推薦