webpack4多页应用HTML按需添加入口依赖chunk【html-webpack-plugin & html-inline-entry-chunk-plugin】

摘要:
在webpack4中使用splitChunkPlugin时,根据需要将公共代码拆分为多个依赖项后,您需要在创建htmlWebpackPlugin时导入与条目文件依赖项对应的块。然而,html webpack插件的chunk配置项只能手动添加。如果不知道分割块,则无法知道相应html的依赖块,因此无法根据需要导入它。所以我用html webpack插件编写了一个插件,它很容易使用,效果很好。如果对你有帮助,我希望你能在github上给我一个小星星。

在webpack4中使用splitChunkPlugin时,根据需要将公共代码拆分为多个依赖后,需要在创建htmlWebpackPlugin时候按需引入对应入口文件依赖的chunk。但是html-webpack-plugin的chunk配置项只能手动添加,在没有得知拆分后的chunk情况下,无法得知对应html的依赖chunk,也就无法按需做引入。

因此鄙人自己写了个配合html-webpack-plugin的插件,使用方便,效果还行,如果有帮到你,希望能在github上赐我一颗小星星。

github地址:https://github.com/pomelott/html-inline-entry-chunk-plugin

html-inline-entry-chunk-plugin使用教程如下:

单页应用:

// webpack plugin config
    module.exports = {
        entry: {
            index: './src/js/index.js'
        },
        plugin: [
            new inlineHtmlEntryChunkPlugin(),
            // when useing inlineHtmlEntryChunkPlugin, the chunk param in htmlWebpackPlugin is invalid
            new htmlWebpackPlugin({
                entry: 'index',
                chunk: ['runtime'] //chunk is invalid
            })
        ]
    }

多页应用:

// webpack plugin config
    module.exports = {
        entry: {
            index: './src/js/index.js',
            list: './src/js/list.js'
        },
        plugin: [
            new inlineHtmlEntryChunkPlugin(),
            new htmlWebpackPlugin({
                entry: 'index'
            }),
            new inlineHtmlEntryChunkPlugin(),
            new htmlWebpackPlugin({
                entry: 'list'
            })
        ]
    }

配置项:

NameTypeDefaultDescription
inject{Object}{css: 'head', js: 'body'}control the assets of position in HTML
tag{Object}{}Add additional resource tags
tagPriority{Number}0Control the insertion order of entry chunk and other tags

示例:

module.exports = {
        plugin: [
            new htmlInlineEntryChunkPlugin({
                inject: {
                    js: 'body',
                    css: 'head'
                },
                tagPriority: 1,
                tag: {
                    head: [
                        'https://cdn.bootcdn.net/ajax/libs/basscss/8.1.0/css/basscss-cp.css',
                        'https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js'
                    ],
                    body: [
                        'https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.0/animate.compat.css',
                        'https://cdn.bootcdn.net/ajax/libs/Chart.js/3.0.0-alpha/Chart.esm.js'
                    ]
                }
            }),
            new htmlWebpackPlugin({
                entry: 'index'
            })
        ]
    }

免责声明:文章转载自《webpack4多页应用HTML按需添加入口依赖chunk【html-webpack-plugin & html-inline-entry-chunk-plugin】》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇asp.net core webapi 文件下载实现DataTable/DataSet 与XML 互转下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

2022年可用QQ机器人框架

以下框架信息均来源网络,排名不分前后,若需更新或新增,请私聊我。 1、Drea Robot 官网:https://drea.cc 测评:框架高效稳定,支持多种协议,多账号登录,界面清爽,社区完善,SDK简洁,包含酷Q Pro全部功能协议甚至更多,更重要的是,它基于GO-CQHTTP开源协议,因此完全免费! 2、Mirai机器人 官网:https://git...

聊聊 webpack 打包如何压缩包文件大小

想必很多人都经历过做完一个项目后,再打包发现某些文件非常大,导致页面加载时很慢,这就很影响用户体验了,所以在我经历了一些打包后,讲讲如何有效地缩小包体积,加快页面的首屏渲染 动态 polyfill 相信很多项目都会用到polyfill 那么一整个polyfill 会占据很多的空间,这个时候需要使用动态polyfill来解决这个问题了: 在 index.ht...

ubuntu 18.04安装PIL(Python Imaging Library )

本人也是个小萌新 安装过程也是曲折 现附上我的安装过程 1.百度教的 sudo apt-get install python-imaging sudo apt-get install libjpeg8 libjpeg62-dev libfreetype6 libfreetype6-dev 2.发现报错,其实是我自己机器的原因 然后又去配置源 我是ubun...

Nuxt.js使用详解

首先来讲一下服务端渲染   直白的说就是在服务端拿数据进行解析渲染,直接生成html片段返回给前端。具体用法也有很多种比如: 传统的服务端模板引擎渲染整个页面 服务渲染生成htmll代码块, 前端 AJAX 获取然后js动态添加   服务端渲染的优劣   首先是seo问题,前端动态渲染的内容是不能被抓取到的,而使用服务端渲染就可以解决这个问题。还有就是首屏...

IDEA必备插件

工欲善其事,必先利其器。 文章目录 `translation` `Alibaba Java Coding Guidelines` `CodeGlance` `Maven-Helper` `JRebel for Intellij` `Lombok plugin` `MyBatis Log Plugin` `Grep console` `CamelCas...

【引用】Qt——web网页和本地对象的交互

有时候可能需要在html网页中调用本地的对象方法。 比如说我点击了一个视频文件的链接,希望调用本地的多媒体模块来播放这个视频文件, 如何实现? 一、html中的JavaScript调用Qt本地对象方法 过程如下: 将本地的QObject对象暴露给webkit和JavaScript 通过JavaScript调用本地QObject的槽 下面对每个步骤进行详...