babel-plugin-transform-import
fork of babel-plugin-transform-imports
introduced style
option for importing css file.
Transforms member style imports:
;;
...into default style imports:
;;;
...or when style
option is provided, style files will be imported as well:
;;;;
Note: this plugin is not restricted to the react-bootstrap and lodash libraries. You may use it with any library.
That's stupid, why would you do that?
When Babel encounters a member style import such as:
;
it will generate something similarish to:
var reactBootstrap = ;var Grid = reactBootstrapGrid;var Row = reactBootstrapRow;var Col = reactBootstrapCol;
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:
;;;
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:
;// -- or --;
Installation
npm install --save-dev babel-plugin-transform-import
Usage
In .babelrc:
Advanced Transformations
In cases where the provided default string replacement transformation is not
sufficient (for example, needing to execute a RegExp on the import name), you
may instead 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:
/path/to/transform.js:
module { return 'my-library/etc/' + importName;};
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
This can be used as a plugin with babel-loader.
webpack.config.js:
module: rules: test: /\.js$/ exclude: // use: loader: 'babel-loader' query: plugins: "my-library": { return 'my-library/etc/' + importName; } preventFullImport: true
Options
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 member, aka Grid/Row/Col/etc. Alternatively, pass a path to a .js file which exports a function to process the transform (see Advanced Transformations) |
style |
string |
no | undefined |
The style file to be imported along with the transformed module. ${member} will be replaced with the member. This option is useful when you extract the style of your module into a single css file. |
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 . |