babel-plugin-hylid-bridge 3.3.0-alpha.13 → 3.3.0-alpha.19
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 +57 -0
- package/lib/getLibraryDir.d.ts +1 -0
- package/lib/getLibraryDir.js +22 -0
- package/lib/helper.d.ts +24 -0
- package/lib/helper.js +119 -0
- package/lib/index.d.ts +14 -0
- package/lib/index.js +203 -0
- package/lib/utils.d.ts +3 -0
- package/lib/utils.js +31 -0
- package/package.json +2 -2
|
@@ -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,57 @@
|
|
|
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 outInnerPath = _path.default.join(lib, 'lib', 'babel-compact-inner.js');
|
|
52
|
+
var outExportPath = _path.default.join(lib, 'lib', 'babel-compact.js');
|
|
53
|
+
var codes = ['// auto generated', "export * from './index'", (0, _generator.default)(ast).code];
|
|
54
|
+
var exportCodes = ['// auto generated', "export * from './babel-compact-inner'", "export * as default from './babel-compact-inner'"];
|
|
55
|
+
_fsExtra.default.writeFileSync(outExportPath, exportCodes.join('\n'));
|
|
56
|
+
_fsExtra.default.writeFileSync(outInnerPath, codes.join('\n'));
|
|
57
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function getLibraryDir(): string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = getLibraryDir;
|
|
7
|
+
var _utils = require("./utils");
|
|
8
|
+
var libraryDir;
|
|
9
|
+
function getLibraryDir() {
|
|
10
|
+
if (!libraryDir) {
|
|
11
|
+
var cwd = process.cwd();
|
|
12
|
+
try {
|
|
13
|
+
libraryDir = process.env.HYLID_BRIDGE_PACKAGE_DIR || require.resolve('hylid-bridge/package.json', {
|
|
14
|
+
paths: [cwd]
|
|
15
|
+
}).replace('package.json', '');
|
|
16
|
+
(0, _utils.log)("Find the hylid-bridge package: ".concat(libraryDir));
|
|
17
|
+
} catch (e) {
|
|
18
|
+
(0, _utils.error)("Can not resolve the hylid-bridge package from the directory: ".concat(cwd, "."), e);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return libraryDir;
|
|
22
|
+
}
|
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 && {}.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.toLowerCase());
|
|
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
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type babelCore from '@babel/core';
|
|
2
|
+
import { PluginPass } from '@babel/core';
|
|
3
|
+
interface IState extends PluginPass {
|
|
4
|
+
usedJSAPIList: 'all' | string[];
|
|
5
|
+
}
|
|
6
|
+
declare const _default: (core: typeof babelCore, opts?: {
|
|
7
|
+
targets?: string[];
|
|
8
|
+
appEnvPath?: string;
|
|
9
|
+
libraryDir?: string;
|
|
10
|
+
polyfillDir?: string;
|
|
11
|
+
transformMy?: boolean;
|
|
12
|
+
compact?: boolean;
|
|
13
|
+
}) => babelCore.PluginObj<IState>;
|
|
14
|
+
export default _default;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
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.default = void 0;
|
|
9
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
10
|
+
var _path = _interopRequireDefault(require("path"));
|
|
11
|
+
var _fsExtra = _interopRequireDefault(require("fs-extra"));
|
|
12
|
+
var _lodash = _interopRequireDefault(require("lodash"));
|
|
13
|
+
var babelTypes = _interopRequireWildcard(require("@babel/types"));
|
|
14
|
+
var _getLibraryDir = _interopRequireDefault(require("./getLibraryDir"));
|
|
15
|
+
var utils = _interopRequireWildcard(require("./utils"));
|
|
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 && {}.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
|
+
/**
|
|
22
|
+
* https://astexplorer.net/
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
var identifierPrefix = '__hylid_bridge__';
|
|
26
|
+
var libraryName = 'hylid-bridge';
|
|
27
|
+
var myLocalName = "".concat(identifierPrefix, "my__");
|
|
28
|
+
var _default = exports.default = function _default(core, opts) {
|
|
29
|
+
var _opts$libraryDir, _opts$transformMy, _opts$compact;
|
|
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
|
+
var compact = (_opts$compact = opts === null || opts === void 0 ? void 0 : opts.compact) !== null && _opts$compact !== void 0 ? _opts$compact : true;
|
|
36
|
+
// 生成 babel-compact 文件
|
|
37
|
+
compact && (0, _generateJSAPIs.default)(targets);
|
|
38
|
+
return {
|
|
39
|
+
name: 'babel-plugin-hylid-bridge',
|
|
40
|
+
visitor: {
|
|
41
|
+
ImportDeclaration: function ImportDeclaration(path) {
|
|
42
|
+
if (!compact) return;
|
|
43
|
+
if (path.node.source.value === libraryName) {
|
|
44
|
+
path.node.source = core.types.stringLiteral("".concat(libraryName, "/babel-compact"));
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
ExportNamedDeclaration: function ExportNamedDeclaration(path) {
|
|
48
|
+
var _path$node$source;
|
|
49
|
+
if (!compact) return;
|
|
50
|
+
if (((_path$node$source = path.node.source) === null || _path$node$source === void 0 ? void 0 : _path$node$source.value) === libraryName) {
|
|
51
|
+
path.node.source = core.types.stringLiteral("".concat(libraryName, "/babel-compact"));
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
MemberExpression: function MemberExpression(path, state) {
|
|
55
|
+
var _getNames = (0, _helper.getNames)(path.node, path.scope, state, targets),
|
|
56
|
+
objectName = _getNames.objectName,
|
|
57
|
+
propertyName = _getNames.propertyName;
|
|
58
|
+
if (propertyName === 'call' || objectName !== 'my' || !transformMy) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
var newNode = core.types.cloneNode(path.node, true, true);
|
|
62
|
+
newNode.object = core.types.identifier(myLocalName);
|
|
63
|
+
if (state.usedJSAPIList === 'all') {
|
|
64
|
+
path.replaceWith(newNode);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (!propertyName && path.node.computed) {
|
|
68
|
+
state.usedJSAPIList = 'all';
|
|
69
|
+
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, "`."));
|
|
70
|
+
path.replaceWith(newNode);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (propertyName) {
|
|
74
|
+
state.usedJSAPIList = state.usedJSAPIList || [];
|
|
75
|
+
var name = propertyName;
|
|
76
|
+
if ((0, _helper.isJSAPIInTargets)(name, targets, state.filename, libraryDir, polyfillDir)) {
|
|
77
|
+
state.usedJSAPIList.push(name);
|
|
78
|
+
} else {
|
|
79
|
+
state.usedJSAPIList = 'all';
|
|
80
|
+
}
|
|
81
|
+
path.replaceWith(newNode);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
CallExpression: function CallExpression(path, state) {
|
|
86
|
+
if (path.node.callee.type !== 'MemberExpression' || !transformMy) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
var _getNames2 = (0, _helper.getNames)(path.node.callee, path.scope, state, targets),
|
|
90
|
+
objectName = _getNames2.objectName,
|
|
91
|
+
propertyName = _getNames2.propertyName;
|
|
92
|
+
if (objectName !== 'my' || propertyName !== 'call' || !path.node.arguments[0] || path.node.arguments[0].type !== 'StringLiteral') {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
var newNode = core.types.callExpression(
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
babelTypes.memberExpression(babelTypes.identifier(myLocalName), babelTypes.identifier(path.node.arguments[0].value)), path.node.arguments.slice(1));
|
|
98
|
+
if (state.usedJSAPIList === 'all') {
|
|
99
|
+
path.replaceWith(newNode);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
state.usedJSAPIList = state.usedJSAPIList || [];
|
|
103
|
+
var name = path.node.arguments[0].value;
|
|
104
|
+
if ((0, _helper.isJSAPIInTargets)(name, targets, state.filename, libraryDir, polyfillDir)) {
|
|
105
|
+
state.usedJSAPIList.push(name);
|
|
106
|
+
} else {
|
|
107
|
+
state.usedJSAPIList = 'all';
|
|
108
|
+
}
|
|
109
|
+
path.replaceWith(newNode);
|
|
110
|
+
},
|
|
111
|
+
Program: {
|
|
112
|
+
exit: function exit(path, state) {
|
|
113
|
+
if (!state.usedJSAPIList) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
var usedJSAPIList = state.usedJSAPIList || [];
|
|
117
|
+
var appEnvLocalName = "".concat(identifierPrefix, "appEnv__");
|
|
118
|
+
if (path.scope.hasBinding(appEnvLocalName)) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
var innerAppEnvLocalName = "".concat(identifierPrefix, "innerAppEnv__");
|
|
122
|
+
var userAppEnvLocalName = "".concat(identifierPrefix, "userAppEnv__");
|
|
123
|
+
var nodes = [babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(innerAppEnvLocalName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/appEnv")))];
|
|
124
|
+
if (userAppEnvPath) {
|
|
125
|
+
var relativePath = _path.default.relative(_path.default.dirname(state.filename), userAppEnvPath.replace(/\.ts$/, ''));
|
|
126
|
+
nodes.push(babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(userAppEnvLocalName))], babelTypes.stringLiteral(relativePath.startsWith('../') ? relativePath : "./".concat(relativePath))));
|
|
127
|
+
} else {
|
|
128
|
+
nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(userAppEnvLocalName), babelTypes.objectExpression([]))]));
|
|
129
|
+
}
|
|
130
|
+
var objectSpreadName = "".concat(identifierPrefix, "objectSpread__");
|
|
131
|
+
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)]))]));
|
|
132
|
+
if (usedJSAPIList === 'all') {
|
|
133
|
+
nodes.push.apply(nodes, (0, _toConsumableArray2.default)(targets.map(function (target) {
|
|
134
|
+
var targetSplit = (0, _helper.splitTarget)(target);
|
|
135
|
+
var innerIndexPath = "".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/index");
|
|
136
|
+
var polyfillIndexPath = polyfillDir && _path.default.resolve(polyfillDir, targetSplit[0], targetSplit[1], 'index');
|
|
137
|
+
var normalizedIndexPath = polyfillIndexPath && (_fsExtra.default.existsSync("".concat(polyfillIndexPath, ".js")) || _fsExtra.default.existsSync("".concat(polyfillIndexPath, ".ts"))) ? (0, _combineExports.default)(require.resolve(innerIndexPath), polyfillIndexPath) : innerIndexPath;
|
|
138
|
+
return babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__")))], babelTypes.stringLiteral(normalizedIndexPath));
|
|
139
|
+
})));
|
|
140
|
+
var _buildElseIf = function buildElseIf(targetList) {
|
|
141
|
+
if (!targetList.length) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
var target = targetList[0];
|
|
145
|
+
var targetSplit = (0, _helper.splitTarget)(target);
|
|
146
|
+
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)));
|
|
147
|
+
};
|
|
148
|
+
nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(myLocalName))]));
|
|
149
|
+
// Do not generate the `if statement` when there is just one target.
|
|
150
|
+
if (targets.length === 1) {
|
|
151
|
+
var targetSplit = (0, _helper.splitTarget)(targets[0]);
|
|
152
|
+
nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__")))));
|
|
153
|
+
} else {
|
|
154
|
+
var elseIfNode = _buildElseIf(targets);
|
|
155
|
+
elseIfNode && nodes.push(elseIfNode);
|
|
156
|
+
}
|
|
157
|
+
} else if (usedJSAPIList.length) {
|
|
158
|
+
var normalizedJSAPIList = _lodash.default.uniq(usedJSAPIList);
|
|
159
|
+
normalizedJSAPIList.forEach(function (name) {
|
|
160
|
+
targets.forEach(function (target) {
|
|
161
|
+
var targetSplit = (0, _helper.splitTarget)(target);
|
|
162
|
+
var localName = "".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__");
|
|
163
|
+
var innerSourcePath = "".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name);
|
|
164
|
+
var userSourcePath = polyfillDir ? _path.default.resolve(polyfillDir, targetSplit[0], targetSplit[1], name) : undefined;
|
|
165
|
+
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));
|
|
166
|
+
nodes.push(n);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
var _buildElseIf2 = function buildElseIf(targetList) {
|
|
170
|
+
if (!targetList.length) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
var target = targetList[0];
|
|
174
|
+
var targetSplit = (0, _helper.splitTarget)(target);
|
|
175
|
+
var propertyList = normalizedJSAPIList.map(function (name) {
|
|
176
|
+
return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__")));
|
|
177
|
+
});
|
|
178
|
+
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)))]), _buildElseIf2(targetList.slice(1)));
|
|
179
|
+
};
|
|
180
|
+
nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(myLocalName))]));
|
|
181
|
+
// Do not generate the `if statement` when there is just one target.
|
|
182
|
+
if (targets.length === 1) {
|
|
183
|
+
var _targetSplit = (0, _helper.splitTarget)(targets[0]);
|
|
184
|
+
var propertyList = normalizedJSAPIList.map(function (name) {
|
|
185
|
+
return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(_targetSplit[0], "_").concat(_targetSplit[1], "_").concat(name, "__")));
|
|
186
|
+
});
|
|
187
|
+
nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.objectExpression(propertyList))));
|
|
188
|
+
} else {
|
|
189
|
+
var _elseIfNode = _buildElseIf2(targets);
|
|
190
|
+
_elseIfNode && nodes.push(_elseIfNode);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (nodes.length) {
|
|
194
|
+
path.unshiftContainer('body', nodes);
|
|
195
|
+
}
|
|
196
|
+
if (targets.length && (usedJSAPIList === 'all' || usedJSAPIList.length)) {
|
|
197
|
+
utils.log("Successfully inject the Hylid-bridge into ".concat(state.filename, " to support the targets: ").concat(targets.join(','), "."));
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
};
|
package/lib/utils.d.ts
ADDED
package/lib/utils.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.error = error;
|
|
8
|
+
exports.log = log;
|
|
9
|
+
exports.warn = warn;
|
|
10
|
+
var _chalk = _interopRequireDefault(require("chalk"));
|
|
11
|
+
function log() {
|
|
12
|
+
var _console;
|
|
13
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
14
|
+
args[_key] = arguments[_key];
|
|
15
|
+
}
|
|
16
|
+
(_console = console).log.apply(_console, [_chalk.default.bold.green('[LOG]')].concat(args));
|
|
17
|
+
}
|
|
18
|
+
function error() {
|
|
19
|
+
var _console2;
|
|
20
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
21
|
+
args[_key2] = arguments[_key2];
|
|
22
|
+
}
|
|
23
|
+
(_console2 = console).error.apply(_console2, [_chalk.default.bold.red('[ERROR]')].concat(args));
|
|
24
|
+
}
|
|
25
|
+
function warn() {
|
|
26
|
+
var _console3;
|
|
27
|
+
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
|
28
|
+
args[_key3] = arguments[_key3];
|
|
29
|
+
}
|
|
30
|
+
(_console3 = console).error.apply(_console3, [_chalk.default.bold.yellow('[WARN]')].concat(args));
|
|
31
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "babel-plugin-hylid-bridge",
|
|
3
|
-
"version": "3.3.0-alpha.
|
|
3
|
+
"version": "3.3.0-alpha.19",
|
|
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": "^3.3.0-alpha.
|
|
25
|
+
"hylid-bridge": "^3.3.0-alpha.19"
|
|
26
26
|
},
|
|
27
27
|
"sideEffects": false,
|
|
28
28
|
"registry": "https://registry.npmjs.org/",
|