babel-plugin-hylid-bridge 2.2.0 → 2.4.0-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/CHANGELOG.md CHANGED
@@ -3,6 +3,38 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [2.4.0-alpha.1](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.4.0-alpha.0...v2.4.0-alpha.1) (2022-08-15)
7
+
8
+
9
+ ### Features
10
+
11
+ * 不修改全局的 my, 新建本地变量
12
+
13
+
14
+ ## [2.4.0-alpha.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.1.0...v2.4.0-alpha.0) (2022-08-12)
15
+
16
+
17
+ ### Features
18
+
19
+ * 调整 eslint 配置* 根据源码注入 hylid-bridge 相应模块* 固定三方依赖版本号
20
+
21
+ ### Bug Fixes
22
+
23
+ * 依赖* build error* split the target
24
+
25
+
26
+ ## [2.3.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.1.0...v2.3.0) (2022-08-05)
27
+
28
+
29
+ ### Features
30
+
31
+ * 调整 eslint 配置
32
+
33
+ ### Bug Fixes
34
+
35
+ * 依赖* build error* split the target
36
+
37
+
6
38
  ## [2.2.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.1.0...v2.2.0) (2022-07-08)
7
39
 
8
40
 
package/lib/index.d.ts CHANGED
@@ -3,5 +3,5 @@ import { PluginPass } from '@babel/core';
3
3
  interface IState extends PluginPass {
4
4
  usedJSAPIList: 'all' | string[];
5
5
  }
6
- declare const _default: (_: typeof babelCore, opts?: Record<string, any> | undefined) => babelCore.PluginObj<IState>;
6
+ declare const _default: (_: typeof babelCore, opts?: Record<string, any>) => babelCore.PluginObj<IState>;
7
7
  export default _default;
package/lib/index.js CHANGED
@@ -34,13 +34,34 @@ var identifierPrefix = '__hylid_bridge__';
34
34
  var libraryName = 'hylid-bridge';
35
35
  var libraryDir = (0, _getLibraryDir.default)();
36
36
 
37
+ function snakeCaseTarget(value) {
38
+ if (value === 'H5') {
39
+ return 'h5';
40
+ }
41
+
42
+ return _lodash.default.snakeCase(value);
43
+ }
44
+
45
+ function isTargetSupported(target) {
46
+ var libraryDir = (0, _getLibraryDir.default)();
47
+ return ['MpWeb', 'Mp', 'Web'].some(function (prefix) {
48
+ if (target.startsWith(prefix)) {
49
+ var targetClientDir = _path.default.resolve(libraryDir, 'lib/clients', snakeCaseTarget(prefix), snakeCaseTarget(target.replace(prefix, '')));
50
+
51
+ return _fsExtra.default.existsSync(targetClientDir);
52
+ }
53
+
54
+ return false;
55
+ });
56
+ }
57
+
37
58
  function getTargets(customTargets) {
38
59
  var targets = customTargets || (process.env.HYLID_BRIDGE_TARGETS ? process.env.HYLID_BRIDGE_TARGETS.split(',') : []);
39
- return targets;
60
+ return targets.filter(isTargetSupported);
40
61
  }
41
62
 
42
63
  function isApiExists(name, target) {
43
- var targetSplit = target.split('|');
64
+ var targetSplit = splitTarget(target);
44
65
 
45
66
  if (targetSplit.length !== 2) {
46
67
  throw new Error("Wrong target configuration: ".concat(target, "."));
@@ -49,8 +70,36 @@ function isApiExists(name, target) {
49
70
  return _fsExtra.default.existsSync(_path.default.resolve(libraryDir, "./lib/clients/".concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name, ".js")));
50
71
  }
51
72
 
73
+ function getUserAppEnvPath(appEnvPath) {
74
+ var realAppEnvPath = appEnvPath !== null && appEnvPath !== void 0 ? appEnvPath : _path.default.resolve(process.cwd(), './src/appEnv.ts');
75
+
76
+ if (!_fsExtra.default.existsSync(realAppEnvPath)) {
77
+ utils.log("Can not find the appEnv module at the path: ".concat(realAppEnvPath, "."));
78
+ return;
79
+ }
80
+
81
+ return realAppEnvPath;
82
+ }
83
+
84
+ function splitTarget(target) {
85
+ var prefixList = ['MpWeb', 'Mp', 'Web'];
86
+
87
+ for (var i = 0, il = prefixList.length; i < il; i++) {
88
+ var prefix = prefixList[i];
89
+
90
+ if (target.startsWith(prefix)) {
91
+ return [snakeCaseTarget(prefix), snakeCaseTarget(target.replace(prefix, ''))];
92
+ }
93
+ }
94
+
95
+ throw new Error("Invalid bridge target: ".concat(target, "."));
96
+ }
97
+
98
+ var myLocalName = "".concat(identifierPrefix, "my__");
99
+
52
100
  var _default = function _default(_, opts) {
53
101
  var targets = getTargets(opts === null || opts === void 0 ? void 0 : opts.targets);
102
+ var userAppEnvPath = getUserAppEnvPath(opts === null || opts === void 0 ? void 0 : opts.appEnvPath);
54
103
  return {
55
104
  name: 'babel-plugin-hylid-bridge',
56
105
  visitor: {
@@ -64,7 +113,11 @@ var _default = function _default(_, opts) {
64
113
  return;
65
114
  }
66
115
 
116
+ var newNode = babelTypes.cloneNode(path.node, true, true);
117
+ newNode.object = babelTypes.identifier(myLocalName);
118
+
67
119
  if (state.usedJSAPIList === 'all') {
120
+ path.replaceWith(newNode);
68
121
  return;
69
122
  }
70
123
 
@@ -76,7 +129,7 @@ var _default = function _default(_, opts) {
76
129
  var name = propertyName;
77
130
 
78
131
  if (targets.every(function (target) {
79
- var targetSplit = target.split('|');
132
+ var targetSplit = splitTarget(target);
80
133
 
81
134
  if (!isApiExists(name, target)) {
82
135
  utils.warn("Can not find the API `".concat(name, "` in `").concat(_path.default.resolve(libraryDir, './lib/clients/' + targetSplit[0] + '/' + targetSplit[1]), "`. Fallback to import all API codes of ").concat(target, ". Current file path: `").concat(_this.file.opts.filename, "`."));
@@ -90,6 +143,8 @@ var _default = function _default(_, opts) {
90
143
  state.usedJSAPIList = 'all';
91
144
  }
92
145
  }
146
+
147
+ path.replaceWith(newNode);
93
148
  },
94
149
  Program: {
95
150
  exit: function exit(path, state) {
@@ -104,11 +159,24 @@ var _default = function _default(_, opts) {
104
159
  return;
105
160
  }
106
161
 
107
- var nodes = [babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(appEnvLocalName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/appEnv")))];
162
+ var innerAppEnvLocalName = "".concat(identifierPrefix, "innerAppEnv__");
163
+ var userAppEnvLocalName = "".concat(identifierPrefix, "userAppEnv__");
164
+ var nodes = [babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(innerAppEnvLocalName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/appEnv")))];
165
+
166
+ if (userAppEnvPath) {
167
+ var relativePath = _path.default.relative(_path.default.dirname(this.filename), userAppEnvPath.replace(/\.ts$/, ''));
168
+
169
+ nodes.push(babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(userAppEnvLocalName))], babelTypes.stringLiteral(relativePath.startsWith('../') ? relativePath : "./".concat(relativePath))));
170
+ } else {
171
+ nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(userAppEnvLocalName), babelTypes.objectExpression([]))]));
172
+ }
173
+
174
+ var objectSpreadName = "".concat(identifierPrefix, "objectSpread__");
175
+ nodes.push(babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(objectSpreadName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/objectSpread"))), babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(appEnvLocalName), babelTypes.callExpression(babelTypes.identifier(objectSpreadName), [babelTypes.identifier(innerAppEnvLocalName), babelTypes.identifier(userAppEnvLocalName)]))]));
108
176
 
109
177
  if (usedJSAPIList === 'all') {
110
178
  nodes.push.apply(nodes, (0, _toConsumableArray2.default)(targets.map(function (target) {
111
- var targetSplit = target.split('|');
179
+ var targetSplit = splitTarget(target);
112
180
  return babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__")))], babelTypes.stringLiteral("".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/index")));
113
181
  })));
114
182
 
@@ -118,15 +186,15 @@ var _default = function _default(_, opts) {
118
186
  }
119
187
 
120
188
  var target = targetList[0];
121
- var targetSplit = target.split('|');
122
- return babelTypes.ifStatement(babelTypes.identifier("".concat(appEnvLocalName, ".").concat(_lodash.default.camelCase("is_".concat(targetSplit[0], "_").concat(targetSplit[1])))), babelTypes.blockStatement([babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier('my'), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__"))))]), buildElseIf(targetList.slice(1)));
189
+ var targetSplit = splitTarget(target);
190
+ return babelTypes.ifStatement(babelTypes.identifier("".concat(appEnvLocalName, ".").concat(_lodash.default.camelCase("is_".concat(targetSplit[0], "_").concat(targetSplit[1])))), babelTypes.blockStatement([babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__"))))]), buildElseIf(targetList.slice(1)));
123
191
  };
124
192
 
125
- nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier('my'))])); // Do not generate the `if statement` when there is just one target.
193
+ nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(myLocalName))])); // Do not generate the `if statement` when there is just one target.
126
194
 
127
195
  if (targets.length === 1) {
128
- var targetSplit = targets[0].split('|');
129
- nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier('my'), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__")))));
196
+ var targetSplit = splitTarget(targets[0]);
197
+ nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__")))));
130
198
  } else {
131
199
  var elseIfNode = buildElseIf(targets);
132
200
  elseIfNode && nodes.push(elseIfNode);
@@ -136,7 +204,7 @@ var _default = function _default(_, opts) {
136
204
 
137
205
  normalizedJSAPIList.forEach(function (name) {
138
206
  targets.forEach(function (target) {
139
- var targetSplit = target.split('|');
207
+ var targetSplit = splitTarget(target);
140
208
  var localName = "".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__");
141
209
  var n = babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(localName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name)));
142
210
  nodes.push(n);
@@ -149,22 +217,22 @@ var _default = function _default(_, opts) {
149
217
  }
150
218
 
151
219
  var target = targetList[0];
152
- var targetSplit = target.split('|');
220
+ var targetSplit = splitTarget(target);
153
221
  var propertyList = normalizedJSAPIList.map(function (name) {
154
222
  return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__")));
155
223
  });
156
- return babelTypes.ifStatement(babelTypes.identifier("".concat(appEnvLocalName, ".").concat(_lodash.default.camelCase("is_".concat(targetSplit[0], "_").concat(targetSplit[1])))), babelTypes.blockStatement([babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier('my'), babelTypes.objectExpression(propertyList)))]), _buildElseIf(targetList.slice(1)));
224
+ return babelTypes.ifStatement(babelTypes.identifier("".concat(appEnvLocalName, ".").concat(_lodash.default.camelCase("is_".concat(targetSplit[0], "_").concat(targetSplit[1])))), babelTypes.blockStatement([babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.objectExpression(propertyList)))]), _buildElseIf(targetList.slice(1)));
157
225
  };
158
226
 
159
- nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier('my'))])); // Do not generate the `if statement` when there is just one target.
227
+ nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(myLocalName))])); // Do not generate the `if statement` when there is just one target.
160
228
 
161
229
  if (targets.length === 1) {
162
- var _targetSplit = targets[0].split('|');
230
+ var _targetSplit = splitTarget(targets[0]);
163
231
 
164
232
  var propertyList = normalizedJSAPIList.map(function (name) {
165
233
  return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(_targetSplit[0], "_").concat(_targetSplit[1], "_").concat(name, "__")));
166
234
  });
167
- nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier('my'), babelTypes.objectExpression(propertyList))));
235
+ nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.objectExpression(propertyList))));
168
236
  } else {
169
237
  var _elseIfNode = _buildElseIf(targets);
170
238
 
@@ -175,6 +243,10 @@ var _default = function _default(_, opts) {
175
243
  if (nodes.length) {
176
244
  path.unshiftContainer('body', nodes);
177
245
  }
246
+
247
+ if (targets.length && (usedJSAPIList === 'all' || usedJSAPIList.length)) {
248
+ utils.log("Successfully inject the Hylid-bridge into ".concat(this.filename, " to support the targets: ").concat(targets.join(','), "."));
249
+ }
178
250
  }
179
251
  }
180
252
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-hylid-bridge",
3
- "version": "2.2.0",
3
+ "version": "2.4.0-alpha.1",
4
4
  "description": "babel-plugin-hylid-bridge",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -13,14 +13,14 @@
13
13
  "keywords": [],
14
14
  "license": "MIT",
15
15
  "dependencies": {
16
- "@babel/core": "^7.15.6",
17
- "@babel/runtime": "^7.18.6",
18
- "@babel/types": "^7.15.6",
19
- "@types/fs-extra": "^9.0.13",
20
- "@types/lodash": "^4.14.176",
21
- "chalk": "^4.1.2",
22
- "fs-extra": "^10.0.0",
23
- "lodash": "^4.17.21"
16
+ "@babel/core": "7.18.2",
17
+ "@babel/runtime": "7.18.9",
18
+ "@babel/types": "7.18.10",
19
+ "@types/fs-extra": "9.0.13",
20
+ "@types/lodash": "4.14.182",
21
+ "chalk": "3.0.0",
22
+ "fs-extra": "10.1.0",
23
+ "lodash": "4.17.21"
24
24
  },
25
25
  "devDependencies": {
26
26
  "gulp": "4.0.2",