Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
babel-plugin-auto-import
Advanced tools
Convert global variables to import statements
.babelrc
{
"plugins": [[
"auto-import", {
"declarations": [
{ "default": "React", "path": "react" }
]
}
]]
}
In
React.createElement("div", null, []);
Out
import React from "react";
React.createElement("div", null, []);
.babelrc
{
"plugins": [[
"auto-import", {
"declarations": [
{ "default": "React", "members": ["Component"], "path": "react" }
]
}
]]
}
In
class MyComponent extends Component { }
Out
import { Component } from "react";
class MyComponent extends Component { }
Suitable for polyfilling browser built-ins (eg. window.fetch
)
.babelrc
{
"plugins": [[
"auto-import", {
"declarations": [
{ "anonymous": ["fetch"], "path": "whatwg-fetch" }
]
}
]]
}
In
fetch("http://example.com/qwe");
Out
import "whatwg-fetch";
fetch("http://example.com/qwe");
Generate import path by filename. [name] will be replaced to processed filename.
.babelrc
{
"plugins": [[
"auto-import", {
"declarations": [
{ "default": "styles", "path": "./[name].css" }
]
}
]]
}
In
component-name.js
// ...
<input className={styles.className} />
// ...
Out
import styles from "./component-name.css";
// ...
<input className={styles.className} />
// ...
You can process filename by "nameReplacePattern" and "nameReplaceString" options. It's processing like this:
"basename.js".replace(new RegExp(nameReplacePattern), nameReplaceString); // == [name]
By default
nameReplacePattern == "\\.js$";
nameReplaceString == "";
.babelrc
{
"plugins": [[
"auto-import", {
"declarations": [
{
"default": "styles", "path": "./[name].css",
"nameReplacePattern": "\.component\.js$", "nameReplaceString": ".styles"
}
]
}
]]
}
In
name.component.js
// ...
<input className={styles.className} />
// ...
Out
import styles from "./name.styles.css";
// ...
<input className={styles.className} />
// ...
If you don't want to eject your project, you can use react-app-rewired and customize-cra.
config-overrides.js
const { override, addBabelPlugins, disableEsLint } = require("customize-cra");
const autoImportPluginOptions = {
"declarations": [
{ "default": "React", members: ["Component"], "path": "react" }
]
};
module.exports = override(
...addBabelPlugins([require("babel-plugin-auto-import"), autoImportPluginOptions]),
disableEsLint(),
);
FAQs
Babel plugin for variable auto imports
The npm package babel-plugin-auto-import receives a total of 15,735 weekly downloads. As such, babel-plugin-auto-import popularity was classified as popular.
We found that babel-plugin-auto-import 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.