Skip to content

Commit

Permalink
fix: correct attributes type, #35
Browse files Browse the repository at this point in the history
  • Loading branch information
webdiscus committed Oct 12, 2023
1 parent f9c53f2 commit 52d96fb
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change log

## 2.14.4 (2023-10-12)

- fix: correct attributes type in the `filter()` function of the `sources` loader option
- refactor: improve code
- docs: fix attributes type

## 2.14.3 (2023-10-08)

- chore: update dependencies in examples
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ type Preload = Array<{
as?: string;
rel?: string;
type?: string;
attributes?: {};
attributes?: { [k: string]: string | boolean };
}>;
```
Expand Down Expand Up @@ -1375,7 +1375,7 @@ preload: [
],
```
The generated preload tag like following:
The generated preload tag like the following:
```html
<link rel="preload" href="css/styles.1f4faaff.css" as="style" />
Expand All @@ -1392,7 +1392,7 @@ preload: [
],
```
The generated preload tag like following:
The generated preload tag like the following:
```html
<link rel="preload" href="js/main.c608b1cd.js" as="script" />
Expand All @@ -1411,7 +1411,7 @@ preload: [
],
```
The generated preload tags like following:
The generated preload tags like the following:
```html
<link rel="preload" href="img/apple.697ef306.png" as="image" type="image/png" />
Expand Down Expand Up @@ -2003,7 +2003,7 @@ type Sources =
tag: string;
attribute: string;
value: string;
attributes: string;
attributes: { [k: string]: string };
resourcePath: string;
}) => boolean | undefined;
}>;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html-bundler-webpack-plugin",
"version": "2.14.3",
"version": "2.14.4",
"description": "HTML bundler plugin for webpack handles a template as an entry point, extracts CSS and JS from their sources referenced in HTML, supports template engines like Eta, EJS, Handlebars, Nunjucks.",
"keywords": [
"html",
Expand Down
32 changes: 16 additions & 16 deletions src/Common/HtmlParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class HtmlParser {
let endPos = 0;

while ((startPos = content.indexOf(open, startPos)) >= 0) {
let attrValues = [];
const parsedAttrs = [];
endPos = indexOfChar(close, content, startPos, '<');

if (endPos < 1) {
Expand All @@ -127,7 +127,7 @@ class HtmlParser {

const source = content.slice(startPos, endPos);
let type = 'asset';
let attrsAll = null;
let attrs = null;

if (tag === 'script') {
type = 'script';
Expand All @@ -137,42 +137,42 @@ class HtmlParser {
}

for (let attribute of attributes) {
const attrValue = this.parseAttr(source, attribute, type, startPos);
if (attrValue === false) continue;
const parsedAttr = this.parseAttr(source, attribute, type, startPos);
if (parsedAttr === false) continue;

let res = true;

if (filter) {
let value = attrValue.value;
let value = parsedAttr.value;

// when is the filter defined, parse all attributes once
if (!attrsAll) {
attrsAll = this.parseAttrAll(source);
if (!attrs) {
attrs = this.parseAttrAll(source);
}

res = filter({ tag, attribute, value, attributes: attrsAll, resourcePath }) !== false;
res = filter({ tag, attribute, value, attributes: attrs, resourcePath }) !== false;
}

if (res === true) {
if (attrValue.attrs) {
attrValues.push(...attrValue.attrs);
if (parsedAttr.attrs) {
parsedAttrs.push(...parsedAttr.attrs);
} else {
attrValues.push(attrValue);
parsedAttrs.push(parsedAttr);
}
}
}

// sort attributes by pos
if (attrValues.length > 1) {
attrValues.sort(comparePos);
if (parsedAttrs.length > 1) {
parsedAttrs.sort(comparePos);
}

result.push({
tag,
source,
type,
attrs: attrValues,
attrsAll,
parsedAttrs,
attrs,
startPos,
endPos,
});
Expand All @@ -187,7 +187,7 @@ class HtmlParser {
* Parse all attributes in the tag.
*
* @param {string} content The HTML tag.
* @return {{}}
* @return {{ [k: string]: string }}
*/
static parseAttrAll(content) {
let attrs = {};
Expand Down
4 changes: 2 additions & 2 deletions src/Loader/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class Template {
let output = '';
let pos = 0;

for (let { type, attrs } of parsedTags) {
for (let { startPos, endPos, value: file, offset } of attrs) {
for (let { type, parsedAttrs } of parsedTags) {
for (let { startPos, endPos, value: file, offset } of parsedAttrs) {
const resolvedFile = this.resolve({ isBasedir, type, file, issuer, entryId });
// skip not resolvable value, e.g. URL
if (!resolvedFile) continue;
Expand Down
14 changes: 7 additions & 7 deletions src/Plugin/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,19 +913,19 @@ class Collection {
let pos = 0;
let output = '';

for (const { tag, attrs, attrsAll, startPos, endPos } of parsedResults) {
if (!attrsAll || attrs.length < 1) continue;
for (const { tag, parsedAttrs, attrs, startPos, endPos } of parsedResults) {
if (!attrs || parsedAttrs.length < 1) continue;

const assetFile = attrsAll.href || attrsAll.src;
const assetFile = attrs.href || attrs.src;
const integrity = assetIntegrity.get(assetFile);

if (integrity) {
attrsAll.integrity = integrity;
attrsAll.crossorigin = Options.webpackOptions.output.crossOriginLoading || 'anonymous';
attrs.integrity = integrity;
attrs.crossorigin = Options.webpackOptions.output.crossOriginLoading || 'anonymous';

let attrsStr = '';
for (const attrName in attrsAll) {
attrsStr += ` ${attrName}="${attrsAll[attrName]}"`;
for (const attrName in attrs) {
attrsStr += ` ${attrName}="${attrs[attrName]}"`;
}

output += content.slice(pos, startPos) + `<${tag}${attrsStr}>`;
Expand Down
8 changes: 4 additions & 4 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ describe('parse tags unit tests', () => {
type: 'asset',
startPos: 0,
endPos: 31,
attrs: [
parsedAttrs: [
{
type: 'asset',
attr: 'src',
Expand All @@ -278,7 +278,7 @@ describe('parse tags unit tests', () => {
offset: 0,
},
],
attrsAll: null,
attrs: null,
},
];
return expect(received).toEqual(expected);
Expand All @@ -296,7 +296,7 @@ describe('parse tags unit tests', () => {
type: 'asset',
startPos: 5,
endPos: 58,
attrs: [
parsedAttrs: [
{
type: 'asset',
attr: 'src',
Expand All @@ -322,7 +322,7 @@ describe('parse tags unit tests', () => {
offset: 5,
},
],
attrsAll: null,
attrs: null,
},
];
return expect(received).toEqual(expected);
Expand Down
4 changes: 2 additions & 2 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ type Preload = Array<{
as?: string;
rel?: string;
type?: string;
attributes?: {};
attributes?: { [k: string]: string | boolean };
}>;

type Sources =
Expand All @@ -212,7 +212,7 @@ type Sources =
tag: string;
attribute: string;
value: string;
attributes: string;
attributes: { [k: string]: string };
resourcePath: string;
}) => boolean | undefined;
}>;
Expand Down

0 comments on commit 52d96fb

Please sign in to comment.