Skip to content

Commit

Permalink
feat: Add typings to package.json
Browse files Browse the repository at this point in the history
See #1132
  • Loading branch information
wingrunr21 authored and jantimon committed Jan 11, 2019
1 parent 342c4e8 commit a524e8f
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 117 deletions.
26 changes: 14 additions & 12 deletions index.js
@@ -1,8 +1,9 @@
// @ts-check
// Import types
/* eslint-disable */
/// <reference path="./typings.d.ts" />
/* eslint-enable */
/** @typedef {import("./typings").HtmlTagObject} HtmlTagObject */
/** @typedef {import("./typings").Options} HtmlWebpackOptions */
/** @typedef {import("./typings").ProcessedOptions} ProcessedHtmlWebpackOptions */
/** @typedef {import("./typings").TemplateParameter} TemplateParameter */
/** @typedef {import("webpack/lib/Compiler.js")} WebpackCompiler */
/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
'use strict';
Expand All @@ -28,14 +29,14 @@ const fsReadFileAsync = promisify(fs.readFile);

class HtmlWebpackPlugin {
/**
* @param {Partial<HtmlWebpackPluginOptions>} [options]
* @param {HtmlWebpackOptions} [options]
*/
constructor (options) {
/** @type {Partial<HtmlWebpackPluginOptions>} */
/** @type {HtmlWebpackOptions} */
const userOptions = options || {};

// Default options
/** @type {HtmlWebpackPluginOptions} */
/** @type {ProcessedHtmlWebpackOptions} */
const defaultOptions = {
template: path.join(__dirname, 'default_index.ejs'),
templateContent: false,
Expand All @@ -56,7 +57,7 @@ class HtmlWebpackPlugin {
xhtml: false
};

/** @type {HtmlWebpackPluginOptions} */
/** @type {ProcessedHtmlWebpackOptions} */
this.options = Object.assign(defaultOptions, userOptions);

// Default metaOptions if no template is provided
Expand Down Expand Up @@ -112,6 +113,7 @@ class HtmlWebpackPlugin {

const minify = this.options.minify;
if (minify === true || (minify === undefined && isProductionLikeMode)) {
/** @type { import('html-minifier').Options } */
this.options.minify = {
// https://github.com/kangax/html-minifier#options-quick-reference
collapseWhitespace: true,
Expand Down Expand Up @@ -381,7 +383,7 @@ class HtmlWebpackPlugin {
/**
* This function renders the actual html by executing the template function
*
* @param {(templatePArameters) => string | Promise<string>} templateFunction
* @param {(templateParameters) => string | Promise<string>} templateFunction
* @param {{
publicPath: string,
js: Array<string>,
Expand Down Expand Up @@ -628,7 +630,7 @@ class HtmlWebpackPlugin {
*
* @param {string|false} faviconFilePath
* @param {WebpackCompilation} compilation
* @parma {string} publicPath
* @param {string} publicPath
* @returns {Promise<string|undefined>}
*/
getFaviconPublicPath (faviconFilePath, compilation, publicPath) {
Expand Down Expand Up @@ -877,7 +879,7 @@ class HtmlWebpackPlugin {
/**
* Helper to return the absolute template path with a fallback loader
* @param {string} template
* The path to the tempalate e.g. './index.html'
* The path to the template e.g. './index.html'
* @param {string} context
* The webpack base resolution path for relative paths e.g. process.cwd()
*/
Expand Down Expand Up @@ -920,8 +922,8 @@ class HtmlWebpackPlugin {
headTags: HtmlTagObject[],
bodyTags: HtmlTagObject[]
}} assetTags
* @param {HtmlWebpackPluginOptions} options
* @returns {HtmlWebpackPluginTemplateParameter}
* @param {ProcessedHtmlWebpackOptions} options
* @returns {TemplateParameter}
*/
function templateParametersGenerator (compilation, assets, assetTags, options) {
const xhtml = options.xhtml;
Expand Down
22 changes: 6 additions & 16 deletions lib/hooks.js
@@ -1,7 +1,5 @@
// @ts-check
/* eslint-disable */
/// <reference path="../typings.d.ts" />
/* eslint-enable */
/** @typedef {import("../typings").Hooks} HtmlWebpackPluginHooks */
'use strict';
/**
* This file provides access to all public htmlWebpackPlugin hooks
Expand All @@ -12,10 +10,9 @@

const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;

// The following typedef holds the API definition for all available hooks
// to allow easier access when using ts-check or typescript inside plugins
/** @typedef {{
// The following is the API definition for all available hooks
// For the TypeScript definition, see the Hooks type in typings.d.ts
/**
beforeAssetTagGeneration:
AsyncSeriesWaterfallHook<{
assets: {
Expand All @@ -28,7 +25,6 @@ const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
outputName: string,
plugin: HtmlWebpackPlugin
}>,
alterAssetTags:
AsyncSeriesWaterfallHook<{
assetTags: {
Expand All @@ -39,15 +35,13 @@ const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
outputName: string,
plugin: HtmlWebpackPlugin
}>,
alterAssetTagGroups:
AsyncSeriesWaterfallHook<{
headTags: Array<HtmlTagObject | HtmlTagObject>,
bodyTags: Array<HtmlTagObject | HtmlTagObject>,
outputName: string,
plugin: HtmlWebpackPlugin
}>,
afterTemplateExecution:
AsyncSeriesWaterfallHook<{
html: string,
Expand All @@ -56,22 +50,18 @@ const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
outputName: string,
plugin: HtmlWebpackPlugin,
}>,
beforeEmit:
AsyncSeriesWaterfallHook<{
html: string,
outputName: string,
plugin: HtmlWebpackPlugin,
}>,
afterEmit:
AsyncSeriesWaterfallHook<{
outputName: string,
plugin: HtmlWebpackPlugin
}>,
}} HtmlWebpackPluginHooks
*/
}>
*/

/**
* @type {WeakMap<WebpackCompilation, HtmlWebpackPluginHooks>}}
Expand Down
4 changes: 1 addition & 3 deletions lib/html-tags.js
@@ -1,7 +1,5 @@
// @ts-check
/* eslint-disable */
/// <reference path="../typings.d.ts" />
/* eslint-enable */
/** @typedef {import("../typings").HtmlTagObject} HtmlTagObject */
/**
* @file
* This file provides to helper to create html as a object repesentation as
Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -5,6 +5,7 @@
"description": "Simplifies creation of HTML files to serve your webpack bundles",
"author": "Jan Nicklas <j.nicklas@me.com> (https://github.com/jantimon)",
"main": "index.js",
"types": "typings.d.ts",
"files": [
"lib/",
"index.js",
Expand All @@ -27,9 +28,11 @@
]
},
"devDependencies": {
"@types/html-minifier": "^3.5.2",
"@types/loader-utils": "1.1.3",
"@types/node": "10.11.4",
"@types/tapable": "1.0.4",
"@types/webpack": "^4.0.0",
"appcache-webpack-plugin": "^1.4.0",
"commitizen": "3.0.2",
"css-loader": "^1.0.0",
Expand Down

0 comments on commit a524e8f

Please sign in to comment.