Skip to content

Commit b0adaea

Browse files
sibiraj-sAnujRNair
authored andcommitted
feat: disable the plugin from HTMLWebpackPlugin (#21)
* feat: disable the plugin from HTMLWebpackPlugin * docs: update README.md
1 parent 5d642ce commit b0adaea

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ This `CspHtmlWebpackPlugin` accepts 2 params with the following structure:
4242
* `{string}` hashingMethod - accepts 'sha256', 'sha384', 'sha512' - your node version must also accept this hashing method.
4343
* `{boolean|Function}` enabled - if false, or the function returns false, the empty CSP tag will be stripped from the html output. The `htmlPluginData` is passed into the function as it's first param.
4444

45+
_Note: CSP usually runs on all files processed from HTML Webpack plugin, to disable it for a particular instance, set `disableCspPlugin` to `true(boolean)` in [HTML Webpack Plugins Options](https://github.com/jantimon/html-webpack-plugin#options)_
46+
4547
#### Default Policy:
4648

4749
```

plugin.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const uniq = require('lodash/uniq');
44
const compact = require('lodash/compact');
55
const flatten = require('lodash/flatten');
66
const isFunction = require('lodash/isFunction');
7+
const get = require('lodash/get');
78

89
const defaultPolicy = {
910
'base-uri': "'self'",
@@ -45,6 +46,15 @@ class CspHtmlWebpackPlugin {
4546
* @return {boolean} - whether the plugin is enabled or not
4647
*/
4748
isEnabled(htmlPluginData) {
49+
const disableCspPlugin = get(
50+
htmlPluginData,
51+
'plugin.options.disableCspPlugin'
52+
);
53+
54+
if (disableCspPlugin && disableCspPlugin === true) {
55+
return false;
56+
}
57+
4858
if (isFunction(this.opts.enabled)) {
4959
return this.opts.enabled(htmlPluginData);
5060
}

spec/plugin.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,37 @@ describe('CspHtmlWebpackPlugin', () => {
300300
);
301301
});
302302

303+
it('removes the empty Content Security Policy meta tag by setting `disableCspPlugin` in HTMLWebpack Plugin`s Options', done => {
304+
const webpackConfig = {
305+
entry: path.join(__dirname, 'fixtures/index.js'),
306+
output: {
307+
path: OUTPUT_DIR,
308+
filename: 'index.bundle.js'
309+
},
310+
mode: 'none',
311+
plugins: [
312+
new HtmlWebpackPlugin({
313+
filename: path.join(OUTPUT_DIR, 'index.html'),
314+
template: path.join(__dirname, 'fixtures', 'with-nothing.html'),
315+
inject: 'body',
316+
disableCspPlugin: true
317+
}),
318+
new CspHtmlWebpackPlugin()
319+
]
320+
};
321+
322+
testCspHtmlWebpackPlugin(
323+
webpackConfig,
324+
'index.html',
325+
(cspPolicy, $, doneFn) => {
326+
expect(cspPolicy).toBeUndefined();
327+
expect($('meta').length).toEqual(1);
328+
doneFn();
329+
},
330+
done
331+
);
332+
});
333+
303334
it('removes the empty Content Security Policy meta tag if enabled is a function which return false', done => {
304335
const webpackConfig = {
305336
entry: path.join(__dirname, 'fixtures/index.js'),

0 commit comments

Comments
 (0)