babel-plugin-hylid-bridge 2.3.0 → 2.4.0-alpha.2

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,34 @@
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.2](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.4.0-alpha.1...v2.4.0-alpha.2) (2022-08-16)
7
+
8
+
9
+ ### Features
10
+
11
+ * 避免频繁打印日志* 适配 Remix 中会编译所有 node_modules 下模块的逻辑
12
+
13
+
14
+ ## [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)
15
+
16
+
17
+ ### Features
18
+
19
+ * 不修改全局的 my, 新建本地变量
20
+
21
+
22
+ ## [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)
23
+
24
+
25
+ ### Features
26
+
27
+ * 调整 eslint 配置* 根据源码注入 hylid-bridge 相应模块* 固定三方依赖版本号
28
+
29
+ ### Bug Fixes
30
+
31
+ * 依赖* build error* split the target
32
+
33
+
6
34
  ## [2.3.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.1.0...v2.3.0) (2022-08-05)
7
35
 
8
36
 
@@ -17,11 +17,11 @@ function getLibraryDir() {
17
17
  libraryDir = process.env.HYLID_BRIDGE_PACKAGE_DIR || require.resolve('hylid-bridge/package.json', {
18
18
  paths: [cwd]
19
19
  }).replace('package.json', '');
20
+ (0, _utils.log)("Find the hylid-bridge package: ".concat(libraryDir));
20
21
  } catch (e) {
21
22
  (0, _utils.error)("Can not resolve the hylid-bridge package from the directory: ".concat(cwd, "."), e);
22
23
  }
23
24
  }
24
25
 
25
- (0, _utils.log)("Find the hylid-bridge package: ".concat(libraryDir));
26
26
  return libraryDir;
27
27
  }
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,22 +70,56 @@ 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: {
57
106
  MemberExpression: function MemberExpression(path, state) {
58
- var _this = this;
107
+ var _this$filename,
108
+ _this = this;
59
109
 
60
110
  var objectName = path.node.object.name;
61
111
  var propertyName = path.node.property.name;
62
112
 
63
- if (!targets || !targets.length || objectName !== 'my' || path.scope.hasBinding('my')) {
113
+ if ( // Remix 中会编译所有 node_modules 下的模块
114
+ (_this$filename = this.filename) !== null && _this$filename !== void 0 && _this$filename.includes("".concat(_path.default.sep, "node_modules").concat(_path.default.sep, "hylid-bridge").concat(_path.default.sep)) || !targets || !targets.length || objectName !== 'my' || path.scope.hasBinding('my')) {
64
115
  return;
65
116
  }
66
117
 
118
+ var newNode = babelTypes.cloneNode(path.node, true, true);
119
+ newNode.object = babelTypes.identifier(myLocalName);
120
+
67
121
  if (state.usedJSAPIList === 'all') {
122
+ path.replaceWith(newNode);
68
123
  return;
69
124
  }
70
125
 
@@ -76,7 +131,7 @@ var _default = function _default(_, opts) {
76
131
  var name = propertyName;
77
132
 
78
133
  if (targets.every(function (target) {
79
- var targetSplit = target.split('|');
134
+ var targetSplit = splitTarget(target);
80
135
 
81
136
  if (!isApiExists(name, target)) {
82
137
  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 +145,8 @@ var _default = function _default(_, opts) {
90
145
  state.usedJSAPIList = 'all';
91
146
  }
92
147
  }
148
+
149
+ path.replaceWith(newNode);
93
150
  },
94
151
  Program: {
95
152
  exit: function exit(path, state) {
@@ -104,11 +161,24 @@ var _default = function _default(_, opts) {
104
161
  return;
105
162
  }
106
163
 
107
- var nodes = [babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(appEnvLocalName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/appEnv")))];
164
+ var innerAppEnvLocalName = "".concat(identifierPrefix, "innerAppEnv__");
165
+ var userAppEnvLocalName = "".concat(identifierPrefix, "userAppEnv__");
166
+ var nodes = [babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(innerAppEnvLocalName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/appEnv")))];
167
+
168
+ if (userAppEnvPath) {
169
+ var relativePath = _path.default.relative(_path.default.dirname(this.filename), userAppEnvPath.replace(/\.ts$/, ''));
170
+
171
+ nodes.push(babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(userAppEnvLocalName))], babelTypes.stringLiteral(relativePath.startsWith('../') ? relativePath : "./".concat(relativePath))));
172
+ } else {
173
+ nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(userAppEnvLocalName), babelTypes.objectExpression([]))]));
174
+ }
175
+
176
+ var objectSpreadName = "".concat(identifierPrefix, "objectSpread__");
177
+ 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
178
 
109
179
  if (usedJSAPIList === 'all') {
110
180
  nodes.push.apply(nodes, (0, _toConsumableArray2.default)(targets.map(function (target) {
111
- var targetSplit = target.split('|');
181
+ var targetSplit = splitTarget(target);
112
182
  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
183
  })));
114
184
 
@@ -118,15 +188,15 @@ var _default = function _default(_, opts) {
118
188
  }
119
189
 
120
190
  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)));
191
+ var targetSplit = splitTarget(target);
192
+ 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
193
  };
124
194
 
125
- nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier('my'))])); // Do not generate the `if statement` when there is just one target.
195
+ nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(myLocalName))])); // Do not generate the `if statement` when there is just one target.
126
196
 
127
197
  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__")))));
198
+ var targetSplit = splitTarget(targets[0]);
199
+ nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__")))));
130
200
  } else {
131
201
  var elseIfNode = buildElseIf(targets);
132
202
  elseIfNode && nodes.push(elseIfNode);
@@ -136,7 +206,7 @@ var _default = function _default(_, opts) {
136
206
 
137
207
  normalizedJSAPIList.forEach(function (name) {
138
208
  targets.forEach(function (target) {
139
- var targetSplit = target.split('|');
209
+ var targetSplit = splitTarget(target);
140
210
  var localName = "".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__");
141
211
  var n = babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(localName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name)));
142
212
  nodes.push(n);
@@ -149,22 +219,22 @@ var _default = function _default(_, opts) {
149
219
  }
150
220
 
151
221
  var target = targetList[0];
152
- var targetSplit = target.split('|');
222
+ var targetSplit = splitTarget(target);
153
223
  var propertyList = normalizedJSAPIList.map(function (name) {
154
224
  return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__")));
155
225
  });
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)));
226
+ 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
227
  };
158
228
 
159
- nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier('my'))])); // Do not generate the `if statement` when there is just one target.
229
+ nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(myLocalName))])); // Do not generate the `if statement` when there is just one target.
160
230
 
161
231
  if (targets.length === 1) {
162
- var _targetSplit = targets[0].split('|');
232
+ var _targetSplit = splitTarget(targets[0]);
163
233
 
164
234
  var propertyList = normalizedJSAPIList.map(function (name) {
165
235
  return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(_targetSplit[0], "_").concat(_targetSplit[1], "_").concat(name, "__")));
166
236
  });
167
- nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier('my'), babelTypes.objectExpression(propertyList))));
237
+ nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.objectExpression(propertyList))));
168
238
  } else {
169
239
  var _elseIfNode = _buildElseIf(targets);
170
240
 
@@ -175,6 +245,10 @@ var _default = function _default(_, opts) {
175
245
  if (nodes.length) {
176
246
  path.unshiftContainer('body', nodes);
177
247
  }
248
+
249
+ if (targets.length && (usedJSAPIList === 'all' || usedJSAPIList.length)) {
250
+ utils.log("Successfully inject the Hylid-bridge into ".concat(this.filename, " to support the targets: ").concat(targets.join(','), "."));
251
+ }
178
252
  }
179
253
  }
180
254
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-hylid-bridge",
3
- "version": "2.3.0",
3
+ "version": "2.4.0-alpha.2",
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",