diff --git a/types/csp-html-webpack-plugin/csp-html-webpack-plugin-tests.ts b/types/csp-html-webpack-plugin/csp-html-webpack-plugin-tests.ts index 97e9e681067fa6..a1e55815e558a6 100755 --- a/types/csp-html-webpack-plugin/csp-html-webpack-plugin-tests.ts +++ b/types/csp-html-webpack-plugin/csp-html-webpack-plugin-tests.ts @@ -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 = { @@ -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 | undefined, options?: CheerioOptions | undefined): string; } + $.html; + // $ExpectType (this: CheerioAPI, dom?: BasicAcceptedElems | undefined) => string + $.xml; + // $ExpectType Compilation + compilation; + // $ExpectType any[] + compilation.errors; + }, }); const optsWithEnableFunc: CspHtmlWebpackPlugin.AdditionalOptions = { @@ -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; }, @@ -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 | undefined, options?: CheerioOptions | undefined): string; } + $.html; + // $ExpectType (this: CheerioAPI, dom?: BasicAcceptedElems | undefined) => string + $.xml; + // $ExpectType Compilation + compilation; + // $ExpectType any[] + compilation.errors; + }, }; // Can be added to Webpack configuration. @@ -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 | undefined, options?: CheerioOptions | undefined): string; } + $.html; + // $ExpectType (this: CheerioAPI, dom?: BasicAcceptedElems | undefined) => string + $.xml; + // $ExpectType Compilation + compilation; + // $ExpectType any[] + compilation.errors; + }, }, + xhtml: false, }); diff --git a/types/csp-html-webpack-plugin/index.d.ts b/types/csp-html-webpack-plugin/index.d.ts index 857b9fba3b06d1..5ff74091efebf8 100755 --- a/types/csp-html-webpack-plugin/index.d.ts +++ b/types/csp-html-webpack-plugin/index.d.ts @@ -1,5 +1,6 @@ +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; @@ -7,8 +8,8 @@ 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, @@ -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 ? T - : HtmlWebpackPlugin.Hooks extends HtmlPluginDataHookV4 ? U + type HtmlPluginData = HtmlWebpackPlugin.Hooks extends HtmlPluginDataHookV4 ? U : any; // Fallback when nothing works. /** @@ -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; } } @@ -101,12 +113,6 @@ declare module "html-webpack-plugin" { } } -// Helpers for extracting the relevant generic types from -// HtmlWebpackPlugin.Hooks. -interface HtmlPluginDataHookV3 { - htmlWebpackPluginAfterHtmlProcessing: AsyncSeriesWaterfallHook; -} - interface HtmlPluginDataHookV4 { beforeEmit: AsyncSeriesWaterfallHook; } diff --git a/types/csp-html-webpack-plugin/package.json b/types/csp-html-webpack-plugin/package.json index 0e14e5def922d6..96e873c70544a8 100644 --- a/types/csp-html-webpack-plugin/package.json +++ b/types/csp-html-webpack-plugin/package.json @@ -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": {