Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
babel-plugin-react-defaultprops
Advanced tools
A babel plugin that extracts es6 default parameters and append it to the component property named __defaultProps
.
npm install --save-dev babel-plugin-react-defaultprops
yarn add -D babel-plugin-react-defaultprops
Add the plugin to the Babel configuration:
babel.config.js
module.exports = {
plugins: ['module:babel-plugin-react-defaultprops'],
};
Before:
export function FunctionComponent({ bar, foo = 'func' }) {
return <div>{bar + foo}</div>;
}
After:
function FunctionComponent(_ref) {
var bar = _ref.bar,
_ref$foo = _ref.foo,
foo = _ref$foo === void 0 ? 'func' : _ref$foo;
return _react['default'].createElement('div', null, bar + foo);
}
FunctionComponent.__defaultProps = {
foo: 'func',
};
Or with defaults in function body:
Before:
export const VariableComponent = (props) => {
const { bar, foo = 'variable' } = props;
return <div>{bar + foo}</div>;
};
After:
var VariableComponent = function VariableComponent(props) {
var bar = props.bar,
_props$foo2 = props.foo,
foo = _props$foo2 === void 0 ? 'variable' : _props$foo2;
return _react['default'].createElement('div', null, bar + foo);
};
VariableComponent.__defaultProps = {
foo: 'variable',
};
For more example look into the test folder.
Node that the default props with the locale variable as a value in the function body will not be included.
To make object form default props, works only inside the function body.
import React from 'react';
import { getDefaultProps } from 'babel-plugin-react-defaultprops/utils';
export function FunctionComponent({ bar, foo = 'func' }) {
const defaultProps = getDefaultProps();
return <div>{bar + foo}</div>;
}
Transform to:
function FunctionComponent(_ref) {
var bar = _ref.bar,
_ref$foo = _ref.foo,
foo = _ref$foo === void 0 ? 'func' : _ref$foo;
var defaultProps = {
foo: 'func',
};
return _react['default'].createElement('div', null, bar + foo);
}
This function does not work in nested component inside function, works only with top level components.
If using typescript ad following to theglobal.d.ts
file:
declare module 'react' {
export interface FunctionComponent<P = unknown> {
__defaultProps?: Partial<P>;
}
}
export {};
FAQs
A plugin to extract es6 default parameters
The npm package babel-plugin-react-defaultprops receives a total of 1 weekly downloads. As such, babel-plugin-react-defaultprops popularity was classified as not popular.
We found that babel-plugin-react-defaultprops demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.