Skip to content

Commit 597e4d1

Browse files
committed
Fix ESLint errors
1 parent ccce97b commit 597e4d1

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

plugin.jest.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ describe('CspHtmlWebpackPlugin', () => {
216216
undefined,
217217
{
218218
entry: {
219-
'1': path.join(__dirname, 'test-utils', 'fixtures', 'index-1.js'),
220-
'2': path.join(__dirname, 'test-utils', 'fixtures', 'index-2.js'),
219+
1: path.join(__dirname, 'test-utils', 'fixtures', 'index-1.js'),
220+
2: path.join(__dirname, 'test-utils', 'fixtures', 'index-2.js'),
221221
},
222222
module: {
223223
rules: [
@@ -579,14 +579,18 @@ describe('CspHtmlWebpackPlugin', () => {
579579

580580
scripts.each((i, script) => {
581581
if (!script.attribs.src.startsWith('http')) {
582-
expect(script.attribs.integrity).toEqual("sha256-IDmpTcnLo5Niek0rbHm9EEQtYiqYHApvDU+Rta9RdVU=");
582+
expect(script.attribs.integrity).toEqual(
583+
'sha256-IDmpTcnLo5Niek0rbHm9EEQtYiqYHApvDU+Rta9RdVU='
584+
);
583585
} else {
584586
expect(script.attribs.integrity).toBeUndefined();
585587
}
586-
})
588+
});
587589
styles.each((i, style) => {
588590
if (!style.attribs.href.startsWith('http')) {
589-
expect(style.attribs.integrity).toEqual("sha256-bFK7QzTObijstzDDaq2yN82QIYcoYx/EDD87NWCGiPw=");
591+
expect(style.attribs.integrity).toEqual(
592+
'sha256-bFK7QzTObijstzDDaq2yN82QIYcoYx/EDD87NWCGiPw='
593+
);
590594
} else {
591595
expect(style.attribs.integrity).toBeUndefined();
592596
}
@@ -630,7 +634,7 @@ describe('CspHtmlWebpackPlugin', () => {
630634

631635
scripts.each((i, script) => {
632636
expect(script.attribs.integrity).toBeUndefined();
633-
})
637+
});
634638
styles.each((i, style) => {
635639
expect(style.attribs.integrity).toBeUndefined();
636640
});

plugin.js

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ try {
2020
}
2121
}
2222

23+
/**
24+
* Remove the public path from a URL, if present
25+
* @param publicPath
26+
* @param {string} filePath
27+
* @returns {string}
28+
*/
29+
const getFilename = (publicPath, filePath) => {
30+
if (!publicPath || !filePath.startsWith(publicPath)) {
31+
return filePath;
32+
}
33+
return filePath.substr(publicPath.length);
34+
};
35+
2336
/**
2437
* The default function for adding the CSP to the head of a document
2538
* Can be overwritten to allow the developer to process the CSP in their own way
@@ -411,19 +424,6 @@ class CspHtmlWebpackPlugin {
411424
return compileCb(null, htmlPluginData);
412425
}
413426

414-
/**
415-
* Remove the public path from a URL, if present
416-
* @param publicPath
417-
* @param {string} path
418-
* @returns {string}
419-
*/
420-
getFilename(publicPath, path) {
421-
if (!publicPath || !path.startsWith(publicPath)) {
422-
return path;
423-
}
424-
return path.substr(publicPath.length);
425-
}
426-
427427
/**
428428
* Add integrity attributes to asset tags
429429
* @param compilation
@@ -432,20 +432,38 @@ class CspHtmlWebpackPlugin {
432432
*/
433433
addIntegrityAttributes(compilation, htmlPluginData, compileCb) {
434434
if (this.hashEnabled['script-src'] !== false) {
435-
htmlPluginData.assetTags.scripts.filter(tag => tag.attributes.src).forEach(tag => {
436-
const filename = this.getFilename(compilation.options.output.publicPath, tag.attributes.src);
437-
if (filename in compilation.assets) {
438-
tag.attributes.integrity = this.hashFile(compilation.assets, filename).slice(1, -1);
439-
}
440-
});
435+
htmlPluginData.assetTags.scripts
436+
.filter((tag) => tag.attributes.src)
437+
.forEach((tag) => {
438+
const filename = getFilename(
439+
compilation.options.output.publicPath,
440+
tag.attributes.src
441+
);
442+
if (filename in compilation.assets) {
443+
// eslint-disable-next-line no-param-reassign
444+
tag.attributes.integrity = this.hashFile(
445+
compilation.assets,
446+
filename
447+
).slice(1, -1);
448+
}
449+
});
441450
}
442451
if (this.hashEnabled['style-src'] !== false) {
443-
htmlPluginData.assetTags.styles.filter(tag => tag.attributes.href).forEach(tag => {
444-
const filename = this.getFilename(compilation.options.output.publicPath, tag.attributes.href);
445-
if (filename in compilation.assets) {
446-
tag.attributes.integrity = this.hashFile(compilation.assets, filename).slice(1, -1);
447-
}
448-
});
452+
htmlPluginData.assetTags.styles
453+
.filter((tag) => tag.attributes.href)
454+
.forEach((tag) => {
455+
const filename = getFilename(
456+
compilation.options.output.publicPath,
457+
tag.attributes.href
458+
);
459+
if (filename in compilation.assets) {
460+
// eslint-disable-next-line no-param-reassign
461+
tag.attributes.integrity = this.hashFile(
462+
compilation.assets,
463+
filename
464+
).slice(1, -1);
465+
}
466+
});
449467
}
450468
return compileCb(null, htmlPluginData);
451469
}
@@ -471,7 +489,7 @@ class CspHtmlWebpackPlugin {
471489
HtmlWebpackPlugin.getHooks(compilation).alterAssetTags.tapAsync(
472490
'CspHtmlWebpackPlugin',
473491
this.addIntegrityAttributes.bind(this, compilation)
474-
)
492+
);
475493
});
476494
}
477495
}

0 commit comments

Comments
 (0)