WpwCspWebPlugin

new WpwCspWebPlugin(policy, additionalOpts)

Setup for our plugin

.

Setup for our plugin

Parameters:
NameTypeDescription
policyobject

the policy object - see defaultPolicy above for the structure

additionalOptsobject

additional config options - see defaultAdditionalOpts above for options available

Methods

apply(compiler)

Hooks into webpack to collect assets and hash them, build the policy, and add it into our HTML template

.

Hooks into webpack to collect assets and hash them, build the policy, and add it into our HTML template

Parameters:
NameTypeDescription
compilerWebpackCompiler

buildPolicy(policyObj) → {string}

Parameters:
NameTypeDescription
policyObjany
Returns:
Type: 
string

createNonce() → {string}

Create a random nonce which we will set onto our assets

.

Create a random nonce which we will set onto our assets

Returns:
Type: 
string

getShas(cheerio, policyName, selector) → {Array.<string>}

Parameters:
NameTypeDescription
cheerioobject

cheerio instance

policyNamestring

'script-src' | 'style-src'

selectorstring

cheerio selector string

Returns:
Type: 
Array.<string>

hash(str) → {string}

Parameters:
NameTypeDescription
strstring

the string to hash

Returns:
  • the returned hash with the hashing method prepended e.g. sha256-123456abcdef
Type: 
string

isEnabled(pluginData) → {boolean}

Checks to see whether the plugin is enabled.

Checks to see whether the plugin is enabled. this.opts.enabled can be a function or bool here

Parameters:
NameTypeDescription
pluginData

the pluginData from compilation

Returns:
  • whether the plugin is enabled or not
Type: 
boolean

mergeOptions(compilation, pluginData, compileCb)

Parameters:
NameTypeDescription
compilationobject
pluginDataobject

data from HtmlWebpackPlugin

compileCbfunction

callback to continue webpack compilation

processCsp(compilation, pluginData, compileCb)

Parameters:
NameTypeDescription
compilationWebpackCompilation
pluginDataany
compileCbany

setNonce(cheerio, policyName, selector) → {Array.<string>}

Parameters:
NameTypeDescription
cheerioobject

cheerio instance

policyNamestring

one of 'script-src' and 'style-src'

selectorstring

a Cheerio selector string for getting the hashable elements for this policy

Returns:
Type: 
Array.<string>

validatePolicy(compilation)

Validate the policy by making sure that all static sources have been wrapped in apostrophes
i.e.

Validate the policy by making sure that all static sources have been wrapped in apostrophes
i.e. policy should contain 'self' instead of self

Parameters:
NameTypeDescription
compilationobject

the webpack compilation object