Gathering detailed insights and metrics for babel-plugin-transform-jsx-directives
Gathering detailed insights and metrics for babel-plugin-transform-jsx-directives
Gathering detailed insights and metrics for babel-plugin-transform-jsx-directives
Gathering detailed insights and metrics for babel-plugin-transform-jsx-directives
Functional directives for JSX elements based on element or attribute name
npm install babel-plugin-transform-jsx-directives
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
173 Commits
1 Watchers
2 Branches
3 Contributors
Updated on Aug 23, 2018
Latest Version
1.1.3
Package Id
babel-plugin-transform-jsx-directives@1.1.3
Size
7.83 kB
NPM Version
5.5.1
Node Version
8.9.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Functional directives for JSX elements based on element or attribute name.
Think of it as globally available on-demand higher order components.
npm install --save-dev babel-plugin-transform-jsx-directives
Add in your .babelrc
1{ 2 "plugins": [ 3 ["transform-jsx-directives", { 4 "directives": [ 5 { 6 "type": "element", 7 "name": "button", 8 "priority": 100, 9 "source": "./myButtonDirective.js", 10 "bootstrap": { 11 "colour": "fuchsia" 12 } 13 }, 14 ["directive-module", { "colour": "pink" }], 15 "path/to/a/directiveConfiguration.js" 16 ] 17 }] 18 ] 19}
directives
(Array): A mandatory* array of configurations
or node modules/files providing one or more configurations.* Not really mandatory, but this plugin wont do nothing without specific configurations
name
(String): When a element or attribute (see type) matches the name,
the directive gets appliedsource
(String|Function): path to the directive runtime component or
function returning the path. Function gets called with transformed options
and bootstrap.type
("attribute"|"element"): whether the directive should be applied
on matches against element names or attribute names. Default: "attribute"priority
(Integer): Directives with a higher priority run first, Default: 0bootstrap
(any): If present, a bootstrap
function is imported
from source
and called a single time when the application starts with any value
provided to bootstrap
. The value is being JSON encoded, in order to be moved
from config to runtime, so methods will get lost.transformOptions
(Function): Only for attribute directives. Optional transformer
for the options node. See Transform OptionsMy main motivation behind this plugin is to provide a clean way of extending the feature set of a JSX target library.
I recommend sticking to plain old components and higher order components when working on application specific solutions.
Use directives in cases where you want to provide a globally available abstraction for a complex solution that feels like it's part of the library you're using.
Assuming we want every button in our app to alert "baby don't hurt me" on click.
This is our button.jsx
file
1<button>What is love?</button>
when we use this directive configuration
1{ 2 "name": "button", 3 "type": "element", 4 "source": "./HaddawayButton.js" 5}
the directive plugin will transform our button.jsx
into
1import _buttonDirective from './HaddawayButton.js'; 2 3<_buttonDirective 4 Elm="button" 5 props={{}} 6 next={(_Elm, _props) => <_Elm {..._props} />} 7/>
the last bit is now the implementation of HaddawayButton.js
1function whatIsLove() { 2 alert('baby don\'t hurt me'); 3} 4 5export default function HaddawayButton({ Elm, props, next }) { 6 return next(Elm, { ...props, onClick: whatIsLove }); 7}
and voilà you have an earworm.
As shown in the example, directive components
receive Elm
, props
, next
and attribute directives additionally options
.
Since the runtime is just another component, it can utilize all features of the
target library, like context in React.
Elm
: Element name or component, the directive was matched against.
Directives can manipulate the element by passing a new one into next
.
Since multiple directives can be applied to the same element, Elm
is not necessarily
the original element.
props
: Object of all attributes used on the element.
Can be manipulated by passing new props into next
.
Since multiple directives can be applied to the same element, these are not necessarily
the original attributes.
next
: Callback function that will apply the next directive or create the child elements.
A no-op directive would just return next(Elm, props)
.
A directive can also decide to not call next
at all and prevent creation
of all child components.
options
: Value of the directive attribute. (Only available on attribute directives)
Given this jsx <div foo="bar" />
, a foo
attribute directive would receive
"bar"
as options
:
The original attribute used for options
is excluded from props
.
Parent directives have no access to the options
of child directives
so this always is the original value.
Directives can provide an option transformer in order to mutate own options beforehand.
as
: JSX namespace of the directive attribute. (Only available on attribute directives)
Given this jsx <div onClick:alert="Hello World!" />
, a alert
attribute directive would
receive "onClick"
as as
.
A transformOptions(babel, node)
function provided to a Directive Configuration that returns a (new) babel node.
The main Idea here is to support transforming of Domain-specific languages for attribute directives pre-runtime but it also could be used for validation or to add defaults.
Lets say our directive expects an object as options but we want to provide a
shorthand for { value: 'Foo' }
and just use "Foo"
in that case.
1{
2 name: 'foo',
3 type: 'attribute',
4 transformOptions({ types: t }, node) {
5 if (!t.isStringLiteral(node)) {
6 return node;
7 }
8
9 return t.jSXExpressionContainer(
10 t.objectExpression([
11 t.objectProperty(
12 t.identifier('value'),
13 node
14 ),
15 ])
16 );
17 }
18}
The MIT License
Copyright (C) 2017 Hannes Diercks
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/2 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
93 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More