Skip to content

Commit 2482b07

Browse files
refactor: rename the "attributes" option to "sources" (#345)
BREAKING CHANGE: rename the "attributes" option to "sources"
1 parent f0bfe2a commit 2482b07

File tree

10 files changed

+136
-136
lines changed

10 files changed

+136
-136
lines changed

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ module.exports = {
5252

5353
| Name | Type | Default | Description |
5454
| :---------------------------------: | :-----------------: | :------------------------------------------: | :----------------------------------------------- |
55-
| **[`attributes`](#attributes)** | `{Boolean\|Object}` | `true` | Enables/Disables attributes handling |
55+
| **[`sources`](#sources)** | `{Boolean\|Object}` | `true` | Enables/Disables sources handling |
5656
| **[`preprocessor`](#preprocessor)** | `{Function}` | `undefined` | Allows pre-processing of content before handling |
5757
| **[`minimize`](#minimize)** | `{Boolean\|Object}` | `true` in production mode, otherwise `false` | Tell `html-loader` to minimize HTML |
5858
| **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Enable/disable ES modules syntax |
5959

60-
### `attributes`
60+
### `sources`
6161

6262
Type: `Boolean|Object`
6363
Default: `true`
@@ -103,7 +103,7 @@ module.exports = {
103103
loader: 'html-loader',
104104
options: {
105105
// Disables attributes processing
106-
attributes: false,
106+
sources: false,
107107
},
108108
},
109109
],
@@ -127,7 +127,7 @@ module.exports = {
127127
test: /\.html$/i,
128128
loader: 'html-loader',
129129
options: {
130-
attributes: {
130+
sources: {
131131
list: [
132132
// All default supported tags and attributes
133133
'...',
@@ -164,11 +164,11 @@ module.exports = {
164164
#### `list`
165165

166166
Type: `Array`
167-
Default: [supported tags and attributes](#attributes).
167+
Default: [supported tags and attributes](#sources).
168168

169169
Allows to setup which tags and attributes to process and how, and the ability to filter some of them.
170170

171-
Using `...` syntax allows you to extend [default supported tags and attributes](#attributes).
171+
Using `...` syntax allows you to extend [default supported tags and attributes](#sources).
172172

173173
For example:
174174

@@ -182,7 +182,7 @@ module.exports = {
182182
test: /\.html$/i,
183183
loader: 'html-loader',
184184
options: {
185-
attributes: {
185+
sources: {
186186
list: [
187187
// All default supported tags and attributes
188188
'...',
@@ -253,7 +253,7 @@ module.exports = {
253253
test: /\.html$/i,
254254
loader: 'html-loader',
255255
options: {
256-
attributes: {
256+
sources: {
257257
list: [
258258
{
259259
// Attribute name
@@ -290,7 +290,7 @@ module.exports = {
290290
test: /\.html$/i,
291291
loader: 'html-loader',
292292
options: {
293-
attributes: {
293+
sources: {
294294
list: [
295295
{
296296
tag: 'meta',
@@ -331,7 +331,7 @@ module.exports = {
331331
test: /\.html$/i,
332332
loader: 'html-loader',
333333
options: {
334-
attributes: {
334+
sources: {
335335
urlFilter: (attribute, value, resourcePath) => {
336336
// The `attribute` argument contains a name of the HTML attribute.
337337
// The `value` argument contains a value of the HTML attribute.
@@ -621,24 +621,24 @@ require('html-loader!./file.html');
621621
```
622622

623623
```js
624-
require('html-loader?{"attributes":{"list":[{"tag":"img","attribute":"data-src","type":"src"}]}}!./file.html');
624+
require('html-loader?{"sources":{"list":[{"tag":"img","attribute":"data-src","type":"src"}]}}!./file.html');
625625

626626
// => '<img src="image.jpg" data-src="data:image/png;base64,..." >'
627627
```
628628

629629
```js
630-
require('html-loader?{"attributes":{"list":[{"tag":"img","attribute":"src","type":"src"},{"tag":"img","attribute":"data-src","type":"src"}]}}!./file.html');
630+
require('html-loader?{"sources":{"list":[{"tag":"img","attribute":"src","type":"src"},{"tag":"img","attribute":"data-src","type":"src"}]}}!./file.html');
631631

632632
// => '<img src="http://cdn.example.com/49eba9f/a992ca.jpg" data-src="data:image/png;base64,..." >'
633633
```
634634

635635
```js
636-
require('html-loader?-attributes!./file.html');
636+
require('html-loader?-sources!./file.html');
637637

638638
// => '<img src="image.jpg" data-src="image2x.png" >'
639639
```
640640

641-
> :warning: `-attributes` sets `attributes: false`.
641+
> :warning: `-sources` sets `sources: false`.
642642
643643
### Process `script` and `link` tags
644644

src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sourcePlugin, minimizerPlugin } from './plugins';
1+
import { sourcesPlugin, minimizerPlugin } from './plugins';
22
import {
33
pluginRunner,
44
normalizeOptions,
@@ -23,10 +23,10 @@ export default async function loader(content) {
2323
const imports = [];
2424
const replacements = [];
2525

26-
if (options.attributes) {
26+
if (options.sources) {
2727
plugins.push(
28-
sourcePlugin({
29-
attributes: options.attributes,
28+
sourcesPlugin({
29+
sources: options.sources,
3030
resourcePath: this.resourcePath,
3131
context: this.context,
3232
imports,

src/options.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"title": "HTML Loader options",
33
"type": "object",
44
"definitions": {
5-
"Attribute": {
5+
"Source": {
66
"anyOf": [
77
{
88
"type": "object",
@@ -30,10 +30,10 @@
3030
}
3131
]
3232
},
33-
"AttributeList": {
33+
"SourcesList": {
3434
"type": "array",
3535
"items": {
36-
"$ref": "#/definitions/Attribute"
36+
"$ref": "#/definitions/Source"
3737
},
3838
"minItems": 1,
3939
"uniqueItems": true
@@ -43,14 +43,14 @@
4343
"preprocessor": {
4444
"instanceof": "Function"
4545
},
46-
"attributes": {
46+
"sources": {
4747
"anyOf": [
4848
{ "type": "boolean" },
4949
{
5050
"type": "object",
5151
"properties": {
5252
"list": {
53-
"$ref": "#/definitions/AttributeList"
53+
"$ref": "#/definitions/SourcesList"
5454
},
5555
"urlFilter": {
5656
"instanceof": "Function"

src/plugins/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import sourcePlugin from './source-plugin';
1+
import sourcesPlugin from './sources-plugin';
22
import minimizerPlugin from './minimizer-plugin';
33

4-
export { sourcePlugin, minimizerPlugin };
4+
export { sourcesPlugin, minimizerPlugin };

src/plugins/source-plugin.js renamed to src/plugins/sources-plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414

1515
export default (options) =>
1616
function process(html) {
17-
const { list, urlFilter: maybeUrlFilter } = options.attributes;
17+
const { list, urlFilter: maybeUrlFilter } = options.sources;
1818
const sources = [];
1919
const urlFilter = getFilter(maybeUrlFilter, (value) =>
2020
isUrlRequestable(value)

src/utils.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -754,31 +754,31 @@ function smartMergeSources(array, factory) {
754754
return newArray;
755755
}
756756

757-
function getAttributesOption(rawOptions) {
758-
if (typeof rawOptions.attributes === 'undefined') {
757+
function getSourcesOption(rawOptions) {
758+
if (typeof rawOptions.sources === 'undefined') {
759759
return { list: defaultAttributes };
760760
}
761761

762-
if (typeof rawOptions.attributes === 'boolean') {
763-
return rawOptions.attributes === true ? { list: defaultAttributes } : false;
762+
if (typeof rawOptions.sources === 'boolean') {
763+
return rawOptions.sources === true ? { list: defaultAttributes } : false;
764764
}
765765

766766
const sources = smartMergeSources(
767-
rawOptions.attributes.list,
767+
rawOptions.sources.list,
768768
() => defaultAttributes
769769
);
770770

771771
return {
772772
list: sources,
773-
urlFilter: rawOptions.attributes.urlFilter,
774-
root: rawOptions.attributes.root,
773+
urlFilter: rawOptions.sources.urlFilter,
774+
root: rawOptions.sources.root,
775775
};
776776
}
777777

778778
export function normalizeOptions(rawOptions, loaderContext) {
779779
return {
780780
preprocessor: rawOptions.preprocessor,
781-
attributes: getAttributesOption(rawOptions),
781+
sources: getSourcesOption(rawOptions),
782782
minimize: getMinimizeOption(rawOptions, loaderContext),
783783
esModule:
784784
typeof rawOptions.esModule === 'undefined' ? true : rawOptions.esModule,

test/__snapshots__/attributes-option.test.js.snap renamed to test/__snapshots__/sources-option.test.js.snap

Lines changed: 52 additions & 52 deletions
Large diffs are not rendered by default.
Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,111 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`validate options should throw an error on the "attributes" option with "[]" value 1`] = `
3+
exports[`validate options should throw an error on the "esModule" option with "true" value 1`] = `
4+
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
5+
- options.esModule should be a boolean."
6+
`;
7+
8+
exports[`validate options should throw an error on the "sources" option with "[]" value 1`] = `
49
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
5-
- options.attributes should be one of these:
10+
- options.sources should be one of these:
611
boolean | object { list?, urlFilter? }
712
Details:
8-
* options.attributes should be a boolean.
9-
* options.attributes should be an object:
13+
* options.sources should be a boolean.
14+
* options.sources should be an object:
1015
object { list?, urlFilter? }"
1116
`;
1217
13-
exports[`validate options should throw an error on the "attributes" option with "{"list":[]}" value 1`] = `
18+
exports[`validate options should throw an error on the "sources" option with "{"list":[]}" value 1`] = `
1419
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
15-
- options.attributes.list should be an non-empty array."
20+
- options.sources.list should be an non-empty array."
1621
`;
1722
18-
exports[`validate options should throw an error on the "attributes" option with "{"list":[{"tag":"","attribute":"src","type":"src"}]}" value 1`] = `
23+
exports[`validate options should throw an error on the "sources" option with "{"list":[{"tag":"","attribute":"src","type":"src"}]}" value 1`] = `
1924
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
20-
- options.attributes.list[0].tag should be an non-empty string."
25+
- options.sources.list[0].tag should be an non-empty string."
2126
`;
2227
23-
exports[`validate options should throw an error on the "attributes" option with "{"list":[{"tag":"img","attribute":"src","type":"src","filter":"test"}]}" value 1`] = `
28+
exports[`validate options should throw an error on the "sources" option with "{"list":[{"tag":"img","attribute":"src","type":"src","filter":"test"}]}" value 1`] = `
2429
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
25-
- options.attributes.list[0].filter should be an instance of function."
30+
- options.sources.list[0].filter should be an instance of function."
2631
`;
2732
28-
exports[`validate options should throw an error on the "attributes" option with "{"list":[{"tag":"img","attribute":"src","type":"src"},{"tag":"img","attribute":"src","type":"src"}]}" value 1`] = `
33+
exports[`validate options should throw an error on the "sources" option with "{"list":[{"tag":"img","attribute":"src","type":"src"},{"tag":"img","attribute":"src","type":"src"}]}" value 1`] = `
2934
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
30-
- options.attributes.list should not contain the item '[object Object]' twice."
35+
- options.sources.list should not contain the item '[object Object]' twice."
3136
`;
3237
33-
exports[`validate options should throw an error on the "attributes" option with "{"list":[{"tag":"img","attribute":"src"}]}" value 1`] = `
38+
exports[`validate options should throw an error on the "sources" option with "{"list":[{"tag":"img","attribute":"src"}]}" value 1`] = `
3439
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
35-
- options.attributes.list[0] misses the property 'type'. Should be:
40+
- options.sources.list[0] misses the property 'type'. Should be:
3641
\\"src\\" | \\"srcset\\""
3742
`;
3843
39-
exports[`validate options should throw an error on the "attributes" option with "{"unknown":true}" value 1`] = `
44+
exports[`validate options should throw an error on the "sources" option with "{"unknown":true}" value 1`] = `
4045
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
41-
- options.attributes has an unknown property 'unknown'. These properties are valid:
46+
- options.sources has an unknown property 'unknown'. These properties are valid:
4247
object { list?, urlFilter? }"
4348
`;
4449
45-
exports[`validate options should throw an error on the "attributes" option with "{"urlFilter":false}" value 1`] = `
50+
exports[`validate options should throw an error on the "sources" option with "{"urlFilter":false}" value 1`] = `
4651
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
47-
- options.attributes.urlFilter should be an instance of function."
52+
- options.sources.urlFilter should be an instance of function."
4853
`;
4954
50-
exports[`validate options should throw an error on the "attributes" option with "true" value 1`] = `
55+
exports[`validate options should throw an error on the "sources" option with "true" value 1`] = `
5156
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
52-
- options.attributes should be one of these:
57+
- options.sources should be one of these:
5358
boolean | object { list?, urlFilter? }
5459
Details:
55-
* options.attributes should be a boolean.
56-
* options.attributes should be an object:
60+
* options.sources should be a boolean.
61+
* options.sources should be an object:
5762
object { list?, urlFilter? }"
5863
`;
5964
60-
exports[`validate options should throw an error on the "esModule" option with "true" value 1`] = `
61-
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
62-
- options.esModule should be a boolean."
63-
`;
64-
6565
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
6666
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
6767
- options has an unknown property 'unknown'. These properties are valid:
68-
object { preprocessor?, attributes?, minimize?, esModule? }"
68+
object { preprocessor?, sources?, minimize?, esModule? }"
6969
`;
7070
7171
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
7272
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
7373
- options has an unknown property 'unknown'. These properties are valid:
74-
object { preprocessor?, attributes?, minimize?, esModule? }"
74+
object { preprocessor?, sources?, minimize?, esModule? }"
7575
`;
7676
7777
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
7878
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
7979
- options has an unknown property 'unknown'. These properties are valid:
80-
object { preprocessor?, attributes?, minimize?, esModule? }"
80+
object { preprocessor?, sources?, minimize?, esModule? }"
8181
`;
8282
8383
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
8484
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
8585
- options has an unknown property 'unknown'. These properties are valid:
86-
object { preprocessor?, attributes?, minimize?, esModule? }"
86+
object { preprocessor?, sources?, minimize?, esModule? }"
8787
`;
8888
8989
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
9090
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
9191
- options has an unknown property 'unknown'. These properties are valid:
92-
object { preprocessor?, attributes?, minimize?, esModule? }"
92+
object { preprocessor?, sources?, minimize?, esModule? }"
9393
`;
9494
9595
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
9696
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
9797
- options has an unknown property 'unknown'. These properties are valid:
98-
object { preprocessor?, attributes?, minimize?, esModule? }"
98+
object { preprocessor?, sources?, minimize?, esModule? }"
9999
`;
100100
101101
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
102102
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
103103
- options has an unknown property 'unknown'. These properties are valid:
104-
object { preprocessor?, attributes?, minimize?, esModule? }"
104+
object { preprocessor?, sources?, minimize?, esModule? }"
105105
`;
106106
107107
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
108108
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
109109
- options has an unknown property 'unknown'. These properties are valid:
110-
object { preprocessor?, attributes?, minimize?, esModule? }"
110+
object { preprocessor?, sources?, minimize?, esModule? }"
111111
`;

0 commit comments

Comments
 (0)