Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
babel-plugin-treasure
Advanced tools
Based on babel-plugin-import, we are committed to realizing the AST optimization requirements of the unified library and responding to the requirements of various AST node modification operations. Currently used in a unified and convenient way to solve the on-demand loading requirements of any component library
// use npm
npm i babel-plugin-treasure --save-dev
// use yarn
yarn add babel-plugin-treasure -D
Can be used in the following two ways
Add to .babelrc
or babel-loader.
{
"plugins": [["treasure", options]]
}
ʻOptions` can be an object.
{
"libraryName": "dt-react-component",
"style": true, // or'css'
}
ʻOptions` can be an array, but it does not support setting in the babel@7+ environment
[
{
libraryName: 'dt-react-component',
style: true, // or'css'
},
];
ʻOptions` cannot be an array in the babel@7+ environment, but you can add a name to the plugin to support reuse.
such as:
// .babelrc
"plugins": [
["import", {"libraryName": "dt-react-component", "libraryDirectory": "lib"}, "dtcomponent"],
["import", {"libraryName": "lodash", "libraryDirectory": "lib"}, "lodash"]
]
When you simply import the library in the project, you only need to add the following configuration. For example, configure dt-react-component:
"plugins": [
[
"treasure",
{
"libraryName": "dt-react-component",
"libraryDirectory": "lib",
"camel2DashComponentName": "lower",
"style": "css"
}
]
]
Note: When you have multiple libraries in your development project that need to be loaded on demand, you can add an alias to distinguish it, for example, dt-react-component is used with antd:
"plugins": [
[
"treasure",
{
"libraryName": "antd",
"libraryDirectory": "lib",
"style": "css"
},
"antd"
],
[
"treasure",
{
"libraryName": "dt-react-component",
"libraryDirectory": "lib",
"camel2DashComponentName": "lower",
"style": "css"
},
"dt-react-component"
]
]
The name of the library needs to be imported as required, which is required.
{ "libraryName": "dt-react-component" }
Formulate the package format directory of the library, generally lib, es, esm, umd, etc., which are determined by the package developer. The default value of this option is lib
{ libraryDirectory: "lib" }
Do you need to load css files on demand, not enabled by default Note: Load the css precompiled file (less/scss) when the style is set to true, and load the css file when it is set to css.
Load the css precompiled file:
{ libraryDirectory: true }
Load the css file:
{ libraryDirectory: "css" }
The library package for making css files, generally does not need to be written
{ styleLibraryDirectory: "lib" }
There are four parameters for the conversion of the component name, and the default is true. The conversion rules are as follows:
import {ChromeDownload} from'dt-react-component'
↓ ↓ ↓ ↓ ↓ ↓
// When camel2DashComponentName: true
ChromeDownload → chrome-download
// When camel2DashComponentName: false
ChromeDownload → ChromeDownload // No changes
// When camel2DashComponentName: "lower"
ChromeDownload → chromeDownload // convert lower camel
// When camel2DashComponentName: "upper"
ChromeDownload → ChromeDownload // convert upper camel
Handle multi-word components with _ for word segmentation
import { ChromeDownload } from'dt-react-component'
↓ ↓ ↓ ↓ ↓ ↓
ChromeDownload → chrome_download
Process the attributes of the default import library, the default is true. You can give an array, and the components in the array will not be exported in the default form. If your library is not imported by default at all, please set the option to false For example:
// Set transformToDefaultImport: [ChromeDownload]
import { ChromeDownload, Circle } from'dt-react-component'
↓ ↓ ↓ ↓ ↓ ↓
import _Circle from "dt-react-component/lib/circle";
import { ChromeDownload as _ChromeDownload } from "dt-react-component/lib/chromeDownload";
Handle individual sequences with irregular rules, support function, object and path import
// Function form
[
'import',
{
libraryName: 'dt-react-component',
customName: (name: string) => {
if (name === 'go-back') {
return 'antd/lib/go-back';
}
return `antd/lib/${name}`;
},
},
];
After processing, it will become like this
import {GoBack} from "antd"
↓ ↓ ↓ ↓ ↓ ↓
var _button = require('antd/lib/go-back');
// Object form
[
'import',
{
libraryName: 'dt-react-component',
customName: {
GoBack: 'dt-react-component/lib/go-back',
},
},
];
Note: When you use a function, the function parameter is the name converted by styleLibraryDirectory or camel2DashComponentName. When you use an object as a parameter, the key of the object will not undergo a special conversion. The same is true for customStyleName. In addition, you can also choose the reference path:
// reference path
[
'import',
{
libraryName: 'dt-react-component',
customName: {
GoBack: require('path').resolve(__dirname, './customName.js'),
},
},
];
customName.js
is similar:
module.exports = function customName(name) {
return `dt-react-component/lib/${name}`;
};
Same as customName, but used to process the style file path
Process links to specific files, such as:
// Object form
[
"import",
{
"libraryName": "dt-react-component",
"fileName": "example"
"customName": {
"GoBack": "dt-react-component/lib/go-back",
}
}
]
The result after conversion is as follows:
import {ChromeDownload} from'dt-react-component'
↓ ↓ ↓ ↓ ↓ ↓
import'dt-react-component/lib/chrome-download/exmaple'
MIT
FAQs
a babel plugin for the special demand
We found that babel-plugin-treasure 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.