babel-plugin-hylid-bridge 2.12.0 → 3.0.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.
@@ -1 +1 @@
1
- export default function combileExports(index1Path: string, index2Path: string): string;
1
+ export default function combieExports(index1Path: string, index2Path: string): string;
@@ -4,12 +4,13 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.default = combileExports;
7
+ exports.default = combieExports;
8
8
  var _path = _interopRequireDefault(require("path"));
9
9
  var _fsExtra = _interopRequireDefault(require("fs-extra"));
10
10
  var _hashIt = _interopRequireDefault(require("hash-it"));
11
- function combileExports(index1Path, index2Path) {
12
- var indexPath = _path.default.resolve(__dirname, '..', '.temp', (0, _hashIt.default)("".concat(index1Path).concat(index2Path)) + '.js');
11
+ var isTest = process.env.NODE_ENV === 'test';
12
+ function combieExports(index1Path, index2Path) {
13
+ var indexPath = _path.default.resolve(__dirname, '..', '.temp', (isTest ? 'test' : (0, _hashIt.default)("".concat(index1Path).concat(index2Path))) + '.js');
13
14
  _fsExtra.default.ensureFileSync(indexPath);
14
15
  _fsExtra.default.writeFileSync(indexPath, "\n import index1 from '".concat(index1Path, "';\n import index2 from '").concat(index2Path, "';\n\n export default {\n ...index1,\n ...index2,\n };\n "));
15
16
  return indexPath;
@@ -0,0 +1 @@
1
+ export default function generateJSAPIs(targets: string[]): void;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = generateJSAPIs;
8
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
+ var _path = _interopRequireDefault(require("path"));
10
+ var _fsExtra = _interopRequireDefault(require("fs-extra"));
11
+ var _parser = require("@babel/parser");
12
+ var _traverse = _interopRequireDefault(require("@babel/traverse"));
13
+ var _generator = _interopRequireDefault(require("@babel/generator"));
14
+ var _getLibraryDir = _interopRequireDefault(require("./getLibraryDir"));
15
+ var _helper = require("./helper");
16
+ function generateJSAPIs(targets) {
17
+ var lib = (0, _getLibraryDir.default)();
18
+ // 获取当前配置的环境信息
19
+ var appEnvList = targets.reduce(function (acc, target) {
20
+ var _splitTarget = (0, _helper.splitTarget)(target),
21
+ _splitTarget2 = (0, _slicedToArray2.default)(_splitTarget, 2),
22
+ platform = _splitTarget2[0],
23
+ client = _splitTarget2[1];
24
+ acc.push("".concat((0, _helper.camelCase)("is_".concat(platform, "_").concat(client))));
25
+ return acc;
26
+ }, []);
27
+ // 获取 lib 下的 bridge.js 文件
28
+ var bridgeFile = _path.default.join(lib, 'lib', 'bridges.js');
29
+ var ast = (0, _parser.parse)(_fsExtra.default.readFileSync(bridgeFile, 'utf-8'), {
30
+ sourceType: 'module'
31
+ });
32
+ (0, _traverse.default)(ast, {
33
+ IfStatement: function IfStatement(path) {
34
+ /**
35
+ * if 条件里有 n 多种写法,test 对应 n 多中类型。
36
+ * 下面为了方便,直接尝试访问 object.name 是否是 appEnv,在 bridge.js 中,符合这个条件的,一定是 jsapi 函数中 if 判断条件
37
+ */
38
+ var _ref = path.node.test || {},
39
+ object = _ref.object,
40
+ property = _ref.property;
41
+ if ((object === null || object === void 0 ? void 0 : object.name) === 'appEnv') {
42
+ var env = property.name;
43
+ // 删除不是目标的 appEnv 判断代码
44
+ if (!appEnvList.includes(env)) {
45
+ path.remove();
46
+ }
47
+ }
48
+ }
49
+ });
50
+ // 生成文件
51
+ var outPath = _path.default.join(lib, 'lib', 'babel-compact.js');
52
+ var codes = ['// hylid-bridge babel 插件生成', "export * from './index'", "export { default } from './index'", (0, _generator.default)(ast).code];
53
+ _fsExtra.default.writeFileSync(outPath, codes.join('\n'));
54
+ }
@@ -0,0 +1,24 @@
1
+ import type babelCore from '@babel/core';
2
+ import { PluginPass } from '@babel/core';
3
+ export declare function snakeCaseTarget(input: string): string;
4
+ export declare function isTargetSupported(target: string, libraryDir: string): boolean;
5
+ export declare function getTargets(libraryDir: string, customTargets?: string[]): string[];
6
+ export declare function isApiExists(name: string, target: string, libraryDir: string, polyfillDir?: string): boolean | "" | undefined;
7
+ export declare function getUserAppEnvPath(appEnvPath?: string): string | undefined;
8
+ export declare function splitTarget(target: string): string[];
9
+ interface IState extends PluginPass {
10
+ usedJSAPIList: 'all' | string[];
11
+ }
12
+ export declare function getNames(node: babelCore.NodePath<babelCore.types.MemberExpression>['node'], scope: babelCore.NodePath<babelCore.types.MemberExpression>['scope'], state: IState, targets: string[]): {
13
+ objectName: string;
14
+ propertyName: undefined;
15
+ } | {
16
+ objectName: string;
17
+ propertyName: string;
18
+ } | {
19
+ objectName: undefined;
20
+ propertyName: undefined;
21
+ };
22
+ export declare function isJSAPIInTargets(jsapiName: string, targets: string[], filename: string, libraryDir: string, polyfillDir?: string): boolean;
23
+ export declare function camelCase(input: string): string;
24
+ export {};
package/lib/helper.js ADDED
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _typeof = require("@babel/runtime/helpers/typeof");
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.camelCase = camelCase;
9
+ exports.getNames = getNames;
10
+ exports.getTargets = getTargets;
11
+ exports.getUserAppEnvPath = getUserAppEnvPath;
12
+ exports.isApiExists = isApiExists;
13
+ exports.isJSAPIInTargets = isJSAPIInTargets;
14
+ exports.isTargetSupported = isTargetSupported;
15
+ exports.snakeCaseTarget = snakeCaseTarget;
16
+ exports.splitTarget = splitTarget;
17
+ var _path = _interopRequireDefault(require("path"));
18
+ var _fsExtra = _interopRequireDefault(require("fs-extra"));
19
+ var utils = _interopRequireWildcard(require("./utils"));
20
+ 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); }
21
+ 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 && Object.prototype.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; }
22
+ function snakeCaseTarget(input) {
23
+ var words = input.replace(/[^a-zA-Z0-9]+/g, ' ').trim();
24
+ var wordArray = words.split(' ');
25
+ var snakeCasedWords = wordArray.map(function (word) {
26
+ return word.replace(/[A-Z]/g, function (match, index) {
27
+ return index === 0 ? match.toLowerCase() : "_".concat(match);
28
+ });
29
+ });
30
+ return snakeCasedWords.join('_');
31
+ }
32
+ function isTargetSupported(target, libraryDir) {
33
+ return ['MpWeb', 'Mp', 'Web'].some(function (prefix) {
34
+ if (target.startsWith(prefix)) {
35
+ var targetClientDir = _path.default.resolve(libraryDir, 'lib/clients', snakeCaseTarget(prefix), snakeCaseTarget(target.replace(prefix, '')));
36
+ return _fsExtra.default.existsSync(targetClientDir);
37
+ }
38
+ return false;
39
+ });
40
+ }
41
+ function getTargets(libraryDir, customTargets) {
42
+ var targets = customTargets || (process.env.HYLID_BRIDGE_TARGETS ? process.env.HYLID_BRIDGE_TARGETS.split(',') : []);
43
+ return targets.filter(function (target) {
44
+ return isTargetSupported(target, libraryDir);
45
+ });
46
+ }
47
+ function isApiExists(name, target, libraryDir, polyfillDir) {
48
+ var targetSplit = splitTarget(target);
49
+ if (targetSplit.length !== 2) {
50
+ throw new Error("Wrong target configuration: ".concat(target, "."));
51
+ }
52
+ return _fsExtra.default.existsSync(_path.default.resolve(libraryDir, "./lib/clients/".concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name, ".js"))) || polyfillDir && (_fsExtra.default.existsSync(_path.default.resolve(polyfillDir, "./".concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name, ".js"))) || _fsExtra.default.existsSync(_path.default.resolve(polyfillDir, "./".concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name, ".ts"))));
53
+ }
54
+ function getUserAppEnvPath(appEnvPath) {
55
+ var realAppEnvPath = appEnvPath !== null && appEnvPath !== void 0 ? appEnvPath : _path.default.resolve(process.cwd(), './src/appEnv.ts');
56
+ if (!_fsExtra.default.existsSync(realAppEnvPath)) {
57
+ utils.log("Can not find the appEnv module at the path: ".concat(realAppEnvPath, "."));
58
+ return;
59
+ }
60
+ return realAppEnvPath;
61
+ }
62
+ function splitTarget(target) {
63
+ var prefixList = ['MpWeb', 'Mp', 'Web'];
64
+ for (var i = 0, il = prefixList.length; i < il; i++) {
65
+ var prefix = prefixList[i];
66
+ if (target.startsWith(prefix)) {
67
+ return [snakeCaseTarget(prefix), snakeCaseTarget(target.replace(prefix, ''))];
68
+ }
69
+ }
70
+ throw new Error("Invalid bridge target: ".concat(target, "."));
71
+ }
72
+ function getNames(node, scope, state, targets) {
73
+ var _state$filename;
74
+ // my.alert || my[a]
75
+ if (node.object.type === 'Identifier' && node.property.type === 'Identifier' &&
76
+ // Remix 中会编译所有 node_modules 下的模块
77
+ !((_state$filename = state.filename) !== null && _state$filename !== void 0 && _state$filename.includes("".concat(_path.default.sep, "node_modules").concat(_path.default.sep, "hylid-bridge").concat(_path.default.sep))) && targets && targets.length && !scope.hasBinding('my')) {
78
+ return node.computed ? {
79
+ objectName: node.object.name,
80
+ propertyName: undefined
81
+ } : {
82
+ objectName: node.object.name,
83
+ propertyName: node.property.name
84
+ };
85
+ }
86
+ // my['alert']
87
+ if (node.object.type === 'Identifier' && node.property.type === 'StringLiteral' && node.computed && targets && targets.length && !scope.hasBinding('my')) {
88
+ return {
89
+ objectName: node.object.name,
90
+ propertyName: node.property.value
91
+ };
92
+ }
93
+ return {
94
+ objectName: undefined,
95
+ propertyName: undefined
96
+ };
97
+ }
98
+ function isJSAPIInTargets(jsapiName, targets, filename, libraryDir, polyfillDir) {
99
+ return targets.every(function (target) {
100
+ var targetSplit = splitTarget(target);
101
+ if (!isApiExists(jsapiName, target, libraryDir, polyfillDir)) {
102
+ utils.warn("Can not find the API `".concat(jsapiName, "` 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(filename, "`."));
103
+ return false;
104
+ }
105
+ return true;
106
+ });
107
+ }
108
+ // lodash 把数字后面第一个字符变大了,所以这里单独实现下
109
+ function camelCase(input) {
110
+ var words = input.replace(/[^a-zA-Z0-9]+/g, ' ').trim();
111
+ var wordArray = words.split(' ');
112
+ var camelCasedWords = wordArray.map(function (word, index) {
113
+ if (index === 0) {
114
+ return word.toLowerCase();
115
+ }
116
+ return word.charAt(0).toUpperCase() + word.slice(1);
117
+ });
118
+ return camelCasedWords.join('');
119
+ }
package/lib/index.d.ts CHANGED
@@ -8,5 +8,6 @@ declare const _default: (core: typeof babelCore, opts?: {
8
8
  appEnvPath?: string;
9
9
  libraryDir?: string;
10
10
  polyfillDir?: string;
11
+ transformMy?: boolean;
11
12
  }) => babelCore.PluginObj<IState>;
12
13
  export default _default;
package/lib/index.js CHANGED
@@ -14,111 +14,45 @@ var babelTypes = _interopRequireWildcard(require("@babel/types"));
14
14
  var _getLibraryDir = _interopRequireDefault(require("./getLibraryDir"));
15
15
  var utils = _interopRequireWildcard(require("./utils"));
16
16
  var _combineExports = _interopRequireDefault(require("./combineExports"));
17
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
18
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
+ var _helper = require("./helper");
18
+ var _generateJSAPIs = _interopRequireDefault(require("./generateJSAPIs"));
19
+ 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
+ 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 && Object.prototype.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; }
19
21
  /**
20
22
  * https://astexplorer.net/
21
23
  */
22
24
 
23
25
  var identifierPrefix = '__hylid_bridge__';
24
26
  var libraryName = 'hylid-bridge';
25
- function snakeCaseTarget(value) {
26
- if (value === 'H5') {
27
- return 'h5';
28
- }
29
- return _lodash.default.snakeCase(value);
30
- }
31
- function isTargetSupported(target, libraryDir) {
32
- return ['MpWeb', 'Mp', 'Web'].some(function (prefix) {
33
- if (target.startsWith(prefix)) {
34
- var targetClientDir = _path.default.resolve(libraryDir, 'lib/clients', snakeCaseTarget(prefix), snakeCaseTarget(target.replace(prefix, '')));
35
- return _fsExtra.default.existsSync(targetClientDir);
36
- }
37
- return false;
38
- });
39
- }
40
- function getTargets(libraryDir, customTargets) {
41
- var targets = customTargets || (process.env.HYLID_BRIDGE_TARGETS ? process.env.HYLID_BRIDGE_TARGETS.split(',') : []);
42
- return targets.filter(function (target) {
43
- return isTargetSupported(target, libraryDir);
44
- });
45
- }
46
- function isApiExists(name, target, libraryDir, polyfillDir) {
47
- var targetSplit = splitTarget(target);
48
- if (targetSplit.length !== 2) {
49
- throw new Error("Wrong target configuration: ".concat(target, "."));
50
- }
51
- return _fsExtra.default.existsSync(_path.default.resolve(libraryDir, "./lib/clients/".concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name, ".js"))) || polyfillDir && (_fsExtra.default.existsSync(_path.default.resolve(polyfillDir, "./".concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name, ".js"))) || _fsExtra.default.existsSync(_path.default.resolve(polyfillDir, "./".concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name, ".ts"))));
52
- }
53
- function getUserAppEnvPath(appEnvPath) {
54
- var realAppEnvPath = appEnvPath !== null && appEnvPath !== void 0 ? appEnvPath : _path.default.resolve(process.cwd(), './src/appEnv.ts');
55
- if (!_fsExtra.default.existsSync(realAppEnvPath)) {
56
- utils.log("Can not find the appEnv module at the path: ".concat(realAppEnvPath, "."));
57
- return;
58
- }
59
- return realAppEnvPath;
60
- }
61
- function splitTarget(target) {
62
- var prefixList = ['MpWeb', 'Mp', 'Web'];
63
- for (var i = 0, il = prefixList.length; i < il; i++) {
64
- var prefix = prefixList[i];
65
- if (target.startsWith(prefix)) {
66
- return [snakeCaseTarget(prefix), snakeCaseTarget(target.replace(prefix, ''))];
67
- }
68
- }
69
- throw new Error("Invalid bridge target: ".concat(target, "."));
70
- }
71
27
  var myLocalName = "".concat(identifierPrefix, "my__");
72
- function getNames(node, scope, state, targets) {
73
- var _state$filename;
74
- // my.alert || my[a]
75
- if (node.object.type === 'Identifier' && node.property.type === 'Identifier' &&
76
- // Remix 中会编译所有 node_modules 下的模块
77
- !((_state$filename = state.filename) !== null && _state$filename !== void 0 && _state$filename.includes("".concat(_path.default.sep, "node_modules").concat(_path.default.sep, "hylid-bridge").concat(_path.default.sep))) && targets && targets.length && !scope.hasBinding('my')) {
78
- return node.computed ? {
79
- objectName: node.object.name,
80
- propertyName: undefined
81
- } : {
82
- objectName: node.object.name,
83
- propertyName: node.property.name
84
- };
85
- }
86
- // my['alert']
87
- if (node.object.type === 'Identifier' && node.property.type === 'StringLiteral' && node.computed && targets && targets.length && !scope.hasBinding('my')) {
88
- return {
89
- objectName: node.object.name,
90
- propertyName: node.property.value
91
- };
92
- }
93
- return {
94
- objectName: undefined,
95
- propertyName: undefined
96
- };
97
- }
98
- function isJSAPIInTargets(jsapiName, targets, filename, libraryDir, polyfillDir) {
99
- return targets.every(function (target) {
100
- var targetSplit = splitTarget(target);
101
- if (!isApiExists(jsapiName, target, libraryDir, polyfillDir)) {
102
- utils.warn("Can not find the API `".concat(jsapiName, "` 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(filename, "`."));
103
- return false;
104
- }
105
- return true;
106
- });
107
- }
108
- var _default = function _default(core, opts) {
109
- var _opts$libraryDir;
28
+ var _default = exports.default = function _default(core, opts) {
29
+ var _opts$libraryDir, _opts$transformMy;
110
30
  var libraryDir = (_opts$libraryDir = opts === null || opts === void 0 ? void 0 : opts.libraryDir) !== null && _opts$libraryDir !== void 0 ? _opts$libraryDir : (0, _getLibraryDir.default)();
111
- var targets = getTargets(libraryDir, opts === null || opts === void 0 ? void 0 : opts.targets);
112
- var userAppEnvPath = getUserAppEnvPath(opts === null || opts === void 0 ? void 0 : opts.appEnvPath);
31
+ var targets = (0, _helper.getTargets)(libraryDir, opts === null || opts === void 0 ? void 0 : opts.targets);
32
+ var userAppEnvPath = (0, _helper.getUserAppEnvPath)(opts === null || opts === void 0 ? void 0 : opts.appEnvPath);
113
33
  var polyfillDir = opts === null || opts === void 0 ? void 0 : opts.polyfillDir;
34
+ var transformMy = (_opts$transformMy = opts === null || opts === void 0 ? void 0 : opts.transformMy) !== null && _opts$transformMy !== void 0 ? _opts$transformMy : false;
35
+ // 生成 babel-compact 文件
36
+ (0, _generateJSAPIs.default)(targets);
114
37
  return {
115
38
  name: 'babel-plugin-hylid-bridge',
116
39
  visitor: {
40
+ ImportDeclaration: function ImportDeclaration(path) {
41
+ if (path.node.source.value === libraryName) {
42
+ path.node.source = core.types.stringLiteral("".concat(libraryName, "/babel-compact"));
43
+ }
44
+ },
45
+ ExportNamedDeclaration: function ExportNamedDeclaration(path) {
46
+ var _path$node$source;
47
+ if (((_path$node$source = path.node.source) === null || _path$node$source === void 0 ? void 0 : _path$node$source.value) === libraryName) {
48
+ path.node.source = core.types.stringLiteral("".concat(libraryName, "/babel-compact"));
49
+ }
50
+ },
117
51
  MemberExpression: function MemberExpression(path, state) {
118
- var _getNames = getNames(path.node, path.scope, state, targets),
52
+ var _getNames = (0, _helper.getNames)(path.node, path.scope, state, targets),
119
53
  objectName = _getNames.objectName,
120
54
  propertyName = _getNames.propertyName;
121
- if (propertyName === 'call' || objectName !== 'my') {
55
+ if (propertyName === 'call' || objectName !== 'my' || !transformMy) {
122
56
  return;
123
57
  }
124
58
  var newNode = core.types.cloneNode(path.node, true, true);
@@ -136,7 +70,7 @@ var _default = function _default(core, opts) {
136
70
  if (propertyName) {
137
71
  state.usedJSAPIList = state.usedJSAPIList || [];
138
72
  var name = propertyName;
139
- if (isJSAPIInTargets(name, targets, state.filename, libraryDir, polyfillDir)) {
73
+ if ((0, _helper.isJSAPIInTargets)(name, targets, state.filename, libraryDir, polyfillDir)) {
140
74
  state.usedJSAPIList.push(name);
141
75
  } else {
142
76
  state.usedJSAPIList = 'all';
@@ -146,23 +80,25 @@ var _default = function _default(core, opts) {
146
80
  }
147
81
  },
148
82
  CallExpression: function CallExpression(path, state) {
149
- if (path.node.callee.type !== 'MemberExpression') {
83
+ if (path.node.callee.type !== 'MemberExpression' || !transformMy) {
150
84
  return;
151
85
  }
152
- var _getNames2 = getNames(path.node.callee, path.scope, state, targets),
86
+ var _getNames2 = (0, _helper.getNames)(path.node.callee, path.scope, state, targets),
153
87
  objectName = _getNames2.objectName,
154
88
  propertyName = _getNames2.propertyName;
155
89
  if (objectName !== 'my' || propertyName !== 'call' || !path.node.arguments[0] || path.node.arguments[0].type !== 'StringLiteral') {
156
90
  return;
157
91
  }
158
- var newNode = core.types.callExpression(babelTypes.memberExpression(babelTypes.identifier(myLocalName), babelTypes.identifier(path.node.arguments[0].value)), path.node.arguments.slice(1));
92
+ var newNode = core.types.callExpression(
93
+ // @ts-ignore
94
+ babelTypes.memberExpression(babelTypes.identifier(myLocalName), babelTypes.identifier(path.node.arguments[0].value)), path.node.arguments.slice(1));
159
95
  if (state.usedJSAPIList === 'all') {
160
96
  path.replaceWith(newNode);
161
97
  return;
162
98
  }
163
99
  state.usedJSAPIList = state.usedJSAPIList || [];
164
100
  var name = path.node.arguments[0].value;
165
- if (isJSAPIInTargets(name, targets, state.filename, libraryDir, polyfillDir)) {
101
+ if ((0, _helper.isJSAPIInTargets)(name, targets, state.filename, libraryDir, polyfillDir)) {
166
102
  state.usedJSAPIList.push(name);
167
103
  } else {
168
104
  state.usedJSAPIList = 'all';
@@ -192,7 +128,7 @@ var _default = function _default(core, opts) {
192
128
  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)]))]));
193
129
  if (usedJSAPIList === 'all') {
194
130
  nodes.push.apply(nodes, (0, _toConsumableArray2.default)(targets.map(function (target) {
195
- var targetSplit = splitTarget(target);
131
+ var targetSplit = (0, _helper.splitTarget)(target);
196
132
  var innerIndexPath = "".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/index");
197
133
  var polyfillIndexPath = polyfillDir && _path.default.resolve(polyfillDir, targetSplit[0], targetSplit[1], 'index');
198
134
  var normalizedIndexPath = polyfillIndexPath && (_fsExtra.default.existsSync("".concat(polyfillIndexPath, ".js")) || _fsExtra.default.existsSync("".concat(polyfillIndexPath, ".ts"))) ? (0, _combineExports.default)(require.resolve(innerIndexPath), polyfillIndexPath) : innerIndexPath;
@@ -203,13 +139,13 @@ var _default = function _default(core, opts) {
203
139
  return;
204
140
  }
205
141
  var target = targetList[0];
206
- var targetSplit = splitTarget(target);
207
- 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)));
142
+ var targetSplit = (0, _helper.splitTarget)(target);
143
+ return babelTypes.ifStatement(babelTypes.identifier("".concat(appEnvLocalName, ".").concat((0, _helper.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)));
208
144
  };
209
145
  nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(myLocalName))]));
210
146
  // Do not generate the `if statement` when there is just one target.
211
147
  if (targets.length === 1) {
212
- var targetSplit = splitTarget(targets[0]);
148
+ var targetSplit = (0, _helper.splitTarget)(targets[0]);
213
149
  nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__")))));
214
150
  } else {
215
151
  var elseIfNode = buildElseIf(targets);
@@ -219,7 +155,7 @@ var _default = function _default(core, opts) {
219
155
  var normalizedJSAPIList = _lodash.default.uniq(usedJSAPIList);
220
156
  normalizedJSAPIList.forEach(function (name) {
221
157
  targets.forEach(function (target) {
222
- var targetSplit = splitTarget(target);
158
+ var targetSplit = (0, _helper.splitTarget)(target);
223
159
  var localName = "".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__");
224
160
  var innerSourcePath = "".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name);
225
161
  var userSourcePath = polyfillDir ? _path.default.resolve(polyfillDir, targetSplit[0], targetSplit[1], name) : undefined;
@@ -232,16 +168,16 @@ var _default = function _default(core, opts) {
232
168
  return;
233
169
  }
234
170
  var target = targetList[0];
235
- var targetSplit = splitTarget(target);
171
+ var targetSplit = (0, _helper.splitTarget)(target);
236
172
  var propertyList = normalizedJSAPIList.map(function (name) {
237
173
  return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__")));
238
174
  });
239
- 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)));
175
+ return babelTypes.ifStatement(babelTypes.identifier("".concat(appEnvLocalName, ".").concat((0, _helper.camelCase)("is_".concat(targetSplit[0], "_").concat(targetSplit[1])))), babelTypes.blockStatement([babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.objectExpression(propertyList)))]), _buildElseIf(targetList.slice(1)));
240
176
  };
241
177
  nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(myLocalName))]));
242
178
  // Do not generate the `if statement` when there is just one target.
243
179
  if (targets.length === 1) {
244
- var _targetSplit = splitTarget(targets[0]);
180
+ var _targetSplit = (0, _helper.splitTarget)(targets[0]);
245
181
  var propertyList = normalizedJSAPIList.map(function (name) {
246
182
  return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(_targetSplit[0], "_").concat(_targetSplit[1], "_").concat(name, "__")));
247
183
  });
@@ -261,5 +197,4 @@ var _default = function _default(core, opts) {
261
197
  }
262
198
  }
263
199
  };
264
- };
265
- exports.default = _default;
200
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-hylid-bridge",
3
- "version": "2.12.0",
3
+ "version": "3.0.0-alpha.2",
4
4
  "description": "babel-plugin-hylid-bridge",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -20,9 +20,11 @@
20
20
  "lodash": "4.17.21"
21
21
  },
22
22
  "devDependencies": {
23
+ "@types/babel-plugin-tester": "^9.0.7",
23
24
  "babel-plugin-tester": "^10.1.0",
24
- "hylid-bridge": "^2.12.0"
25
+ "hylid-bridge": "^3.0.0-alpha.2"
25
26
  },
26
27
  "sideEffects": false,
27
- "registry": "https://registry.npmjs.org/"
28
+ "registry": "https://registry.npmjs.org/",
29
+ "repository": "https://code.alipay.com/ant-ife/hylid-bridge.git"
28
30
  }
package/CHANGELOG.md DELETED
@@ -1,193 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
-
6
- ## [2.12.0] (2023-07-25)
7
-
8
- **Note:** Version bump only for package babel-plugin-hylid-bridge
9
-
10
-
11
-
12
-
13
-
14
- ## [2.12.0-alpha.7] (2023-07-25)
15
-
16
- **Note:** Version bump only for package babel-plugin-hylid-bridge
17
-
18
-
19
-
20
-
21
-
22
- ## [2.12.0-alpha.6] (2023-07-21)
23
-
24
-
25
- ### Features
26
-
27
- * 判断 ts 文件
28
-
29
-
30
-
31
- ## [2.12.0-alpha.5] (2023-07-20)
32
-
33
-
34
- ### Bug Fixes
35
-
36
- * 检查文件是否存在
37
-
38
-
39
-
40
- ## [2.12.0-alpha.4] (2023-07-20)
41
-
42
-
43
- ### Features
44
-
45
- * 指定编译 src 目录
46
-
47
-
48
-
49
- ## [2.12.0-alpha.3] (2023-07-20)
50
-
51
-
52
- ### Bug Fixes
53
-
54
- * type error
55
-
56
-
57
-
58
- ## [2.12.0-alpha.2] (2023-07-20)
59
-
60
- **Note:** Version bump only for package babel-plugin-hylid-bridge
61
-
62
-
63
-
64
-
65
-
66
- ## [2.12.0-alpha.1] (2023-07-19)
67
-
68
- **Note:** Version bump only for package babel-plugin-hylid-bridge
69
-
70
-
71
-
72
-
73
-
74
- ## [2.12.0-alpha.0] (2023-07-19)
75
-
76
-
77
- ### Features
78
-
79
- * 升级版本号
80
-
81
-
82
-
83
- ## [2.10.0] (2023-02-22)
84
-
85
- **Note:** Version bump only for package babel-plugin-hylid-bridge
86
-
87
-
88
-
89
-
90
-
91
- ## [2.9.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.9.0-alpha.0...v2.9.0) (2022-11-09)
92
-
93
-
94
- ### Bug Fixes
95
-
96
- * 类型兼容
97
-
98
-
99
- ## [2.9.0-alpha.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.8.0...v2.9.0-alpha.0) (2022-11-09)
100
-
101
-
102
- ### Features
103
-
104
- * 支持 my.call 调用
105
-
106
-
107
- ## [2.4.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.4.0-alpha.4...v2.4.0) (2022-08-18)
108
-
109
- **Note:** Version bump only for package babel-plugin-hylid-bridge
110
-
111
-
112
-
113
-
114
-
115
- ## [2.4.0-alpha.4](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.1.0...v2.4.0-alpha.4) (2022-08-18)
116
-
117
-
118
- ### Features
119
-
120
- * 避免频繁打印日志* 不修改全局的 my, 新建本地变量* 调整 eslint 配置* 根据源码注入 hylid-bridge 相应模块* 固定三方依赖版本号* 适配 Remix 中会编译所有 node_modules 下模块的逻辑
121
-
122
- ### Bug Fixes
123
-
124
- * 依赖* build error* split the target
125
-
126
-
127
- ## [2.4.0-alpha.3](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.4.0-alpha.2...v2.4.0-alpha.3) (2022-08-17)
128
-
129
- **Note:** Version bump only for package babel-plugin-hylid-bridge
130
-
131
-
132
-
133
-
134
-
135
- ## [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)
136
-
137
-
138
- ### Features
139
-
140
- * 避免频繁打印日志* 适配 Remix 中会编译所有 node_modules 下模块的逻辑
141
-
142
-
143
- ## [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)
144
-
145
-
146
- ### Features
147
-
148
- * 不修改全局的 my, 新建本地变量
149
-
150
-
151
- ## [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)
152
-
153
-
154
- ### Features
155
-
156
- * 调整 eslint 配置* 根据源码注入 hylid-bridge 相应模块* 固定三方依赖版本号
157
-
158
- ### Bug Fixes
159
-
160
- * 依赖* build error* split the target
161
-
162
-
163
- ## [2.3.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.1.0...v2.3.0) (2022-08-05)
164
-
165
-
166
- ### Features
167
-
168
- * 调整 eslint 配置
169
-
170
- ### Bug Fixes
171
-
172
- * 依赖* build error* split the target
173
-
174
-
175
- ## [2.2.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.1.0...v2.2.0) (2022-07-08)
176
-
177
-
178
- ### Features
179
-
180
- * 调整 eslint 配置
181
-
182
- ### Bug Fixes
183
-
184
- * 依赖* build error* split the target
185
-
186
-
187
- # [2.1.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v1.1.0...v2.1.0) (2022-04-25)
188
-
189
-
190
- ### Features
191
-
192
- * 接入 babel 插件 ([fdb8885](https://code.alipay.com/ant-ife/hylid-bridge/commits/fdb8885c6d7f16b8fbc528540ff3b397096da60b))
193
- * 统一版本号 ([7539c2f](https://code.alipay.com/ant-ife/hylid-bridge/commits/7539c2f9fa4646fde964b609259e61d5e008c511))