Skip to content

Commit

Permalink
Added types declarations to handlebars-webpack-plugin (DefinitelyType…
Browse files Browse the repository at this point in the history
…d#51425)

* Added haldebars-webpack-plugin types

* Fixed errors

* Fixed errors

* Fixed errors

* Update types/handlebars-webpack-plugin/package.json

Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) <[email protected]>

Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) <[email protected]>
  • Loading branch information
Odas0R and peterblazejewicz authored Mar 1, 2021
1 parent f14df1f commit ac48cb0
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 0 deletions.
54 changes: 54 additions & 0 deletions types/handlebars-webpack-plugin/handlebars-webpack-plugin-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import path = require('path');
import HandlebarsPlugin = require('handlebars-webpack-plugin');
import HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
plugins: [
new HandlebarsPlugin(),
new HandlebarsPlugin({}),
new HandlebarsPlugin({
htmlWebpackPlugin: {
title: 'hey',
},
}),
new HandlebarsPlugin({
htmlWebpackPlugin: {
title: 'hey',
HtmlWebpackPlugin,
},
}),
new HandlebarsPlugin({
entry: path.join(process.cwd(), 'app', 'src', '*.hbs'),
output: path.join(process.cwd(), 'build', '[name].html'),
data: require('package.json'),
partials: [path.join(process.cwd(), 'app', 'src', 'components', '*', '*.hbs')],
helpers: {
hi: (arg1, arg2, options) => {},
hey: (arg1, arg2, options) => {},
},
getTargetFilepath: (filepath, outputTemplate, rootFolder) => {
const fileName = path.basename(filepath).replace(path.extname(filepath), '');
return outputTemplate.replace('[name]', fileName);
},
getPartialId: filePath => {
return filePath.match(/\/([^/]+\/[^/]+)\.[^.]+$/)?.pop();
},
}),
new HandlebarsPlugin({
entry: path.join(process.cwd(), 'app', 'src', '*.hbs'),
output: path.join(process.cwd(), 'build', '[name].html'),
data: path.join(__dirname, 'app/data/project.json'),
partials: [path.join(process.cwd(), 'app', 'src', 'components', '*', '*.hbs')],
helpers: {
is: (arg1, arg2, options) => {},
projectHelpers: path.join(process.cwd(), 'app', 'helpers', '*.helper.js'),
},
onBeforeSetup: Handlebars => {},
onBeforeAddPartials: (Handlebars, partialsMap) => {},
onBeforeCompile: (Handlebars, templateContent) => {},
onBeforeRender: (Handlebars, data, filename) => {},
onBeforeSave: (Handlebars, resultHtml, filename) => {},
onDone: (Handlebars, filename) => {},
}),
],
};
114 changes: 114 additions & 0 deletions types/handlebars-webpack-plugin/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Type definitions for handlebars-webpack-plugin 2.2
// Project: https://github.com/sagold/handlebars-webpack-plugin
// Definitions by: odas0R <https://github.com/Odas0R>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 3.7

import { Compiler, WebpackPluginInstance } from 'webpack';
import { HelperDeclareSpec } from 'handlebars';
import { Options } from 'html-webpack-plugin';

import HtmlWebpackPlugin = require('html-webpack-plugin');

declare class HandlebarsWebpackPlugin implements WebpackPluginInstance {
constructor(options?: HandlebarsWebpackPlugin.PluginOptions);

/**
* Apply the plugin
*/
apply(compiler: Compiler): void;
}

declare namespace HandlebarsWebpackPlugin {
/**
* Type for the object partials that the plugin creates in other to process all partials
*/
interface PartialsMap {
[partial: string]: string;
}

/**
* Handlebars Webpack Plugin Options
*/
interface PluginOptions {
/**
* Path to hbs entry file(s).
* Also supports nested directories if write
* path.join(process.cwd(), "app", "src", "**", "*.hbs"),
*/
entry?: string;

/**
* Output path and filename(s).
* This should lie within the webpacks output-folder
* if omitted, the input filepath stripped of its extension will be used
*/
output?: string;

/**
* You can also add a [path] variable, which will emit the files with their
* relative path, like output:
* path.join(process.cwd(), "build", [path], "[name].html
*
* data passed to main hbs template: `main-template(data)`
*/
data?: object | string;

/**
* globbed path to partials, where folder/filename is unique
*/
partials?: string[];

/**
* Register custom helpers. May be either a function or a glob-pattern
*/
helpers?: HelperDeclareSpec | { projectHelpers: string };

/**
* Modify the default output path of each entry-template
*/
getTargetFilepath?: (filepath: string, outputTemplate: string, rootFolder: string) => string | undefined;

/**
* Modify the hbs partial-id created for a loaded partial
*/
getPartialId?: (filePath: string) => string | undefined;

/**
* onBeforeSetup hook, runs before setup of the plugin
*/
onBeforeSetup?: (Handlebars: RuntimeOptions) => any;

/**
* onBeforeAddPartials hook, runs before the partials addition to the .html files
*/
onBeforeAddPartials?: (Handlebars: RuntimeOptions, partialsMap: PartialsMap) => any;

/**
* onBeforeCompile hook, runs before the plugin compilation
*/
onBeforeCompile?: (Handlebars: RuntimeOptions, templateContent: string) => any;

/**
* onBeforeRender hook, runs before rendering of the templates
*/
onBeforeRender?: (Handlebars: RuntimeOptions, data: object, filename: string) => any;

/**
* onBeforeSave hook, runs before saving
*/
onBeforeSave?: (Handlebars: RuntimeOptions, resultHtml: string, filename: string) => any;

/**
* onDone, runs before the final stages of the plugin
*/
onDone?: (Handlebars: RuntimeOptions, filename: string) => any;

/**
* HtmlWebpackPlugin additional configurations
*/
htmlWebpackPlugin?: Options | HtmlWebpackPlugin;
}
}

export = HandlebarsWebpackPlugin;
6 changes: 6 additions & 0 deletions types/handlebars-webpack-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"handlebars": ">=4.0.3"
}
}
23 changes: 23 additions & 0 deletions types/handlebars-webpack-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"handlebars-webpack-plugin-tests.ts"
]
}
1 change: 1 addition & 0 deletions types/handlebars-webpack-plugin/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

0 comments on commit ac48cb0

Please sign in to comment.