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-transform-imports
Advanced tools
Transforms member style imports (import {x} from 'y') into default style imports (import x from 'y/lib/x')
babel-plugin-transform-imports is a Babel plugin that allows you to transform import statements in your JavaScript code. This can be useful for optimizing bundle sizes by enabling tree-shaking, aliasing imports, or converting import paths.
Tree-shaking
This feature allows you to import only the parts of a library that you need, which can significantly reduce the size of your JavaScript bundle. In this example, importing from 'lodash' will be transformed to import only the specific lodash function, preventing full library imports.
{
"plugins": [
[
"transform-imports",
{
"lodash": {
"transform": "lodash/${member}",
"preventFullImport": true
}
}
]
]
}
Aliasing Imports
This feature allows you to alias import paths, making it easier to manage and refactor your code. In this example, importing from 'my-library' will be transformed to import from 'my-library/dist/${member}', allowing for more flexible and maintainable import paths.
{
"plugins": [
[
"transform-imports",
{
"my-library": {
"transform": "my-library/dist/${member}",
"preventFullImport": true
}
}
]
]
}
Custom Import Paths
This feature allows you to define custom import paths for your modules. In this example, importing from 'components' will be transformed to import from './src/components/${member}', making it easier to manage and organize your project structure.
{
"plugins": [
[
"transform-imports",
{
"components": {
"transform": "./src/components/${member}",
"preventFullImport": true
}
}
]
]
}
babel-plugin-import is a Babel plugin for modular import of components, mainly used for UI libraries like antd. It allows you to import only the components you need, reducing the bundle size. Compared to babel-plugin-transform-imports, it is more specialized for UI libraries and offers additional features like style imports.
babel-plugin-lodash is a Babel plugin that cherry-picks Lodash modules so you don’t have to. It is specifically designed for optimizing Lodash imports, making it more specialized than babel-plugin-transform-imports, which is more general-purpose.
babel-plugin-transform-require-ignore is a Babel plugin that allows you to ignore certain require calls, replacing them with an empty object. This can be useful for ignoring non-JavaScript imports like CSS or image files. While it doesn't offer the same import transformation capabilities as babel-plugin-transform-imports, it can be useful for managing non-code dependencies.
Transforms member style imports:
import { Row, Grid as MyGrid } from 'react-bootstrap';
import { merge } from 'lodash';
...into default style imports:
import Row from 'react-bootstrap/lib/Row';
import MyGrid from 'react-bootstrap/lib/Grid';
import merge from 'lodash/merge';
Note: this plugin is not restricted to the react-bootstrap and lodash libraries. You may use it with any library.
When Babel encounters a member style import such as:
import { Grid, Row, Col } from 'react-bootstrap';
it will generate something similarish to:
var reactBootstrap = require('react-bootstrap');
var Grid = reactBootstrap.Grid;
var Row = reactBootstrap.Row;
var Col = reactBootstrap.Col;
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:
import Grid from 'react-bootstrap/lib/Grid';
import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col';
But, the more pieces we need, the more this sucks. This plugin will allow you to pull in just the pieces you need, without a separate import for each item. Additionally, it can be configured to throw when somebody accidentally writes an import which would cause the entire module to resolve, such as:
import Bootstrap, { Grid } from 'react-bootstrap';
// -- or --
import * as Bootstrap from 'react-bootstrap';
npm install --save-dev babel-plugin-transform-imports
In .babelrc:
{
"plugins": [
["transform-imports", {
"react-bootstrap": {
"transform": "react-bootstrap/lib/${member}",
"preventFullImport": true
},
"lodash": {
"transform": "lodash/${member}",
"preventFullImport": true
}
}]
]
}
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.
.babelrc:
{
"plugins": [
["transform-imports", {
"my-library\/?(((\\w*)?\/?)*)": {
"transform": "my-library/${1}/${member}",
"preventFullImport": true
}
}]
]
}
For instance, the previous configuration will solve properly the next scenarios:
import { MyModule } from 'my-library';
import { App } from 'my-library/components';
import { Header, Footer } from 'my-library/components/App';
becomes:
import MyModule from 'my-library/MyModule';
import App from 'my-library/components/App';
import Header from 'my-library/components/App/Header';
import Footer from 'my-library/components/App/Footer';
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
.
.babelrc:
{
"plugins": [
["transform-imports", {
"my-library": {
"transform": "../../path/to/transform.js",
"preventFullImport": true
}
}]
]
}
/path/to/transform.js:
module.exports = function(importName, matches) {
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
This can be used as a plugin with babel-loader.
webpack.config.js:
module: {
rules: [{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
query: {
plugins: [
[require('babel-plugin-transform-imports'), {
"my-library": {
"transform": function(importName) {
return 'my-library/etc/' + importName.toUpperCase();
},
preventFullImport: true
}
}]
]
}
}
}]
}
Name | Type | Required | Default | Description |
---|---|---|---|---|
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) |
preventFullImport | boolean | no | false | Whether or not to throw when an import is encountered which would cause the entire module to be imported. |
camelCase | boolean | no | false | When set to true, runs ${member} through _.camelCase. |
kebabCase | boolean | no | false | When set to true, runs ${member} through _.kebabCase. |
snakeCase | boolean | no | false | When set to true, runs ${member} through _.snakeCase. |
skipDefaultConversion | boolean | no | false | When set to true, will preserve import { X } syntax instead of converting to import X . |
FAQs
Transforms member style imports (import {x} from 'y') into default style imports (import x from 'y/lib/x')
The npm package babel-plugin-transform-imports receives a total of 186,915 weekly downloads. As such, babel-plugin-transform-imports popularity was classified as popular.
We found that babel-plugin-transform-imports 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.