babel-plugin-hylid-bridge 4.0.0-alpha.8 → 4.0.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.
@@ -0,0 +1,2 @@
1
+ export default function (targets: string[]): void;
2
+ export declare function filterWhichCanIUseJsapi(apiList: string[]): void;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = _default;
8
+ exports.filterWhichCanIUseJsapi = filterWhichCanIUseJsapi;
9
+ var _path = _interopRequireDefault(require("path"));
10
+ var _fsExtra = _interopRequireDefault(require("fs-extra"));
11
+ var _getLibraryDir = _interopRequireDefault(require("./getLibraryDir"));
12
+ var _helper = require("./helper");
13
+ // 筛选 hylid-bridge 兼容度信息
14
+ function _default(targets) {
15
+ var lib = (0, _getLibraryDir.default)();
16
+ targets = targets.map(function (item) {
17
+ return item.toLowerCase();
18
+ });
19
+ // 获取 lib 下的 bridge.js 文件
20
+ var bridgeFile = _path.default.join(lib, 'lib', 'whichCanIUse', 'compatibilities.json');
21
+ var compatibilities = JSON.parse(_fsExtra.default.readFileSync(bridgeFile, 'utf-8'));
22
+ // 获取 hylid-bridge 版本
23
+ var packageJsonFile = _path.default.join(lib, 'package.json');
24
+ var _JSON$parse = JSON.parse(_fsExtra.default.readFileSync(packageJsonFile, 'utf-8')),
25
+ version = _JSON$parse.version;
26
+ for (var api in compatibilities) {
27
+ compatibilities[api] = compatibilities[api].filter(function (item) {
28
+ // 标记删除的数据不要
29
+ if (item.deleteAt) return false;
30
+ // 比当前 hylid-bridge 版本低的不要
31
+ if (item.hylidBridgeVersion && (0, _helper.compareVersion)(version, item.hylidBridgeVersion) < 0) return false;
32
+ // babel 配置中没有的平台和客户端不要
33
+ if (!targets.includes((item.platformType + item.clientName).toLowerCase())) return false;
34
+ return true;
35
+ });
36
+ compatibilities[api] = compatibilities[api].map(function (item) {
37
+ return {
38
+ clientName: item.clientName,
39
+ platformType: item.platformType,
40
+ clientVersion: item.clientVersion,
41
+ osType: item.osType,
42
+ supported: item.supported
43
+ };
44
+ });
45
+ }
46
+ _fsExtra.default.writeFileSync(bridgeFile, JSON.stringify(compatibilities));
47
+ }
48
+ // 从 api 维度筛除数据
49
+ function filterWhichCanIUseJsapi(apiList) {
50
+ var lib = (0, _getLibraryDir.default)();
51
+ // 获取 lib 下的 bridge.js 文件
52
+ var bridgeFile = _path.default.join(lib, 'lib', 'whichCanIUse', 'compatibilities.json');
53
+ var compatibilities = JSON.parse(_fsExtra.default.readFileSync(bridgeFile, 'utf-8'));
54
+ Object.keys(compatibilities).forEach(function (api) {
55
+ if (!apiList.includes(api)) {
56
+ delete compatibilities[api];
57
+ }
58
+ });
59
+ _fsExtra.default.writeFileSync(bridgeFile, JSON.stringify(compatibilities));
60
+ }
@@ -10,9 +10,20 @@ function getLibraryDir() {
10
10
  if (!libraryDir) {
11
11
  var cwd = process.cwd();
12
12
  try {
13
- libraryDir = process.env.HYLID_BRIDGE_PACKAGE_DIR || require.resolve('hylid-bridge/package.json', {
13
+ // 找到依赖的 hylid-bridge,内外网版本同时找
14
+ var pathInternal = require.resolve('@alipay/hylid-bridge/package.json', {
14
15
  paths: [cwd]
15
16
  }).replace('package.json', '');
17
+ var path = require.resolve('hylid-bridge/package.json', {
18
+ paths: [cwd]
19
+ }).replace('package.json', '');
20
+ // 本地开发时,只安装了内网版本的包,外网的的包会找到 packages 目录下的开发包,需要忽略该情况
21
+ if (!pathInternal.includes('hylid-bridge/packages')) {
22
+ libraryDir = pathInternal;
23
+ }
24
+ if (!path.includes('hylid-bridge/packages')) {
25
+ libraryDir = path;
26
+ }
16
27
  (0, _utils.log)("Find the hylid-bridge package: ".concat(libraryDir));
17
28
  } catch (e) {
18
29
  (0, _utils.error)("Can not resolve the hylid-bridge package from the directory: ".concat(cwd, "."), e);
package/lib/helper.d.ts CHANGED
@@ -21,4 +21,5 @@ export declare function getNames(node: babelCore.NodePath<babelCore.types.Member
21
21
  };
22
22
  export declare function isJSAPIInTargets(jsapiName: string, targets: string[], filename: string, libraryDir: string, polyfillDir?: string): boolean;
23
23
  export declare function camelCase(input: string): string;
24
+ export declare function compareVersion(version1: string, version2: string): number;
24
25
  export {};
package/lib/helper.js CHANGED
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
8
  exports.camelCase = camelCase;
9
+ exports.compareVersion = compareVersion;
9
10
  exports.getNames = getNames;
10
11
  exports.getTargets = getTargets;
11
12
  exports.getUserAppEnvPath = getUserAppEnvPath;
@@ -116,4 +117,24 @@ function camelCase(input) {
116
117
  return word.charAt(0).toUpperCase() + word.slice(1);
117
118
  });
118
119
  return camelCasedWords.join('');
120
+ }
121
+ // 用于 babel 插件中比较 hylid-bridge 版本号
122
+ function compareVersion(version1, version2) {
123
+ // 删除 alpha 后缀进行比较,4.0.0-alpha.1 => 4.0.0
124
+ version1 = version1.split('-')[0];
125
+ version2 = version2.split('-')[0];
126
+ var v1Parts = version1.split('.').map(Number);
127
+ var v2Parts = version2.split('.').map(Number);
128
+ var maxLength = Math.max(v1Parts.length, v2Parts.length);
129
+ for (var i = 0; i < maxLength; i++) {
130
+ var v1 = v1Parts[i] || 0;
131
+ var v2 = v2Parts[i] || 0;
132
+ if (v1 > v2) {
133
+ return 1;
134
+ }
135
+ if (v1 < v2) {
136
+ return -1;
137
+ }
138
+ }
139
+ return 0;
119
140
  }
package/lib/index.d.ts CHANGED
@@ -2,6 +2,7 @@ import type babelCore from '@babel/core';
2
2
  import { PluginPass } from '@babel/core';
3
3
  interface IState extends PluginPass {
4
4
  usedJSAPIList: 'all' | string[];
5
+ whichCanIUseJsapiList: string[];
5
6
  }
6
7
  declare const _default: (core: typeof babelCore, opts?: {
7
8
  targets?: string[];
package/lib/index.js CHANGED
@@ -12,10 +12,12 @@ var _fsExtra = _interopRequireDefault(require("fs-extra"));
12
12
  var _lodash = _interopRequireDefault(require("lodash"));
13
13
  var babelTypes = _interopRequireWildcard(require("@babel/types"));
14
14
  var _getLibraryDir = _interopRequireDefault(require("./getLibraryDir"));
15
- var utils = _interopRequireWildcard(require("./utils"));
15
+ var _utils = _interopRequireWildcard(require("./utils"));
16
+ var utils = _utils;
16
17
  var _combineExports = _interopRequireDefault(require("./combineExports"));
17
18
  var _helper = require("./helper");
18
19
  var _generateJSAPIs = _interopRequireDefault(require("./generateJSAPIs"));
20
+ var _generateCompatibilities = _interopRequireWildcard(require("./generateCompatibilities"));
19
21
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
20
22
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
21
23
  /**
@@ -35,6 +37,7 @@ var _default = exports.default = function _default(core, opts) {
35
37
  var compact = (_opts$compact = opts === null || opts === void 0 ? void 0 : opts.compact) !== null && _opts$compact !== void 0 ? _opts$compact : true;
36
38
  // 生成 babel-compact 文件
37
39
  compact && (0, _generateJSAPIs.default)(targets);
40
+ (0, _generateCompatibilities.default)(targets);
38
41
  return {
39
42
  name: 'babel-plugin-hylid-bridge',
40
43
  visitor: {
@@ -83,6 +86,23 @@ var _default = exports.default = function _default(core, opts) {
83
86
  }
84
87
  },
85
88
  CallExpression: function CallExpression(path, state) {
89
+ if (path.node.callee.type === 'Identifier' && path.node.callee.name === 'whichCanIUse') {
90
+ if (!state.whichCanIUseJsapiList) {
91
+ state.whichCanIUseJsapiList = [];
92
+ }
93
+ if (path.node.arguments[0].type === 'ObjectExpression') {
94
+ path.node.arguments[0].properties.forEach(function (property) {
95
+ if (property.type === 'ObjectProperty' && property.key.type === 'Identifier' && property.key.name === 'apiName') {
96
+ if (property.value.type === 'StringLiteral') {
97
+ state.whichCanIUseJsapiList.push(property.value.value);
98
+ } else {
99
+ // apiName 为非字符串类型,警告开发者
100
+ (0, _utils.warn)('WhichCanIUse warning: apiName must be a string literal and not a variable');
101
+ }
102
+ }
103
+ });
104
+ }
105
+ }
86
106
  if (path.node.callee.type !== 'MemberExpression' || !transformMy) {
87
107
  return;
88
108
  }
@@ -110,6 +130,10 @@ var _default = exports.default = function _default(core, opts) {
110
130
  },
111
131
  Program: {
112
132
  exit: function exit(path, state) {
133
+ var _state$whichCanIUseJs;
134
+ if ((_state$whichCanIUseJs = state.whichCanIUseJsapiList) !== null && _state$whichCanIUseJs !== void 0 && _state$whichCanIUseJs.length) {
135
+ (0, _generateCompatibilities.filterWhichCanIUseJsapi)(state.whichCanIUseJsapiList);
136
+ }
113
137
  if (!state.usedJSAPIList) {
114
138
  return;
115
139
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-hylid-bridge",
3
- "version": "4.0.0-alpha.8",
3
+ "version": "4.0.0",
4
4
  "description": "babel-plugin-hylid-bridge",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -22,7 +22,7 @@
22
22
  "devDependencies": {
23
23
  "@types/babel-plugin-tester": "^9.0.7",
24
24
  "babel-plugin-tester": "^10.1.0",
25
- "hylid-bridge": "^4.0.0-alpha.8"
25
+ "hylid-bridge": "^4.0.0"
26
26
  },
27
27
  "sideEffects": false,
28
28
  "registry": "https://registry.npmjs.org/"