babel-plugin-transform-imports
Advanced tools
Comparing version 1.5.0 to 1.5.1
{ | ||
"name": "babel-plugin-transform-imports", | ||
"version": "1.5.0", | ||
"version": "1.5.1", | ||
"description": "Transforms member style imports (import {x} from 'y') into default style imports (import x from 'y/lib/x')", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -21,3 +21,3 @@ # babel-plugin-transform-imports | ||
## That's stupid, why would you do that? | ||
## Why? | ||
@@ -39,6 +39,6 @@ When Babel encounters a member style import such as: | ||
Some libraries, such as react-bootstrap and lodash, are rather large and | ||
pulling in the entire module just to use a few pieces would cause unnecessary | ||
bloat to your client optimized (webpack etc.) bundle. The only way around | ||
this is to use default style imports: | ||
Which causes the entire library to be loaded, even though only some components | ||
are needed. Some libraries are rather large and pulling in the entire module | ||
would cause unnecessary bloat to your client optimized (webpack etc.) bundle. | ||
The only way around this is to use default style imports: | ||
@@ -93,5 +93,5 @@ ```javascript | ||
Sometimes you may enforce the same convention in all folder levels on the | ||
structure of your libraries. For achieving this dynamism, you may use regexp | ||
to cover all tranformations. | ||
Sometimes, you may wish to use regular expressions in your transformation (for | ||
example, to enforce the same convention in all folder levels on the structure | ||
of your library). | ||
@@ -113,3 +113,3 @@ .babelrc: | ||
For instance, the previous configuration will solve properly the next scenarios: | ||
Causes this code: | ||
@@ -122,3 +122,3 @@ ```javascript | ||
becomes: | ||
to become: | ||
@@ -132,9 +132,34 @@ ```javascript | ||
### Using a function as the transformer | ||
If you need more advanced or more specific transformation logic, and are using | ||
Babel 7+ with a `.babelrc.js` file, you may provide a function instead of a | ||
string for the `transform` option: | ||
.babelrc.js: | ||
```javascript | ||
module.exports = { | ||
presets: ['@babel/env'], | ||
plugins: [ | ||
['transform-imports', { | ||
'my-library': { | ||
transform: function(importName, matches) { | ||
return `my-library/etc/${importName.toUpperCase()}`; | ||
}, | ||
preventFullImport: true, | ||
} | ||
}] | ||
] | ||
}; | ||
``` | ||
### Using a transformation file | ||
If you need more advanced transformation logic, you may provide a path to a .js | ||
file which exports a function to run instead. Keep in mind that the .js file | ||
will be `require`d relative from this plugin's path, likely located in | ||
`/node_modules/babel-plugin-transform-imports`. You may provide any filename, | ||
as long as it ends with `.js`. | ||
If you need the above flexibility of using a function as a transformer, but | ||
are still on Babel 6 or cannot use a `.babelrc.js` file for some reason, you | ||
may provide a path to a .js file which exports a function to run instead. | ||
Keep in mind that the .js file will be `require`d relative from this plugin's | ||
path, likely located in `/node_modules/babel-plugin-transform-imports`. You | ||
may provide any filename, as long as it ends with `.js`. | ||
@@ -158,3 +183,3 @@ .babelrc: | ||
```js | ||
```javascript | ||
module.exports = function(importName, matches) { | ||
@@ -165,8 +190,2 @@ return 'my-library/etc/' + importName.toUpperCase(); | ||
This is a little bit hacky, but options are a bit limited due to .babelrc being | ||
a JSON5 file which does not support functions as a type. In Babel 7.0, it | ||
appears .babelrc.js files will be supported, at which point this plugin will be | ||
updated to allow transform functions directly in the configuration file. | ||
See: https://github.com/babel/babel/pull/4892 | ||
## Webpack | ||
@@ -177,3 +196,3 @@ | ||
webpack.config.js: | ||
```js | ||
```javascript | ||
module: { | ||
@@ -188,4 +207,4 @@ rules: [{ | ||
[require('babel-plugin-transform-imports'), { | ||
"my-library": { | ||
"transform": function(importName) { | ||
'my-library': { | ||
transform: function(importName, matches) { | ||
return 'my-library/etc/' + importName.toUpperCase(); | ||
@@ -207,3 +226,3 @@ }, | ||
| --- | --- | --- | --- | --- | | ||
| `transform` | `string` | yes | `undefined` | The library name to use instead of the one specified in the import statement. ${member} will be replaced with the import name, aka Grid/Row/Col/etc., and ${1-n} will be replaced by any matched regular expression groups. Alternatively, pass a path to a .js file which exports a function to process the transform, which is invoked with parameters: (importName, matches). (see Advanced Transformations) | | ||
| `transform` | `string | function` | yes | `undefined` | The library name to use instead of the one specified in the import statement. ${member} will be replaced with the import name, aka Grid/Row/Col/etc., and ${1-n} will be replaced by any matched regular expression groups. Alternatively, pass a path to a .js file which exports a function to process the transform, which is invoked with parameters: (importName, matches). If using Babel 7+, a function may be passed directly. (see Advanced Transformations) | | ||
| `preventFullImport` | `boolean` | no | `false` | Whether or not to throw when an import is encountered which would cause the entire module to be imported. | | ||
@@ -210,0 +229,0 @@ | `camelCase` | `boolean` | no | `false` | When set to true, runs ${member} through _.camelCase. | |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22354
222
7