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

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,18 @@
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.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.1.0...v2.4.0-alpha.0) (2022-08-12)
7
+
8
+
9
+ ### Features
10
+
11
+ * 调整 eslint 配置* 根据源码注入 hylid-bridge 相应模块* 固定三方依赖版本号
12
+
13
+ ### Bug Fixes
14
+
15
+ * 依赖* build error* split the target
16
+
17
+
6
18
  ## [2.3.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.1.0...v2.3.0) (2022-08-05)
7
19
 
8
20
 
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,34 @@ 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
+
52
98
  var _default = function _default(_, opts) {
53
99
  var targets = getTargets(opts === null || opts === void 0 ? void 0 : opts.targets);
100
+ var userAppEnvPath = getUserAppEnvPath(opts === null || opts === void 0 ? void 0 : opts.appEnvPath);
54
101
  return {
55
102
  name: 'babel-plugin-hylid-bridge',
56
103
  visitor: {
@@ -76,7 +123,7 @@ var _default = function _default(_, opts) {
76
123
  var name = propertyName;
77
124
 
78
125
  if (targets.every(function (target) {
79
- var targetSplit = target.split('|');
126
+ var targetSplit = splitTarget(target);
80
127
 
81
128
  if (!isApiExists(name, target)) {
82
129
  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, "`."));
@@ -104,11 +151,24 @@ var _default = function _default(_, opts) {
104
151
  return;
105
152
  }
106
153
 
107
- var nodes = [babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(appEnvLocalName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/appEnv")))];
154
+ var innerAppEnvLocalName = "".concat(identifierPrefix, "innerAppEnv__");
155
+ var userAppEnvLocalName = "".concat(identifierPrefix, "userAppEnv__");
156
+ var nodes = [babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(innerAppEnvLocalName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/appEnv")))];
157
+
158
+ if (userAppEnvPath) {
159
+ var relativePath = _path.default.relative(_path.default.dirname(this.filename), userAppEnvPath.replace(/\.ts$/, ''));
160
+
161
+ nodes.push(babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(userAppEnvLocalName))], babelTypes.stringLiteral(relativePath.startsWith('../') ? relativePath : "./".concat(relativePath))));
162
+ } else {
163
+ nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(userAppEnvLocalName), babelTypes.objectExpression([]))]));
164
+ }
165
+
166
+ var objectSpreadName = "".concat(identifierPrefix, "objectSpread__");
167
+ 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
168
 
109
169
  if (usedJSAPIList === 'all') {
110
170
  nodes.push.apply(nodes, (0, _toConsumableArray2.default)(targets.map(function (target) {
111
- var targetSplit = target.split('|');
171
+ var targetSplit = splitTarget(target);
112
172
  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
173
  })));
114
174
 
@@ -118,14 +178,14 @@ var _default = function _default(_, opts) {
118
178
  }
119
179
 
120
180
  var target = targetList[0];
121
- var targetSplit = target.split('|');
181
+ var targetSplit = splitTarget(target);
122
182
  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)));
123
183
  };
124
184
 
125
185
  nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier('my'))])); // Do not generate the `if statement` when there is just one target.
126
186
 
127
187
  if (targets.length === 1) {
128
- var targetSplit = targets[0].split('|');
188
+ var targetSplit = splitTarget(targets[0]);
129
189
  nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier('my'), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__")))));
130
190
  } else {
131
191
  var elseIfNode = buildElseIf(targets);
@@ -136,7 +196,7 @@ var _default = function _default(_, opts) {
136
196
 
137
197
  normalizedJSAPIList.forEach(function (name) {
138
198
  targets.forEach(function (target) {
139
- var targetSplit = target.split('|');
199
+ var targetSplit = splitTarget(target);
140
200
  var localName = "".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__");
141
201
  var n = babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(localName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name)));
142
202
  nodes.push(n);
@@ -149,7 +209,7 @@ var _default = function _default(_, opts) {
149
209
  }
150
210
 
151
211
  var target = targetList[0];
152
- var targetSplit = target.split('|');
212
+ var targetSplit = splitTarget(target);
153
213
  var propertyList = normalizedJSAPIList.map(function (name) {
154
214
  return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__")));
155
215
  });
@@ -159,7 +219,7 @@ var _default = function _default(_, opts) {
159
219
  nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier('my'))])); // Do not generate the `if statement` when there is just one target.
160
220
 
161
221
  if (targets.length === 1) {
162
- var _targetSplit = targets[0].split('|');
222
+ var _targetSplit = splitTarget(targets[0]);
163
223
 
164
224
  var propertyList = normalizedJSAPIList.map(function (name) {
165
225
  return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(_targetSplit[0], "_").concat(_targetSplit[1], "_").concat(name, "__")));
@@ -175,6 +235,10 @@ var _default = function _default(_, opts) {
175
235
  if (nodes.length) {
176
236
  path.unshiftContainer('body', nodes);
177
237
  }
238
+
239
+ if (targets.length && (usedJSAPIList === 'all' || usedJSAPIList.length)) {
240
+ utils.log("Successfully inject the Hylid-bridge into ".concat(this.filename, " to support the targets: ").concat(targets.join(','), "."));
241
+ }
178
242
  }
179
243
  }
180
244
  }
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.0",
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",