https://github.com/PavelDymkov/babel-plugin-auto-import
https://github.com/PavelDymkov/babel-plugin-auto-import
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/PavelDymkov/babel-plugin-auto-import
- Owner: PavelDymkov
- Created: 2017-04-26T09:36:12.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2023-03-02T16:56:29.000Z (about 3 years ago)
- Last Synced: 2024-11-02T15:50:15.210Z (over 1 year ago)
- Language: JavaScript
- Size: 441 KB
- Stars: 41
- Watchers: 2
- Forks: 6
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-list - babel-plugin-auto-import
README
# babel-plugin-auto-import
Convert global variables to import statements
- [Using with Create React App](#using-with-create-react-app)
## Examples
### Example 1
**.babelrc**
```json
{
"plugins": [[
"auto-import", {
"declarations": [
{ "default": "React", "path": "react" }
]
}
]]
}
```
**In**
```javascript
React.createElement("div", null, []);
```
**Out**
```javascript
import React from "react";
React.createElement("div", null, []);
```
### Example 2
**.babelrc**
```json
{
"plugins": [[
"auto-import", {
"declarations": [
{ "default": "React", "members": ["Component"], "path": "react" }
]
}
]]
}
```
**In**
```javascript
class MyComponent extends Component { }
```
**Out**
```javascript
import { Component } from "react";
class MyComponent extends Component { }
```
### Example 3
Suitable for polyfilling browser built-ins (eg. `window.fetch`)
**.babelrc**
```json
{
"plugins": [[
"auto-import", {
"declarations": [
{ "anonymous": ["fetch"], "path": "whatwg-fetch" }
]
}
]]
}
```
**In**
```javascript
fetch("http://example.com/qwe");
```
**Out**
```javascript
import "whatwg-fetch";
fetch("http://example.com/qwe");
```
### Example 4
Generate import path by filename. [name] will be replaced to processed filename.
**.babelrc**
```json
{
"plugins": [[
"auto-import", {
"declarations": [
{ "default": "styles", "path": "./[name].css" }
]
}
]]
}
```
**In**
``` component-name.js ```
```javascript
// ...
// ...
```
**Out**
```javascript
import styles from "./component-name.css";
// ...
// ...
```
You can process filename by "nameReplacePattern" and "nameReplaceString" options. It's processing like this:
```javascript
"basename.js".replace(new RegExp(nameReplacePattern), nameReplaceString); // == [name]
```
By default
```javascript
nameReplacePattern == "\\.js$";
nameReplaceString == "";
```
**.babelrc**
```json
{
"plugins": [[
"auto-import", {
"declarations": [
{
"default": "styles", "path": "./[name].css",
"nameReplacePattern": "\.component\.js$", "nameReplaceString": ".styles"
}
]
}
]]
}
```
**In**
``` name.component.js ```
```javascript
// ...
// ...
```
**Out**
```javascript
import styles from "./name.styles.css";
// ...
// ...
```
## Using with Create React App
If you don't want to eject your project, you can use [react-app-rewired](https://github.com/timarney/react-app-rewired) and
[customize-cra](https://github.com/arackaf/customize-cra).
[Follow the instructions](https://github.com/timarney/react-app-rewired#how-to-rewire-your-create-react-app-project).
**config-overrides.js**
```javascript
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(),
);
```