Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-import

Package Overview
Dependencies
Maintainers
4
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-import - npm Package Compare versions

Comparing version 1.11.2 to 1.12.0

4

lib/index.js

@@ -93,4 +93,4 @@ "use strict";

for (var _i = 0, _methods = methods; _i < _methods.length; _i++) {
const method = _methods[_i];
for (var _i = 0; _i < methods.length; _i++) {
const method = methods[_i];

@@ -97,0 +97,0 @@ ret.visitor[method] = function () {

@@ -8,18 +8,12 @@ "use strict";

var _path2 = require("path");
var _path = require("path");
var _helperModuleImports = require("@babel/helper-module-imports");
function camel2Dash(_str) {
function transCamel(_str, symbol) {
const str = _str[0].toLowerCase() + _str.substr(1);
return str.replace(/([A-Z])/g, $1 => `-${$1.toLowerCase()}`);
return str.replace(/([A-Z])/g, $1 => `${symbol}${$1.toLowerCase()}`);
}
function camel2Underline(_str) {
const str = _str[0].toLowerCase() + _str.substr(1);
return str.replace(/([A-Z])/g, $1 => `_${$1.toLowerCase()}`);
}
function winPath(path) {

@@ -29,2 +23,13 @@ return path.replace(/\\/g, '/');

function normalizeCustomName(originCustomName) {
// If set to a string, treat it as a JavaScript source file path.
if (typeof originCustomName === 'string') {
const customNameExports = require(originCustomName);
return typeof customNameExports === 'function' ? customNameExports : customNameExports.default;
}
return originCustomName;
}
class Plugin {

@@ -38,3 +43,3 @@ constructor(libraryName, libraryDirectory, style, camel2DashComponentName, camel2UnderlineComponentName, fileName, customName, transformToDefaultImport, types, index = 0) {

this.fileName = fileName || '';
this.customName = customName;
this.customName = normalizeCustomName(customName);
this.transformToDefaultImport = typeof transformToDefaultImport === 'undefined' ? true : transformToDefaultImport;

@@ -53,7 +58,2 @@ this.types = types;

isInGlobalScope(path, name, pluginState) {
const parentPath = path.findParent(_path => _path.scope.hasOwnBinding(pluginState.specified[name]));
return !!parentPath && parentPath.isProgram();
}
importMethod(methodName, file, pluginState) {

@@ -64,4 +64,4 @@ if (!pluginState.selectedMethods[methodName]) {

const transformedMethodName = this.camel2UnderlineComponentName // eslint-disable-line
? camel2Underline(methodName) : this.camel2DashComponentName ? camel2Dash(methodName) : methodName;
const path = winPath(this.customName ? this.customName(transformedMethodName) : (0, _path2.join)(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
? transCamel(methodName, '_') : this.camel2DashComponentName ? transCamel(methodName, '-') : methodName;
const path = winPath(this.customName ? this.customName(transformedMethodName) : (0, _path.join)(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
);

@@ -96,3 +96,3 @@ pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line

if (pluginState.specified[node[prop].name]) {
if (pluginState.specified[node[prop].name] && types.isImportSpecifier(path.scope.getBinding(node[prop].name).path)) {
node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line

@@ -228,10 +228,4 @@ }

ReturnStatement(path, state) {
const types = this.types;
const file = path && path.hub && path.hub.file || state && state.file;
const node = path.node;
const pluginState = this.getPluginState(state);
if (node.argument && types.isIdentifier(node.argument) && pluginState.specified[node.argument.name] && this.isInGlobalScope(path, node.argument.name, pluginState)) {
node.argument = this.importMethod(node.argument.name, file, pluginState);
}
this.buildExpressionHandler(node, ['argument'], path, state);
}

@@ -238,0 +232,0 @@

{
"name": "babel-plugin-import",
"version": "1.11.2",
"version": "1.12.0",
"description": "Component modular import plugin for babel.",

@@ -16,3 +16,3 @@ "repository": {

"coveralls": "cat ./coverage/lcov.info | coveralls",
"prepublish": "npm run build"
"prepack": "npm run build"
},

@@ -19,0 +19,0 @@ "pre-commit": [

@@ -29,3 +29,3 @@ # babel-plugin-import

↓ ↓ ↓ ↓ ↓ ↓
var _button = require('antd/lib/button');

@@ -42,3 +42,3 @@ ReactDOM.render(<_button>xxxx</_button>);

↓ ↓ ↓ ↓ ↓ ↓
var _button = require('antd/lib/button');

@@ -56,3 +56,3 @@ require('antd/lib/button/style/css');

↓ ↓ ↓ ↓ ↓ ↓
var _button = require('antd/lib/button');

@@ -63,3 +63,3 @@ require('antd/lib/button/style');

Note : with `style: true` css source files are imported and optimizations can be done during compilation time. With `style: "css"`, pre bundled css files are imported as they are.
Note : with `style: true` css source files are imported and optimizations can be done during compilation time. With `style: "css"`, pre bundled css files are imported as they are.

@@ -111,3 +111,3 @@ `style: true` can reduce the bundle size significantly, depending on your usage of the library.

For Example:
For Example:

@@ -146,3 +146,3 @@ ```javascript

e.g.
e.g.
- ``["import", { "libraryName": "antd", "style": (name) => `${name}/style/2x` }]``: import js and css modularly & css file path is `ComponentName/style/2x`

@@ -152,8 +152,8 @@

e.g.
e.g.
```js
[
"import",
{
"libraryName": "antd",
"import",
{
"libraryName": "antd",
"style": (name: string, file: Object) => {

@@ -193,5 +193,5 @@ if(name === 'antd/lib/utils'){

[
"import",
{
"libraryName": "antd",
"import",
{
"libraryName": "antd",
"customName": (name: string) => {

@@ -215,2 +215,24 @@ if (name === 'TimePicker'){

In some cases, the transformer may serialize the configuration object. If we set the `customName` to a function, it will lost after the serialization.
So we also support specifying the customName with a JavaScript source file path:
```js
[
"import",
{
"libraryName": "antd",
"customName": require('path').resolve(__dirname, './customName.js')
}
]
```
The `customName.js` looks like this:
```js
module.exports = function customName(name) {
return `antd/lib/${name}`;
};
```
#### transformToDefaultImport

@@ -222,2 +244,2 @@

babel-plugin-import will not work properly if you add the library to the webpack config [vendor](https://webpack.github.io/docs/code-splitting.html#split-app-and-vendor-code).
babel-plugin-import will not work properly if you add the library to the webpack config [vendor](https://webpack.github.io/docs/code-splitting.html#split-app-and-vendor-code).
import { join } from 'path';
import { addSideEffect, addDefault, addNamed } from '@babel/helper-module-imports';
function camel2Dash(_str) {
function transCamel(_str, symbol) {
const str = _str[0].toLowerCase() + _str.substr(1);
return str.replace(/([A-Z])/g, ($1) => `-${$1.toLowerCase()}`);
return str.replace(/([A-Z])/g, ($1) => `${symbol}${$1.toLowerCase()}`);
}
function camel2Underline(_str) {
const str = _str[0].toLowerCase() + _str.substr(1);
return str.replace(/([A-Z])/g, ($1) => `_${$1.toLowerCase()}`);
}
function winPath(path) {

@@ -18,2 +13,13 @@ return path.replace(/\\/g, '/');

function normalizeCustomName(originCustomName) {
// If set to a string, treat it as a JavaScript source file path.
if (typeof originCustomName === 'string') {
const customNameExports = require(originCustomName);
return typeof customNameExports === 'function'
? customNameExports : customNameExports.default;
}
return originCustomName;
}
export default class Plugin {

@@ -42,3 +48,3 @@ constructor(

this.fileName = fileName || '';
this.customName = customName;
this.customName = normalizeCustomName(customName);
this.transformToDefaultImport = typeof transformToDefaultImport === 'undefined'

@@ -58,8 +64,2 @@ ? true

isInGlobalScope(path, name, pluginState) {
const parentPath = path.findParent((_path) =>
_path.scope.hasOwnBinding(pluginState.specified[name]));
return !!parentPath && parentPath.isProgram();
}
importMethod(methodName, file, pluginState) {

@@ -70,5 +70,5 @@ if (!pluginState.selectedMethods[methodName]) {

const transformedMethodName = this.camel2UnderlineComponentName // eslint-disable-line
? camel2Underline(methodName)
? transCamel(methodName, '_')
: this.camel2DashComponentName
? camel2Dash(methodName)
? transCamel(methodName, '-')
: methodName;

@@ -101,3 +101,6 @@ const path = winPath(

if (!types.isIdentifier(node[prop])) return;
if (pluginState.specified[node[prop].name]) {
if (
pluginState.specified[node[prop].name] &&
types.isImportSpecifier(path.scope.getBinding(node[prop].name).path)
) {
node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line

@@ -235,12 +238,4 @@ }

ReturnStatement(path, state) {
const types = this.types;
const file = (path && path.hub && path.hub.file) || (state && state.file);
const { node } = path;
const pluginState = this.getPluginState(state);
if (node.argument && types.isIdentifier(node.argument) &&
pluginState.specified[node.argument.name] &&
this.isInGlobalScope(path, node.argument.name, pluginState)) {
node.argument = this.importMethod(node.argument.name, file, pluginState);
}
this.buildExpressionHandler(node, ['argument'], path, state);
}

@@ -247,0 +242,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc