babel-plugin-event-on-class-extend
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

Babel Plugin For Event On Class Extend

Demo

Run Example To See Compiled Output

Stack Overflow Answer

Changelog

NPM Package

Install

npm i -D babel-plugin-event-on-class-extend

Add this plugin to your babel config file or add it from the cli

Babel Config

{
	"plugins": ["babel-plugin-event-on-class-extend"]
}

CLI

npx babel ... --plugins=babel-plugin-event-on-class-extend

Usage

class X {
	static name = '"I\'m X"'

	public instanceMethod() {}

	// new (...args: any[]) => X Prevent calling instance method
	static onExtend(targetClass: new (...args: any[]) => X) {
		console.log(`Class X is extended by ${targetClass.name}`)
		// targetClass.instanceMethod() This will throw error
	}
}

class Y extends X {
	static name = '"I\'m Y"'
}

// X?.onExtend(Y) will be called here

This will output Class X is extended by "I'm Y" when ran.

Options

How To Use Plugin Options

Change the plugin usage to this in babel config file

{
	"plugins": [
		[
			"babel-plugin-event-on-class-extend",
			{ "staticCallbackName": "onExtend" }
		]
	]
}

List

Option Description Default Required
staticCallbackName Change the default static method callback name onExtend false
classOptionalChain Enable class optional operator. X?. optional chain false false
passDerivedClassAsParameter If disable derived class will not be passed to onExtend as parameter false true false

Package Sidebar

Install

npm i babel-plugin-event-on-class-extend

Weekly Downloads

1

Version

1.2.1

License

MIT

Unpacked Size

10.4 kB

Total Files

8

Last publish

Collaborators

  • kennarddh