Skip to content

Commit 32b584c

Browse files
committed
feat: add options
1 parent 03bd51c commit 32b584c

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ module: {
1717
{
1818
test: /\.(htm|html)$/i,
1919
loader: 'html-src-loader',
20-
options: {} // todo
20+
options: {
21+
// 按需配置需要处理的标签
22+
img: ["src", "data-src"], // 每个标签都可以通过数组指定多个需要替换的属性
23+
link: ["href"],
24+
audio: ["src"],
25+
script: ["src"],
26+
video: ["src"]
27+
}
2128
},
2229
]
2330
},
@@ -28,6 +35,16 @@ plugins: [
2835
)
2936
],
3037
```
38+
默认`options`
39+
```js
40+
const defaultTagConfig = {
41+
img: ["src"],
42+
link: ["href"],
43+
audio: ["src"],
44+
script: ["src"],
45+
video: ["src"]
46+
}
47+
```
3148

3249
## 为什么需要自定义匹配规则
3350
该loader主要借鉴了[html-withimg-loader](https://github.com/wzsxyz/html-withimg-loader)的一些思想,但是`html-withimg-loader`缺少对于自定义匹配规则的配置,由于原作者貌似已经停止维护了,无法提交PR,因此手动实现一个版本。

demo/webpack.config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ module.exports = (env) => {
4242
{
4343
test: /\.(htm|html)$/i,
4444
loader: 'html-file-loader',
45-
options: {} // todo
45+
options: {
46+
img: ["src", "data-src"],
47+
link: ["href"],
48+
audio: ["src"],
49+
script: ["src"],
50+
video: ["src"]
51+
}
4652
},
4753
]
4854
},

src/loader.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
// const loaderUtils = require("loader-utils");
1+
const loaderUtils = require("loader-utils");
22

33
const replaceUtil = require('./replace')
4-
54
module.exports = function (source) {
65
this.cacheable();
7-
86
const callback = this.async();
97

10-
let content = replaceUtil.replace(source)
8+
let options = loaderUtils.getOptions(this)
9+
10+
let content = replaceUtil.replace(source, options)
1111

1212
callback(null, `module.exports = \`${content}\``);
1313
}

src/replace.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// todo 开放配置接口
2-
const tagsConfig = {
3-
img: ["src", "data-src"],
1+
const defaultTagConfig = {
2+
img: ["src"],
43
link: ["href"],
54
audio: ["src"],
65
script: ["src"],
@@ -13,7 +12,7 @@ function replaceAllSemicolon(content) {
1312
}
1413

1514
// 使用正则解析img、audio、script、link等标签的文件引用
16-
function replaceTag(content) {
15+
function replaceTag(content, tagsConfig) {
1716
// todo 增加其他标签的解析方式
1817
const re = /<(img|link|audio|script).*?\/?>/g
1918
return content.replace(re, (tag) => {
@@ -23,7 +22,7 @@ function replaceTag(content) {
2322
let attrs = tagsConfig[tagName]
2423
Array.isArray(attrs) && attrs.forEach(attr => {
2524
tag = tag.replace(new RegExp(`${attr}=(['"])(.*?)\\1`), function (match, $1, source) {
26-
// 不管$1捕获的是单引号还是双引号,都将属性替换为"双引号包围
25+
// 不管$1捕获的是单引号还是双引号,都将属性替换为"双引号包围
2726
return `${attr}="\` + require('${source}') + \`"`
2827
})
2928
})
@@ -33,8 +32,10 @@ function replaceTag(content) {
3332
}
3433

3534
module.exports = {
36-
replace(content) {
37-
return replaceTag(replaceAllSemicolon(content))
35+
replace(content, options) {
36+
37+
let config = Object.assign({}, defaultTagConfig, options)
38+
return replaceTag(replaceAllSemicolon(content), config)
3839
},
3940
replaceAllSemicolon,
4041
replaceTag

test/replace.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ describe('Replace', () => {
6363
expect(result).equal(`<img src="\` + require('./assets/placeholder.png') + \`" data-src="\` + require('./assets/1.png') + \`" alt="">`)
6464
})
6565

66+
// todo
67+
it('should not replace when resource start with http?', () => {
68+
const input = `<img src='http://shymean.com/assets/1.png' alt="">`
69+
70+
const result = replaceTag(input)
71+
72+
expect(result).equal(input)
73+
})
74+
75+
it('should not replace when resource start with https', () => {
76+
const input = `<img src='https://shymean.com/assets/1.png' alt="">`
77+
78+
const result = replaceTag(input)
79+
80+
expect(result).equal(input)
81+
})
82+
6683
})
6784
})
6885

0 commit comments

Comments
 (0)