babel-plugin-transform-taroapi 4.1.12-alpha.0 → 4.1.12-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -122,9 +122,34 @@ const plugin = function (babel) {
122
122
  }
123
123
  },
124
124
  CallExpression(ast) {
125
+ const callee = ast.node.callee;
126
+ // 对存在命名空间的 API 支持 tree-shaking:Taro.xx.yy -> xx_yy
127
+ if (t.isMemberExpression(callee) && t.isMemberExpression(callee.object)) {
128
+ const inner = callee.object;
129
+ const isTaroNamespace = t.isIdentifier(inner.object, { name: taroName });
130
+ if (isTaroNamespace) {
131
+ const namespaceName = t.isIdentifier(inner.property) ? inner.property.name : (t.isStringLiteral(inner.property) ? inner.property.value : null);
132
+ const methodName = t.isIdentifier(callee.property) ? callee.property.name : (t.isStringLiteral(callee.property) ? callee.property.value : null);
133
+ if (namespaceName && methodName) {
134
+ const flatName = `${namespaceName}_${methodName}`;
135
+ if (this.apis.has(flatName)) {
136
+ let identifier;
137
+ if (invokedApis.has(flatName)) {
138
+ identifier = t.identifier(invokedApis.get(flatName));
139
+ }
140
+ else {
141
+ const newName = ast.scope.generateUid(flatName);
142
+ invokedApis.set(flatName, newName);
143
+ identifier = t.identifier(newName);
144
+ }
145
+ ast.node.callee = identifier;
146
+ return;
147
+ }
148
+ }
149
+ }
150
+ }
125
151
  if (!ast.scope.hasReference(this.canIUse))
126
152
  return;
127
- const callee = ast.node.callee;
128
153
  if (t.isMemberExpression(callee) && t.isIdentifier(callee.object, { name: taroName })) {
129
154
  let propertyName = null;
130
155
  let propName = 'name';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-transform-taroapi",
3
- "version": "4.1.12-alpha.0",
3
+ "version": "4.1.12-alpha.1",
4
4
  "author": "O2Team",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
package/src/index.ts CHANGED
@@ -144,8 +144,34 @@ const plugin = function (babel: typeof BabelCore): BabelCore.PluginObj<IState> {
144
144
  }
145
145
  },
146
146
  CallExpression (ast: BabelCore.NodePath<any>) {
147
- if (!ast.scope.hasReference(this.canIUse)) return
148
147
  const callee = ast.node.callee
148
+
149
+ // 对存在命名空间的 API 支持 tree-shaking:Taro.xx.yy -> xx_yy
150
+ if (t.isMemberExpression(callee) && t.isMemberExpression(callee.object)) {
151
+ const inner = callee.object
152
+ const isTaroNamespace = t.isIdentifier(inner.object, { name: taroName })
153
+ if (isTaroNamespace) {
154
+ const namespaceName = t.isIdentifier(inner.property) ? inner.property.name : (t.isStringLiteral(inner.property) ? inner.property.value : null)
155
+ const methodName = t.isIdentifier(callee.property) ? callee.property.name : (t.isStringLiteral(callee.property) ? callee.property.value : null)
156
+ if (namespaceName && methodName) {
157
+ const flatName = `${namespaceName}_${methodName}`
158
+ if (this.apis.has(flatName)) {
159
+ let identifier: BabelCore.types.Identifier
160
+ if (invokedApis.has(flatName)) {
161
+ identifier = t.identifier(invokedApis.get(flatName)!)
162
+ } else {
163
+ const newName = ast.scope.generateUid(flatName)
164
+ invokedApis.set(flatName, newName)
165
+ identifier = t.identifier(newName)
166
+ }
167
+ ast.node.callee = identifier as any
168
+ return
169
+ }
170
+ }
171
+ }
172
+ }
173
+
174
+ if (!ast.scope.hasReference(this.canIUse)) return
149
175
  if (t.isMemberExpression(callee) && t.isIdentifier(callee.object, { name: taroName })) {
150
176
  let propertyName: string | null = null
151
177
  let propName = 'name'