DefinitelyTyped/types/csp-html-webpack-plugin/csp-html-webpack-plugin-tests.ts
Seally f4d65d8c02 [csp-html-webpack-plugin] Add type definitions (#37544)
* [csp-html-webpack-plugin] Add type definitions.

* [csp-html-webpack-plugin] Fix test file formatting with Prettier.

* [csp-html-webpack-plugin] Move HtmlPluginData into exported namespace.

* [csp-html-webpack-plugin] Remove comment on HtmlPluginData.

* [csp-html-webpack-plugin] Use conditional types to figure out HtmlPluginData type.

* [csp-html-webpack-plugin] Remove 'esModuleInterop' from tsconfig.json.
2019-08-14 15:16:07 -07:00

88 lines
2.1 KiB
TypeScript
Executable File

import HtmlWebpackPlugin = require('html-webpack-plugin');
import CspHtmlWebpackPlugin = require('csp-html-webpack-plugin');
import { Configuration as WebpackConfiguration } from 'webpack';
// Should accept various CSP directive types.
const policy: CspHtmlWebpackPlugin.Policy = {
'base-uri': "'self'",
'object-src': "'none'",
'script-src': ["'unsafe-inline'", "'self'", "'unsafe-eval'"],
'style-src': ["'unsafe-inline'", "'self'", "'unsafe-eval'"],
'block-all-mixed-content': '',
'report-uri': 'http://reportcollector.example.com/collector.cgi',
};
// Should allow no parameters.
new CspHtmlWebpackPlugin();
// Should allow empty object parameters.
new CspHtmlWebpackPlugin({}, {});
// Should allow full parameters.
new CspHtmlWebpackPlugin(policy, {
enabled: true,
hashingMethod: 'sha256',
hashEnabled: {
'script-src': true,
'style-src': true,
},
nonceEnabled: {
'script-src': true,
'style-src': true,
},
});
const optsWithEnableFunc: CspHtmlWebpackPlugin.AdditionalOptions = {
enabled(htmlPluginData) {
// $ExpectType string
htmlPluginData.outputName;
// $ExpectType string
htmlPluginData.html;
if (htmlPluginData.plugin.apply) {
}
return true;
},
hashEnabled: {
'script-src': true,
'style-src': true,
},
nonceEnabled: {
'script-src': true,
'style-src': true,
},
};
// Can be added to Webpack configuration.
const webpackConfig: WebpackConfiguration = {
plugins: [new HtmlWebpackPlugin(), new CspHtmlWebpackPlugin()],
};
// HtmlWebpackPlugin augmentations should work
new HtmlWebpackPlugin({
// Original options should be present.
filename: 'my_index.html',
template: 'src/static/index.ejs',
// Added options should be present.
cspPlugin: {
enabled: true,
policy: {
'base-uri': "'self'",
'object-src': "'none'",
'script-src': ["'unsafe-inline'", "'self'", "'unsafe-eval'"],
'style-src': ["'unsafe-inline'", "'self'", "'unsafe-eval'"],
},
hashingMethod: 'sha384',
hashEnabled: {
'script-src': true,
'style-src': true,
},
nonceEnabled: {
'script-src': true,
'style-src': true,
},
},
});