babel-plugin-hylid-bridge 2.12.0-alpha.2 → 2.12.0-alpha.22
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/lib/combineExports.d.ts +1 -0
- package/lib/combineExports.js +17 -0
- package/lib/generateJSAPIs.d.ts +1 -0
- package/lib/generateJSAPIs.js +54 -0
- package/lib/helper.d.ts +24 -0
- package/lib/helper.js +119 -0
- package/lib/index.d.ts +7 -1
- package/lib/index.js +52 -99
- package/package.json +9 -2
- package/CHANGELOG.md +0 -141
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function combieExports(index1Path: string, index2Path: string): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = combieExports;
|
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
|
9
|
+
var _fsExtra = _interopRequireDefault(require("fs-extra"));
|
|
10
|
+
var _hashIt = _interopRequireDefault(require("hash-it"));
|
|
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');
|
|
14
|
+
_fsExtra.default.ensureFileSync(indexPath);
|
|
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 "));
|
|
16
|
+
return indexPath;
|
|
17
|
+
}
|
|
@@ -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', '_entry.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
|
+
}
|
package/lib/helper.d.ts
ADDED
|
@@ -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
|
@@ -3,5 +3,11 @@ import { PluginPass } from '@babel/core';
|
|
|
3
3
|
interface IState extends PluginPass {
|
|
4
4
|
usedJSAPIList: 'all' | string[];
|
|
5
5
|
}
|
|
6
|
-
declare const _default: (core: typeof babelCore, opts?:
|
|
6
|
+
declare const _default: (core: typeof babelCore, opts?: {
|
|
7
|
+
targets?: string[];
|
|
8
|
+
appEnvPath?: string;
|
|
9
|
+
libraryDir?: string;
|
|
10
|
+
polyfillDir?: string;
|
|
11
|
+
transformMy?: boolean;
|
|
12
|
+
}) => babelCore.PluginObj<IState>;
|
|
7
13
|
export default _default;
|
package/lib/index.js
CHANGED
|
@@ -13,97 +13,40 @@ var _lodash = _interopRequireDefault(require("lodash"));
|
|
|
13
13
|
var babelTypes = _interopRequireWildcard(require("@babel/types"));
|
|
14
14
|
var _getLibraryDir = _interopRequireDefault(require("./getLibraryDir"));
|
|
15
15
|
var utils = _interopRequireWildcard(require("./utils"));
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
var _combineExports = _interopRequireDefault(require("./combineExports"));
|
|
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; }
|
|
18
21
|
/**
|
|
19
22
|
* https://astexplorer.net/
|
|
20
23
|
*/
|
|
21
24
|
|
|
22
25
|
var identifierPrefix = '__hylid_bridge__';
|
|
23
26
|
var libraryName = 'hylid-bridge';
|
|
24
|
-
var libraryDir = (0, _getLibraryDir.default)();
|
|
25
|
-
function snakeCaseTarget(value) {
|
|
26
|
-
if (value === 'H5') {
|
|
27
|
-
return 'h5';
|
|
28
|
-
}
|
|
29
|
-
return _lodash.default.snakeCase(value);
|
|
30
|
-
}
|
|
31
|
-
function isTargetSupported(target) {
|
|
32
|
-
var libraryDir = (0, _getLibraryDir.default)();
|
|
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(customTargets) {
|
|
42
|
-
var targets = customTargets || (process.env.HYLID_BRIDGE_TARGETS ? process.env.HYLID_BRIDGE_TARGETS.split(',') : []);
|
|
43
|
-
return targets.filter(isTargetSupported);
|
|
44
|
-
}
|
|
45
|
-
function isApiExists(name, target) {
|
|
46
|
-
var targetSplit = splitTarget(target);
|
|
47
|
-
if (targetSplit.length !== 2) {
|
|
48
|
-
throw new Error("Wrong target configuration: ".concat(target, "."));
|
|
49
|
-
}
|
|
50
|
-
return _fsExtra.default.existsSync(_path.default.resolve(libraryDir, "./lib/clients/".concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name, ".js")));
|
|
51
|
-
}
|
|
52
|
-
function getUserAppEnvPath(appEnvPath) {
|
|
53
|
-
var realAppEnvPath = appEnvPath !== null && appEnvPath !== void 0 ? appEnvPath : _path.default.resolve(process.cwd(), './src/appEnv.ts');
|
|
54
|
-
if (!_fsExtra.default.existsSync(realAppEnvPath)) {
|
|
55
|
-
utils.log("Can not find the appEnv module at the path: ".concat(realAppEnvPath, "."));
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
return realAppEnvPath;
|
|
59
|
-
}
|
|
60
|
-
function splitTarget(target) {
|
|
61
|
-
var prefixList = ['MpWeb', 'Mp', 'Web'];
|
|
62
|
-
for (var i = 0, il = prefixList.length; i < il; i++) {
|
|
63
|
-
var prefix = prefixList[i];
|
|
64
|
-
if (target.startsWith(prefix)) {
|
|
65
|
-
return [snakeCaseTarget(prefix), snakeCaseTarget(target.replace(prefix, ''))];
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
throw new Error("Invalid bridge target: ".concat(target, "."));
|
|
69
|
-
}
|
|
70
27
|
var myLocalName = "".concat(identifierPrefix, "my__");
|
|
71
|
-
function
|
|
72
|
-
var
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
return {
|
|
82
|
-
objectName: undefined,
|
|
83
|
-
propertyName: undefined
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
function isJSAPIInTargets(jsapiName, targets, filename) {
|
|
87
|
-
return targets.every(function (target) {
|
|
88
|
-
var targetSplit = splitTarget(target);
|
|
89
|
-
if (!isApiExists(jsapiName, target)) {
|
|
90
|
-
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, "`."));
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
return true;
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
var _default = function _default(core, opts) {
|
|
97
|
-
var targets = getTargets(opts === null || opts === void 0 ? void 0 : opts.targets);
|
|
98
|
-
var userAppEnvPath = getUserAppEnvPath(opts === null || opts === void 0 ? void 0 : opts.appEnvPath);
|
|
28
|
+
var _default = exports.default = function _default(core, opts) {
|
|
29
|
+
var _opts$libraryDir, _opts$transformMy;
|
|
30
|
+
var libraryDir = (_opts$libraryDir = opts === null || opts === void 0 ? void 0 : opts.libraryDir) !== null && _opts$libraryDir !== void 0 ? _opts$libraryDir : (0, _getLibraryDir.default)();
|
|
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);
|
|
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
|
+
// 生成 _entry 文件
|
|
36
|
+
(0, _generateJSAPIs.default)(targets);
|
|
99
37
|
return {
|
|
100
38
|
name: 'babel-plugin-hylid-bridge',
|
|
101
39
|
visitor: {
|
|
40
|
+
ImportDeclaration: function ImportDeclaration(path) {
|
|
41
|
+
if (path.node.source.value === libraryName) {
|
|
42
|
+
path.node.source = core.types.stringLiteral("".concat(libraryName, "/lib/_entry.js"));
|
|
43
|
+
}
|
|
44
|
+
},
|
|
102
45
|
MemberExpression: function MemberExpression(path, state) {
|
|
103
|
-
var _getNames = getNames(path.node, path.scope, state, targets),
|
|
46
|
+
var _getNames = (0, _helper.getNames)(path.node, path.scope, state, targets),
|
|
104
47
|
objectName = _getNames.objectName,
|
|
105
48
|
propertyName = _getNames.propertyName;
|
|
106
|
-
if (
|
|
49
|
+
if (propertyName === 'call' || objectName !== 'my' || !transformMy) {
|
|
107
50
|
return;
|
|
108
51
|
}
|
|
109
52
|
var newNode = core.types.cloneNode(path.node, true, true);
|
|
@@ -112,38 +55,44 @@ var _default = function _default(core, opts) {
|
|
|
112
55
|
path.replaceWith(newNode);
|
|
113
56
|
return;
|
|
114
57
|
}
|
|
115
|
-
if (path.node.computed) {
|
|
58
|
+
if (!propertyName && path.node.computed) {
|
|
116
59
|
state.usedJSAPIList = 'all';
|
|
117
|
-
utils.warn("Detect visiting the property of `my` with computed mode
|
|
118
|
-
|
|
60
|
+
utils.warn("Detect visiting the property of `my` with computed mode. Fallback to import all API codes of ".concat(targets.join(','), ". Current file path: `").concat(state.filename, "`."));
|
|
61
|
+
path.replaceWith(newNode);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (propertyName) {
|
|
119
65
|
state.usedJSAPIList = state.usedJSAPIList || [];
|
|
120
66
|
var name = propertyName;
|
|
121
|
-
if (isJSAPIInTargets(name, targets, state.filename)) {
|
|
67
|
+
if ((0, _helper.isJSAPIInTargets)(name, targets, state.filename, libraryDir, polyfillDir)) {
|
|
122
68
|
state.usedJSAPIList.push(name);
|
|
123
69
|
} else {
|
|
124
70
|
state.usedJSAPIList = 'all';
|
|
125
71
|
}
|
|
72
|
+
path.replaceWith(newNode);
|
|
73
|
+
return;
|
|
126
74
|
}
|
|
127
|
-
path.replaceWith(newNode);
|
|
128
75
|
},
|
|
129
76
|
CallExpression: function CallExpression(path, state) {
|
|
130
|
-
if (path.node.callee.type !== 'MemberExpression') {
|
|
77
|
+
if (path.node.callee.type !== 'MemberExpression' || !transformMy) {
|
|
131
78
|
return;
|
|
132
79
|
}
|
|
133
|
-
var _getNames2 = getNames(path.node.callee, path.scope, state, targets),
|
|
80
|
+
var _getNames2 = (0, _helper.getNames)(path.node.callee, path.scope, state, targets),
|
|
134
81
|
objectName = _getNames2.objectName,
|
|
135
82
|
propertyName = _getNames2.propertyName;
|
|
136
83
|
if (objectName !== 'my' || propertyName !== 'call' || !path.node.arguments[0] || path.node.arguments[0].type !== 'StringLiteral') {
|
|
137
84
|
return;
|
|
138
85
|
}
|
|
139
|
-
var newNode = core.types.callExpression(
|
|
86
|
+
var newNode = core.types.callExpression(
|
|
87
|
+
// @ts-ignore
|
|
88
|
+
babelTypes.memberExpression(babelTypes.identifier(myLocalName), babelTypes.identifier(path.node.arguments[0].value)), path.node.arguments.slice(1));
|
|
140
89
|
if (state.usedJSAPIList === 'all') {
|
|
141
90
|
path.replaceWith(newNode);
|
|
142
91
|
return;
|
|
143
92
|
}
|
|
144
93
|
state.usedJSAPIList = state.usedJSAPIList || [];
|
|
145
94
|
var name = path.node.arguments[0].value;
|
|
146
|
-
if (isJSAPIInTargets(name, targets, state.filename)) {
|
|
95
|
+
if ((0, _helper.isJSAPIInTargets)(name, targets, state.filename, libraryDir, polyfillDir)) {
|
|
147
96
|
state.usedJSAPIList.push(name);
|
|
148
97
|
} else {
|
|
149
98
|
state.usedJSAPIList = 'all';
|
|
@@ -173,21 +122,24 @@ var _default = function _default(core, opts) {
|
|
|
173
122
|
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)]))]));
|
|
174
123
|
if (usedJSAPIList === 'all') {
|
|
175
124
|
nodes.push.apply(nodes, (0, _toConsumableArray2.default)(targets.map(function (target) {
|
|
176
|
-
var targetSplit = splitTarget(target);
|
|
177
|
-
|
|
125
|
+
var targetSplit = (0, _helper.splitTarget)(target);
|
|
126
|
+
var innerIndexPath = "".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/index");
|
|
127
|
+
var polyfillIndexPath = polyfillDir && _path.default.resolve(polyfillDir, targetSplit[0], targetSplit[1], 'index');
|
|
128
|
+
var normalizedIndexPath = polyfillIndexPath && (_fsExtra.default.existsSync("".concat(polyfillIndexPath, ".js")) || _fsExtra.default.existsSync("".concat(polyfillIndexPath, ".ts"))) ? (0, _combineExports.default)(require.resolve(innerIndexPath), polyfillIndexPath) : innerIndexPath;
|
|
129
|
+
return babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__")))], babelTypes.stringLiteral(normalizedIndexPath));
|
|
178
130
|
})));
|
|
179
131
|
var buildElseIf = function buildElseIf(targetList) {
|
|
180
132
|
if (!targetList.length) {
|
|
181
133
|
return;
|
|
182
134
|
}
|
|
183
135
|
var target = targetList[0];
|
|
184
|
-
var targetSplit = splitTarget(target);
|
|
185
|
-
return babelTypes.ifStatement(babelTypes.identifier("".concat(appEnvLocalName, ".").concat(
|
|
136
|
+
var targetSplit = (0, _helper.splitTarget)(target);
|
|
137
|
+
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)));
|
|
186
138
|
};
|
|
187
139
|
nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(myLocalName))]));
|
|
188
140
|
// Do not generate the `if statement` when there is just one target.
|
|
189
141
|
if (targets.length === 1) {
|
|
190
|
-
var targetSplit = splitTarget(targets[0]);
|
|
142
|
+
var targetSplit = (0, _helper.splitTarget)(targets[0]);
|
|
191
143
|
nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__")))));
|
|
192
144
|
} else {
|
|
193
145
|
var elseIfNode = buildElseIf(targets);
|
|
@@ -197,9 +149,11 @@ var _default = function _default(core, opts) {
|
|
|
197
149
|
var normalizedJSAPIList = _lodash.default.uniq(usedJSAPIList);
|
|
198
150
|
normalizedJSAPIList.forEach(function (name) {
|
|
199
151
|
targets.forEach(function (target) {
|
|
200
|
-
var targetSplit = splitTarget(target);
|
|
152
|
+
var targetSplit = (0, _helper.splitTarget)(target);
|
|
201
153
|
var localName = "".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__");
|
|
202
|
-
var
|
|
154
|
+
var innerSourcePath = "".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name);
|
|
155
|
+
var userSourcePath = polyfillDir ? _path.default.resolve(polyfillDir, targetSplit[0], targetSplit[1], name) : undefined;
|
|
156
|
+
var n = babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(localName))], babelTypes.stringLiteral(userSourcePath && (_fsExtra.default.existsSync("".concat(userSourcePath, ".js")) || _fsExtra.default.existsSync("".concat(userSourcePath, ".ts"))) ? userSourcePath : innerSourcePath));
|
|
203
157
|
nodes.push(n);
|
|
204
158
|
});
|
|
205
159
|
});
|
|
@@ -208,16 +162,16 @@ var _default = function _default(core, opts) {
|
|
|
208
162
|
return;
|
|
209
163
|
}
|
|
210
164
|
var target = targetList[0];
|
|
211
|
-
var targetSplit = splitTarget(target);
|
|
165
|
+
var targetSplit = (0, _helper.splitTarget)(target);
|
|
212
166
|
var propertyList = normalizedJSAPIList.map(function (name) {
|
|
213
167
|
return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__")));
|
|
214
168
|
});
|
|
215
|
-
return babelTypes.ifStatement(babelTypes.identifier("".concat(appEnvLocalName, ".").concat(
|
|
169
|
+
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)));
|
|
216
170
|
};
|
|
217
171
|
nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(myLocalName))]));
|
|
218
172
|
// Do not generate the `if statement` when there is just one target.
|
|
219
173
|
if (targets.length === 1) {
|
|
220
|
-
var _targetSplit = splitTarget(targets[0]);
|
|
174
|
+
var _targetSplit = (0, _helper.splitTarget)(targets[0]);
|
|
221
175
|
var propertyList = normalizedJSAPIList.map(function (name) {
|
|
222
176
|
return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(_targetSplit[0], "_").concat(_targetSplit[1], "_").concat(name, "__")));
|
|
223
177
|
});
|
|
@@ -237,5 +191,4 @@ var _default = function _default(core, opts) {
|
|
|
237
191
|
}
|
|
238
192
|
}
|
|
239
193
|
};
|
|
240
|
-
};
|
|
241
|
-
exports.default = _default;
|
|
194
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "babel-plugin-hylid-bridge",
|
|
3
|
-
"version": "2.12.0-alpha.
|
|
3
|
+
"version": "2.12.0-alpha.22",
|
|
4
4
|
"description": "babel-plugin-hylid-bridge",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -16,8 +16,15 @@
|
|
|
16
16
|
"@types/lodash": "4.14.182",
|
|
17
17
|
"chalk": "3.0.0",
|
|
18
18
|
"fs-extra": "10.1.0",
|
|
19
|
+
"hash-it": "^6.0.0",
|
|
19
20
|
"lodash": "4.17.21"
|
|
20
21
|
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/babel-plugin-tester": "^9.0.7",
|
|
24
|
+
"babel-plugin-tester": "^10.1.0",
|
|
25
|
+
"hylid-bridge": "^2.12.0-alpha.22"
|
|
26
|
+
},
|
|
21
27
|
"sideEffects": false,
|
|
22
|
-
"registry": "https://registry.npmjs.org/"
|
|
28
|
+
"registry": "https://registry.npmjs.org/",
|
|
29
|
+
"repository": "https://code.alipay.com/ant-ife/hylid-bridge.git"
|
|
23
30
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,141 +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-alpha.2] (2023-07-20)
|
|
7
|
-
|
|
8
|
-
**Note:** Version bump only for package babel-plugin-hylid-bridge
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
## [2.12.0-alpha.1] (2023-07-19)
|
|
15
|
-
|
|
16
|
-
**Note:** Version bump only for package babel-plugin-hylid-bridge
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
## [2.12.0-alpha.0] (2023-07-19)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
### Features
|
|
26
|
-
|
|
27
|
-
* 升级版本号
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
## [2.10.0] (2023-02-22)
|
|
32
|
-
|
|
33
|
-
**Note:** Version bump only for package babel-plugin-hylid-bridge
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
## [2.9.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.9.0-alpha.0...v2.9.0) (2022-11-09)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
### Bug Fixes
|
|
43
|
-
|
|
44
|
-
* 类型兼容
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
## [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)
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
### Features
|
|
51
|
-
|
|
52
|
-
* 支持 my.call 调用
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
## [2.4.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.4.0-alpha.4...v2.4.0) (2022-08-18)
|
|
56
|
-
|
|
57
|
-
**Note:** Version bump only for package babel-plugin-hylid-bridge
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
## [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)
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
### Features
|
|
67
|
-
|
|
68
|
-
* 避免频繁打印日志* 不修改全局的 my, 新建本地变量* 调整 eslint 配置* 根据源码注入 hylid-bridge 相应模块* 固定三方依赖版本号* 适配 Remix 中会编译所有 node_modules 下模块的逻辑
|
|
69
|
-
|
|
70
|
-
### Bug Fixes
|
|
71
|
-
|
|
72
|
-
* 依赖* build error* split the target
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
## [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)
|
|
76
|
-
|
|
77
|
-
**Note:** Version bump only for package babel-plugin-hylid-bridge
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
## [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)
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
### Features
|
|
87
|
-
|
|
88
|
-
* 避免频繁打印日志* 适配 Remix 中会编译所有 node_modules 下模块的逻辑
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
## [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)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
### Features
|
|
95
|
-
|
|
96
|
-
* 不修改全局的 my, 新建本地变量
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
## [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)
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
### Features
|
|
103
|
-
|
|
104
|
-
* 调整 eslint 配置* 根据源码注入 hylid-bridge 相应模块* 固定三方依赖版本号
|
|
105
|
-
|
|
106
|
-
### Bug Fixes
|
|
107
|
-
|
|
108
|
-
* 依赖* build error* split the target
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
## [2.3.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.1.0...v2.3.0) (2022-08-05)
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
### Features
|
|
115
|
-
|
|
116
|
-
* 调整 eslint 配置
|
|
117
|
-
|
|
118
|
-
### Bug Fixes
|
|
119
|
-
|
|
120
|
-
* 依赖* build error* split the target
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
## [2.2.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v2.1.0...v2.2.0) (2022-07-08)
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
### Features
|
|
127
|
-
|
|
128
|
-
* 调整 eslint 配置
|
|
129
|
-
|
|
130
|
-
### Bug Fixes
|
|
131
|
-
|
|
132
|
-
* 依赖* build error* split the target
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
# [2.1.0](https://code.alipay.com/ant-ife/hylid-bridge/compare/v1.1.0...v2.1.0) (2022-04-25)
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
### Features
|
|
139
|
-
|
|
140
|
-
* 接入 babel 插件 ([fdb8885](https://code.alipay.com/ant-ife/hylid-bridge/commits/fdb8885c6d7f16b8fbc528540ff3b397096da60b))
|
|
141
|
-
* 统一版本号 ([7539c2f](https://code.alipay.com/ant-ife/hylid-bridge/commits/7539c2f9fa4646fde964b609259e61d5e008c511))
|