Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 63 additions & 5 deletions types/csp-html-webpack-plugin/csp-html-webpack-plugin-tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import HtmlWebpackPlugin = require("html-webpack-plugin");
import CspHtmlWebpackPlugin = require("csp-html-webpack-plugin");
import CspHtmlWebpackPlugin from "csp-html-webpack-plugin";
import HtmlWebpackPlugin from "html-webpack-plugin";

import { Configuration as WebpackConfiguration } from "webpack";
import { Compiler, Configuration as WebpackConfiguration } from "webpack";

// Should accept various CSP directive types.
const policy: CspHtmlWebpackPlugin.Policy = {
Expand Down Expand Up @@ -31,6 +31,25 @@ new CspHtmlWebpackPlugin(policy, {
"script-src": true,
"style-src": true,
},
processFn(builtPolicy, htmlPluginData, $, compilation) {
// $ExpectType string
builtPolicy;
// $ExpectType string
htmlPluginData.html;
// $ExpectType string
htmlPluginData.outputName;
htmlPluginData.plugin satisfies HtmlWebpackPlugin;
// $ExpectType CheerioAPI
$;
// $ExpectType { (this: CheerioAPI, options?: CheerioOptions | undefined): string; (this: CheerioAPI, dom?: BasicAcceptedElems<AnyNode> | undefined, options?: CheerioOptions | undefined): string; }
$.html;
// $ExpectType (this: CheerioAPI, dom?: BasicAcceptedElems<AnyNode> | undefined) => string
$.xml;
// $ExpectType Compilation
compilation;
// $ExpectType any[]
compilation.errors;
},
});

const optsWithEnableFunc: CspHtmlWebpackPlugin.AdditionalOptions = {
Expand All @@ -40,8 +59,8 @@ const optsWithEnableFunc: CspHtmlWebpackPlugin.AdditionalOptions = {
// $ExpectType string
htmlPluginData.html;

if (htmlPluginData.plugin.apply as any) {
}
// $ExpectType (compiler: Compiler) => void
htmlPluginData.plugin.apply;

return true;
},
Expand All @@ -53,6 +72,25 @@ const optsWithEnableFunc: CspHtmlWebpackPlugin.AdditionalOptions = {
"script-src": true,
"style-src": true,
},
processFn(builtPolicy, htmlPluginData, $, compilation) {
// $ExpectType string
builtPolicy;
// $ExpectType string
htmlPluginData.html;
// $ExpectType string
htmlPluginData.outputName;
htmlPluginData.plugin satisfies HtmlWebpackPlugin;
// $ExpectType CheerioAPI
$;
// $ExpectType { (this: CheerioAPI, options?: CheerioOptions | undefined): string; (this: CheerioAPI, dom?: BasicAcceptedElems<AnyNode> | undefined, options?: CheerioOptions | undefined): string; }
$.html;
// $ExpectType (this: CheerioAPI, dom?: BasicAcceptedElems<AnyNode> | undefined) => string
$.xml;
// $ExpectType Compilation
compilation;
// $ExpectType any[]
compilation.errors;
},
};

// Can be added to Webpack configuration.
Expand Down Expand Up @@ -83,5 +121,25 @@ new HtmlWebpackPlugin({
"script-src": true,
"style-src": true,
},
processFn(builtPolicy, htmlPluginData, $, compilation) {
// $ExpectType string
builtPolicy;
// $ExpectType string
htmlPluginData.html;
// $ExpectType string
htmlPluginData.outputName;
htmlPluginData.plugin satisfies HtmlWebpackPlugin;
// $ExpectType CheerioAPI
$;
// $ExpectType { (this: CheerioAPI, options?: CheerioOptions | undefined): string; (this: CheerioAPI, dom?: BasicAcceptedElems<AnyNode> | undefined, options?: CheerioOptions | undefined): string; }
$.html;
// $ExpectType (this: CheerioAPI, dom?: BasicAcceptedElems<AnyNode> | undefined) => string
$.xml;
// $ExpectType Compilation
compilation;
// $ExpectType any[]
compilation.errors;
},
},
xhtml: false,
});
32 changes: 19 additions & 13 deletions types/csp-html-webpack-plugin/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { CheerioAPI } from "cheerio";
import { AsyncSeriesWaterfallHook } from "tapable";
import { Compiler as WebpackCompiler } from "webpack";
import { compilation, Compiler as WebpackCompiler } from "webpack";
import HtmlWebpackPlugin = require("html-webpack-plugin");

export = CspHtmlWebpackPlugin;

declare class CspHtmlWebpackPlugin {
/**
* Setup for our plugin
* @param policy - the policy object
* @param additionalOpts - additional config options
* @param policy a flat object which defines your CSP policy. Valid keys and values can be found on the [MDN CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy) page. Values can either be a string, or an array of strings.
* @param additionalOpts a flat object with the optional configuration options
*/
constructor(
policy?: CspHtmlWebpackPlugin.Policy,
Expand Down Expand Up @@ -38,10 +39,7 @@ declare namespace CspHtmlWebpackPlugin {
[directive: string]: string | string[];
}

// HtmlWebpackPlugin v3 and v4 use different hook interfaces. Figure out
// which we're using and infer the generic type variable inside.
type HtmlPluginData = HtmlWebpackPlugin.Hooks extends HtmlPluginDataHookV3<infer T> ? T
: HtmlWebpackPlugin.Hooks extends HtmlPluginDataHookV4<infer U> ? U
type HtmlPluginData = HtmlWebpackPlugin.Hooks extends HtmlPluginDataHookV4<infer U> ? U
: any; // Fallback when nothing works.

/**
Expand Down Expand Up @@ -88,6 +86,20 @@ declare namespace CspHtmlWebpackPlugin {
* include nonces.
*/
nonceEnabled?: { [directive: string]: boolean } | undefined;
/**
* Allows the developer to overwrite the default method of what happens to the CSP after it has been created.
*
* @param builtPolicy A string containing the completed policy
* @param htmlPluginData The `HtmlWebpackPlugin` object
* @param $ The `cheerio` object of the html file currently being processed
* @param compilation Internal webpack object to manipulate the build
*/
processFn?(
builtPolicy: string,
htmlPluginData: HtmlPluginData,
$: CheerioAPI,
compilation: compilation.Compilation,
): void;
}
}

Expand All @@ -101,12 +113,6 @@ declare module "html-webpack-plugin" {
}
}

// Helpers for extracting the relevant generic types from
// HtmlWebpackPlugin.Hooks.
interface HtmlPluginDataHookV3<T> {
htmlWebpackPluginAfterHtmlProcessing: AsyncSeriesWaterfallHook<T>;
}

interface HtmlPluginDataHookV4<T> {
beforeEmit: AsyncSeriesWaterfallHook<T>;
}
7 changes: 4 additions & 3 deletions types/csp-html-webpack-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"private": true,
"name": "@types/csp-html-webpack-plugin",
"version": "3.0.9999",
"version": "5.1.9999",
"projects": [
"https://github.com/slackhq/csp-html-webpack-plugin"
],
"dependencies": {
"@types/html-webpack-plugin": "*",
"@types/tapable": "^1",
"cheerio": ">=1",
"html-webpack-plugin": ">=4",
"tapable": "^2.2.1",
"@types/webpack": "^4"
},
"devDependencies": {
Expand Down