html loader的作用,html-loader

1 Docs

将html文档以字符串形式导出,需要时,可以进行压缩

1 默认情况下,每个本地的 image.png 都需要通过 require (require('./image.png'))来进行加载。

2 你可能需要在配置中为图片指定 loader(推荐 file-loader 或 url-loader )

3 你可以通过查询参数 attrs,来指定哪个标签属性组合(tag-attribute combination)应该被此 loader 处理。

4 传递数组或以空格分隔的 : 组合的列表。(默认值:attrs=img:src)

{

test: /\.(html)$/,

use: {

loader: 'html-loader',

options: {

attrs: [':data-src']

}

}

}

要完全禁用对标签属性的处理(例如,如果你在客户端处理图片加载),你可以传入 attrs=false。

2 options

{

attributes // 类型:{Boolean|Object},默认值:true,描述:启用/禁用属性替换处理

minimize // 类型:{Function},默认值:undefined,描述:允许在html-loader处理前进行预处理

preprocessor // 类型:{Boolean|Object},默认值:生产环境true,其他环境false,描述:是否压缩html

esModule // 类型:{Boolean},默认值:false,描述:使用es模块化语法

}

attributes

{

test: /\.html$/i,

loader: 'html-loader',

options: {

// Disables attributes processing

attributes: false,

},

}

{

test: /\.html$/i,

loader: 'html-loader',

options: {

attributes: {

list: [

// All default supported tags and attributes

'...',

{

tag: 'img', // Tag name

attribute: 'data-src', // Attribute name

type: 'src', // Type of processing, can be `src` or `scrset`

},

{

tag: 'img',

attribute: 'data-srcset',

type: 'srcset',

},

],

urlFilter: (attribute, value, resourcePath) => {

// The `attribute` argument contains a name of the HTML attribute.

// The `value` argument contains a value of the HTML attribute.

// The `resourcePath` argument contains a path to the loaded HTML file.

if (/example\.pdf$/.test(value)) {

return false;

}

return true;

},

root: '.',

},

// Allow to filter some attributes

filter: (tag, attribute, attributes, resourcePath) => {

// The `tag` argument contains a name of the HTML tag.

// The `attribute` argument contains a name of the HTML attribute.

// The `attributes` argument contains all attributes of the tag.

// The `resourcePath` argument contains a path to the loaded HTML file.

if (/my-html\.html$/.test(resourcePath)) {

return false;

}

if (!/stylesheet/i.test(attributes.rel)) {

return false;

}

if (

attributes.type &&

attributes.type.trim().toLowerCase() !== 'text/css'

) {

return false;

}

return true;

}

},

}

minimize

{

test: /\.html$/i,

loader: 'html-loader',

options: {

minimize: true|false

},

}

{

test: /\.html$/i,

loader: 'html-loader',

options: {

minimize: {

caseSensitive: true,

collapseWhitespace: true,

conservativeCollapse: true,

keepClosingSlash: true,

minifyCSS: true,

minifyJS: true,

removeComments: true,

removeRedundantAttributes: true,

removeScriptTypeAttributes: true,

removeStyleLinkTypeAttributes: true,

}

},

}

preprocessor

esModule

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值