Skip to content

Commit 6ac737c

Browse files
author
Zhicheng Wang
committed
feat: add option: ignoreFiles
It is useful for handling Angular templates correctly. see angular/angular-cli#3415
1 parent 4b13d4c commit 6ac737c

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,29 @@ require("html-loader?root=.!./file.html");
173173
// => '<img src="http://cdn.example.com/49eba9f/a992ca.jpg">'
174174
```
175175

176+
### Ignore specified image files
177+
178+
You can use `ignoreFiles` option to ignore some image files, like so:
179+
180+
```js
181+
module: {
182+
rules: [{
183+
test: /\.html$/,
184+
use: [ {
185+
loader: 'html-loader',
186+
options: {
187+
ignoreFiles: [
188+
/{{.*?}}/
189+
]
190+
}
191+
}],
192+
}]
193+
}
194+
```
195+
196+
`<img src="angular{{version}}.jpg"/>` should be remained as `<img src="angular{{version}}.jpg"/>`.
197+
You can also use a function or a string instead of the RegExp.
198+
176199
### Interpolation
177200

178201
You can use `interpolate` flag to enable interpolation syntax for ES6 template strings, like so:

index.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module.exports = function(content) {
5353
content = [content];
5454
links.forEach(function(link) {
5555
if(!loaderUtils.isUrlRequest(link.value, root)) return;
56-
56+
5757
if (link.value.indexOf('mailto:') > -1 ) return;
5858

5959
var uri = url.parse(link.value);
@@ -142,9 +142,32 @@ module.exports = function(content) {
142142
exportsString = "export default ";
143143
}
144144

145-
return exportsString + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
146-
if(!data[match]) return match;
147-
return '" + require(' + JSON.stringify(loaderUtils.urlToRequest(data[match], root)) + ') + "';
145+
function shouldIgnoreFile(fileName) {
146+
var rules = config.ignoreFiles || [];
147+
return rules.find(function(rule) {
148+
if (rule instanceof Function) {
149+
return rule(fileName);
150+
}
151+
152+
if (typeof rule === 'string') {
153+
rule = new RegExp(rule);
154+
}
155+
156+
if (rule instanceof RegExp) {
157+
return rule.test(fileName);
158+
}
159+
160+
throw "html-loader: the `ignoreRules` option should be an array of strings/RegExps/functions";
161+
});
162+
}
163+
164+
return exportsString + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function (match) {
165+
var fileName = data[match];
166+
if (!fileName) return match;
167+
if (shouldIgnoreFile(fileName)) {
168+
return fileName;
169+
}
170+
return '" + require(' + JSON.stringify(loaderUtils.urlToRequest(fileName, root)) + ') + "';
148171
}) + ";";
149172

150173
}

test/loaderTest.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,53 @@ describe("loader", function() {
174174
'module.exports = "<a href=\\"${list.href}\\"><img src=\\"" + require("./test.jpg") + "\\" /></a>";'
175175
);
176176
});
177+
it("should ignore files specified by regexp", function() {
178+
loader.call({
179+
options: {
180+
htmlLoader: {
181+
ignoreFiles: [/{{.*?}}/]
182+
}
183+
}
184+
}, '<img src="angular{{version}}.jpg"/>').should.be.eql('module.exports = "<img src=\\"angular{{version}}.jpg\\"/>";');
185+
});
186+
187+
it("should ignore files specified by regexp(string)", function() {
188+
loader.call({
189+
options: {
190+
htmlLoader: {
191+
ignoreFiles: ["{{.*?}}"]
192+
}
193+
}
194+
}, '<img src="angular{{version}}.jpg"/>').should.be.eql('module.exports = "<img src=\\"angular{{version}}.jpg\\"/>";');
195+
});
196+
197+
it("should ignore files specified by function", function() {
198+
loader.call({
199+
options: {
200+
htmlLoader: {
201+
ignoreFiles: [function(fileName) {
202+
return /{{.*?}}/.test(fileName);
203+
}]
204+
}
205+
}
206+
}, '<img src="angular{{version}}.jpg"/>').should.be.eql('module.exports = "<img src=\\"angular{{version}}.jpg\\"/>";');
207+
});
208+
209+
it("should ignore files specified by multiple filters", function() {
210+
loader.call({
211+
options: {
212+
htmlLoader: {
213+
ignoreFiles: [
214+
function(fileName) {
215+
return /{{.*?}}/.test(fileName);
216+
},
217+
/no-hash/,
218+
]
219+
}
220+
}
221+
}, '<img src="angular{{version}}.jpg"/><img src="no-hash.png"/>').should.be.eql('module.exports = "<img src=\\"angular{{version}}.jpg\\"/><img src=\\"no-hash.png\\"/>";');
222+
});
223+
177224
it("should export as default export for es6to5 transpilation", function() {
178225
loader.call({
179226
query: "?exportAsDefault"

0 commit comments

Comments
 (0)