babel-plugin-import
Advanced tools
Comparing version 1.7.0 to 1.8.0
@@ -8,3 +8,3 @@ "use strict"; | ||
var _path = require("path"); | ||
var _path2 = require("path"); | ||
@@ -59,2 +59,12 @@ var _helperModuleImports = require("@babel/helper-module-imports"); | ||
_createClass(Plugin, [{ | ||
key: "isInGlobalScope", | ||
value: function isInGlobalScope(path, name) { | ||
var _this = this; | ||
var parentPath = path.findParent(function (_path) { | ||
return _path.scope.hasOwnBinding(_this.specified[name]); | ||
}); | ||
return !!parentPath && parentPath.isProgram(); | ||
} | ||
}, { | ||
key: "importMethod", | ||
@@ -67,3 +77,3 @@ value: function importMethod(methodName, file) { | ||
? camel2Underline(methodName) : this.camel2DashComponentName ? camel2Dash(methodName) : methodName; | ||
var path = winPath(this.customName ? this.customName(transformedMethodName) : (0, _path.join)(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line | ||
var path = winPath(this.customName ? this.customName(transformedMethodName) : (0, _path2.join)(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line | ||
); | ||
@@ -79,3 +89,7 @@ this.selectedMethods[methodName] = (0, _helperModuleImports.addDefault)(file.path, path, { | ||
} else if (typeof style === 'function') { | ||
(0, _helperModuleImports.addSideEffect)(file.path, style(path)); | ||
var stylePath = style(path, file); | ||
if (stylePath) { | ||
(0, _helperModuleImports.addSideEffect)(file.path, stylePath); | ||
} | ||
} | ||
@@ -89,3 +103,3 @@ } | ||
value: function buildExpressionHandler(node, props, path, state) { | ||
var _this = this; | ||
var _this2 = this; | ||
@@ -97,4 +111,4 @@ var file = path && path.hub && path.hub.file || state && state.file; | ||
if (_this.specified[node[prop].name]) { | ||
node[prop] = _this.importMethod(_this.specified[node[prop].name], file); // eslint-disable-line | ||
if (_this2.specified[node[prop].name]) { | ||
node[prop] = _this2.importMethod(_this2.specified[node[prop].name], file); // eslint-disable-line | ||
} | ||
@@ -132,3 +146,3 @@ }); | ||
value: function ImportDeclaration(path) { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -145,5 +159,5 @@ var node = path.node; // path maybe removed by prev instances. | ||
if (types.isImportSpecifier(spec)) { | ||
_this2.specified[spec.local.name] = spec.imported.name; | ||
_this3.specified[spec.local.name] = spec.imported.name; | ||
} else { | ||
_this2.libraryObjs[spec.local.name] = true; | ||
_this3.libraryObjs[spec.local.name] = true; | ||
} | ||
@@ -157,3 +171,3 @@ }); | ||
value: function CallExpression(path, state) { | ||
var _this3 = this; | ||
var _this4 = this; | ||
@@ -174,4 +188,4 @@ var node = path.node; | ||
if (_this3.specified[argName] && path.scope.hasBinding(argName) && path.scope.getBinding(argName).path.type === 'ImportSpecifier') { | ||
return _this3.importMethod(_this3.specified[argName], file); | ||
if (_this4.specified[argName] && path.scope.hasBinding(argName) && path.scope.getBinding(argName).path.type === 'ImportSpecifier') { | ||
return _this4.importMethod(_this4.specified[argName], file); | ||
} | ||
@@ -254,3 +268,3 @@ | ||
if (node.argument && types.isIdentifier(node.argument) && this.specified[node.argument.name]) { | ||
if (node.argument && types.isIdentifier(node.argument) && this.specified[node.argument.name] && this.isInGlobalScope(path, node.argument.name)) { | ||
node.argument = this.importMethod(node.argument.name, file); | ||
@@ -257,0 +271,0 @@ } |
{ | ||
"name": "babel-plugin-import", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"description": "Component modular import plugin for babel.", | ||
@@ -46,6 +46,9 @@ "repository": { | ||
"presets": [ | ||
["umi", { | ||
"commonjs": true, | ||
"disableTransform": true | ||
}] | ||
[ | ||
"umi", | ||
{ | ||
"commonjs": true, | ||
"disableTransform": true | ||
} | ||
] | ||
] | ||
@@ -52,0 +55,0 @@ }, |
@@ -91,2 +91,10 @@ # babel-plugin-import | ||
{ | ||
"libraryName": "lodash", | ||
"libraryDirectory": "", | ||
"camel2DashComponentName": false, // default: true | ||
} | ||
``` | ||
```javascript | ||
{ | ||
"libraryName": "material-ui", | ||
@@ -98,3 +106,3 @@ "libraryDirectory": "components", // default: lib | ||
`options` can be an array. | ||
~`options` can be an array.~ It's not available in bable@7+ | ||
@@ -115,3 +123,14 @@ For Example: | ||
``` | ||
`Options` can't be an array in babel@7+, but you can add plugins with name to support multiple dependencies. | ||
For Example: | ||
```javascrit | ||
// .babelrc | ||
"plugins": [ | ||
["import", { "libraryName": "antd", "libraryDirectory": "lib"}, "ant"], | ||
["import", { "libraryName": "ant-mobile", "libraryDirectory": "lib"}, "ant-mobile"] | ||
] | ||
``` | ||
### style | ||
@@ -126,6 +145,24 @@ | ||
e.g. | ||
- `["import", { "libraryName": "antd", "style": (name) => `${name}/style/2x` }]`: import js and css modularly & css file path is `ComponentName/style/2x` | ||
- ``["import", { "libraryName": "antd", "style": (name) => `${name}/style/2x` }]``: import js and css modularly & css file path is `ComponentName/style/2x` | ||
If a component has no style, you can use the `style` function to return a `false` and the style will be ignored. | ||
e.g. | ||
```js | ||
[ | ||
"import", | ||
{ | ||
"libraryName": "antd", | ||
"style": (name: string, file: Object) => { | ||
if(name === 'antd/lib/utils'){ | ||
return false; | ||
} | ||
return `${name}/style/2x`; | ||
} | ||
} | ||
] | ||
``` | ||
### Note | ||
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). |
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
19592
338
165