Skip to content

Commit fd6094b

Browse files
authored
If plugin is disabled, we should not modify the html at all (#28)
* If plugin is disabled, we should not modify the html at all * Updating readme to reflect new recommendation of not including a blank meta tag - it will be added for us
1 parent cf2984d commit fd6094b

File tree

3 files changed

+11
-21
lines changed

3 files changed

+11
-21
lines changed

README.md

-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ new HtmlWebpackPlugin()
2929
new CspHtmlWebpackPlugin()
3030
```
3131

32-
Finally, add the following tag to your HTML template where you would like to add the CSP policy:
33-
```
34-
<meta http-equiv="Content-Security-Policy" content="">
35-
```
36-
3732
## Configuration
3833

3934
This `CspHtmlWebpackPlugin` accepts 2 params with the following structure:

plugin.jest.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -508,15 +508,15 @@ describe('CspHtmlWebpackPlugin', () => {
508508
});
509509

510510
describe('Enabled check', () => {
511-
it('removes the empty Content Security Policy meta tag if enabled is the bool false', done => {
511+
it("doesn't modify the html if enabled is the bool false", done => {
512512
const config = createWebpackConfig([
513513
new HtmlWebpackPlugin({
514514
filename: path.join(WEBPACK_OUTPUT_DIR, 'index.html'),
515515
template: path.join(
516516
__dirname,
517517
'test-utils',
518518
'fixtures',
519-
'with-nothing.html'
519+
'with-no-meta-tag.html'
520520
)
521521
}),
522522
new CspHtmlWebpackPlugin(
@@ -534,15 +534,15 @@ describe('CspHtmlWebpackPlugin', () => {
534534
});
535535
});
536536

537-
it('removes the empty Content Security Policy meta tag if the `cspPlugin.disabled` option in HtmlWebpack Plugin is true', done => {
537+
it("doesn't modify the html if the `cspPlugin.enabled` option in HtmlWebpack Plugin is false", done => {
538538
const config = createWebpackConfig([
539539
new HtmlWebpackPlugin({
540540
filename: path.join(WEBPACK_OUTPUT_DIR, 'index.html'),
541541
template: path.join(
542542
__dirname,
543543
'test-utils',
544544
'fixtures',
545-
'with-nothing.html'
545+
'with-no-meta-tag.html'
546546
),
547547
cspPlugin: {
548548
enabled: false
@@ -558,15 +558,15 @@ describe('CspHtmlWebpackPlugin', () => {
558558
});
559559
});
560560

561-
it('removes the empty Content Security Policy meta tag if enabled is a function which return false', done => {
561+
it("doesn't modify the html if enabled is a function which return false", done => {
562562
const config = createWebpackConfig([
563563
new HtmlWebpackPlugin({
564564
filename: path.join(WEBPACK_OUTPUT_DIR, 'index.html'),
565565
template: path.join(
566566
__dirname,
567567
'test-utils',
568568
'fixtures',
569-
'with-nothing.html'
569+
'with-no-meta-tag.html'
570570
)
571571
}),
572572
new CspHtmlWebpackPlugin(
@@ -584,15 +584,15 @@ describe('CspHtmlWebpackPlugin', () => {
584584
});
585585
});
586586

587-
it('only removes the Content Security Policy meta tag from the HtmlWebpackPlugin instance which has been disabled', done => {
587+
it("doesn't modify html from the HtmlWebpackPlugin instance which has been disabled", done => {
588588
const config = createWebpackConfig([
589589
new HtmlWebpackPlugin({
590590
filename: path.join(WEBPACK_OUTPUT_DIR, 'index-enabled.html'),
591591
template: path.join(
592592
__dirname,
593593
'test-utils',
594594
'fixtures',
595-
'with-nothing.html'
595+
'with-no-meta-tag.html'
596596
)
597597
}),
598598
new HtmlWebpackPlugin({
@@ -601,7 +601,7 @@ describe('CspHtmlWebpackPlugin', () => {
601601
__dirname,
602602
'test-utils',
603603
'fixtures',
604-
'with-nothing.html'
604+
'with-no-meta-tag.html'
605605
),
606606
cspPlugin: {
607607
enabled: false

plugin.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -282,18 +282,13 @@ class CspHtmlWebpackPlugin {
282282
decodeEntities: false
283283
});
284284

285-
let metaTag = $('meta[http-equiv="Content-Security-Policy"]');
286-
287285
// if not enabled, remove the empty tag
288286
if (!this.isEnabled(htmlPluginData)) {
289-
metaTag.remove();
290-
291-
// eslint-disable-next-line no-param-reassign
292-
htmlPluginData.html = $.html();
293-
294287
return compileCb(null, htmlPluginData);
295288
}
296289

290+
let metaTag = $('meta[http-equiv="Content-Security-Policy"]');
291+
297292
// Add element if it doesn't exist.
298293
if (!metaTag.length) {
299294
metaTag = cheerio.load('<meta http-equiv="Content-Security-Policy">')(

0 commit comments

Comments
 (0)