babel-plugin-hylid-bridge 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
File without changes
@@ -0,0 +1 @@
1
+ export default function getLibraryDir(): string;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = getLibraryDir;
7
+
8
+ var _utils = require("./utils");
9
+
10
+ var libraryDir;
11
+
12
+ function getLibraryDir() {
13
+ if (!libraryDir) {
14
+ var cwd = process.cwd();
15
+
16
+ try {
17
+ libraryDir = process.env.HYLID_BRIDGE_PACKAGE_DIR || require.resolve('hylid-bridge/package.json', {
18
+ paths: [cwd]
19
+ }).replace('package.json', '');
20
+ } catch (e) {
21
+ (0, _utils.error)("Can not resolve the hylid-bridge package from the directory: ".concat(cwd, "."), e);
22
+ }
23
+ }
24
+
25
+ (0, _utils.log)("Find the hylid-bridge package: ".concat(libraryDir));
26
+ return libraryDir;
27
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,7 @@
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: (_: typeof babelCore, opts?: Record<string, any> | undefined) => babelCore.PluginObj<IState>;
7
+ export default _default;
package/lib/index.js ADDED
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _typeof = require("@babel/runtime/helpers/typeof");
6
+
7
+ Object.defineProperty(exports, "__esModule", {
8
+ value: true
9
+ });
10
+ exports.default = void 0;
11
+
12
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
+
14
+ var _path = _interopRequireDefault(require("path"));
15
+
16
+ var _fsExtra = _interopRequireDefault(require("fs-extra"));
17
+
18
+ var _lodash = _interopRequireDefault(require("lodash"));
19
+
20
+ var babelTypes = _interopRequireWildcard(require("@babel/types"));
21
+
22
+ var _getLibraryDir = _interopRequireDefault(require("./getLibraryDir"));
23
+
24
+ var utils = _interopRequireWildcard(require("./utils"));
25
+
26
+ 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); }
27
+
28
+ 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; }
29
+
30
+ /**
31
+ * https://astexplorer.net/
32
+ */
33
+ var identifierPrefix = '__hylid_bridge__';
34
+ var libraryName = 'hylid-bridge';
35
+ var libraryDir = (0, _getLibraryDir.default)();
36
+
37
+ function getTargets(customTargets) {
38
+ var targets = customTargets || (process.env.HYLID_BRIDGE_TARGETS ? process.env.HYLID_BRIDGE_TARGETS.split(',') : []);
39
+ return targets;
40
+ }
41
+
42
+ function isApiExists(name, target) {
43
+ var targetSplit = target.split('|');
44
+
45
+ if (targetSplit.length !== 2) {
46
+ throw new Error("Wrong target configuration: ".concat(target, "."));
47
+ }
48
+
49
+ return _fsExtra.default.existsSync(_path.default.resolve(libraryDir, "./lib/clients/".concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name, ".js")));
50
+ }
51
+
52
+ var _default = function _default(_, opts) {
53
+ var targets = getTargets(opts === null || opts === void 0 ? void 0 : opts.targets);
54
+ return {
55
+ name: 'babel-plugin-hylid-bridge',
56
+ visitor: {
57
+ MemberExpression: function MemberExpression(path, state) {
58
+ var _this = this;
59
+
60
+ var objectName = path.node.object.name;
61
+ var propertyName = path.node.property.name;
62
+
63
+ if (!targets || !targets.length || objectName !== 'my' || path.scope.hasBinding('my')) {
64
+ return;
65
+ }
66
+
67
+ if (state.usedJSAPIList === 'all') {
68
+ return;
69
+ }
70
+
71
+ if (path.node.computed) {
72
+ state.usedJSAPIList = 'all';
73
+ utils.warn("Detect visiting the property of `my` with computed mode: my[".concat(propertyName, "]. Fallback to import all API codes of ").concat(targets.join(','), ". Current file path: `").concat(this.file.opts.filename, "`."));
74
+ } else {
75
+ state.usedJSAPIList = state.usedJSAPIList || [];
76
+ var name = propertyName;
77
+
78
+ if (targets.every(function (target) {
79
+ var targetSplit = target.split('|');
80
+
81
+ if (!isApiExists(name, target)) {
82
+ utils.warn("Can not find the API `".concat(name, "` in `").concat(_path.default.resolve(libraryDir, './lib/clients/' + targetSplit[0] + '/' + targetSplit[1]), "`. Fallback to import all API codes of ").concat(target, ". Current file path: `").concat(_this.file.opts.filename, "`."));
83
+ return false;
84
+ }
85
+
86
+ return true;
87
+ })) {
88
+ state.usedJSAPIList.push(name);
89
+ } else {
90
+ state.usedJSAPIList = 'all';
91
+ }
92
+ }
93
+ },
94
+ Program: {
95
+ exit: function exit(path, state) {
96
+ if (!state.usedJSAPIList) {
97
+ return;
98
+ }
99
+
100
+ var usedJSAPIList = state.usedJSAPIList || [];
101
+ var appEnvLocalName = "".concat(identifierPrefix, "appEnv__");
102
+
103
+ if (path.scope.hasBinding(appEnvLocalName)) {
104
+ return;
105
+ }
106
+
107
+ var nodes = [babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(appEnvLocalName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/appEnv")))];
108
+
109
+ if (usedJSAPIList === 'all') {
110
+ nodes.push.apply(nodes, (0, _toConsumableArray2.default)(targets.map(function (target) {
111
+ var targetSplit = target.split('|');
112
+ return babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__")))], babelTypes.stringLiteral("".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/index")));
113
+ })));
114
+
115
+ var buildElseIf = function buildElseIf(targetList) {
116
+ if (!targetList.length) {
117
+ return;
118
+ }
119
+
120
+ var target = targetList[0];
121
+ var targetSplit = target.split('|');
122
+ return babelTypes.ifStatement(babelTypes.identifier("".concat(appEnvLocalName, ".").concat(_lodash.default.camelCase("is_".concat(targetSplit[0], "_").concat(targetSplit[1])))), babelTypes.blockStatement([babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier('my'), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__"))))]), buildElseIf(targetList.slice(1)));
123
+ };
124
+
125
+ nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier('my'))])); // Do not generate the `if statement` when there is just one target.
126
+
127
+ if (targets.length === 1) {
128
+ var targetSplit = targets[0].split('|');
129
+ nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier('my'), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__")))));
130
+ } else {
131
+ var elseIfNode = buildElseIf(targets);
132
+ elseIfNode && nodes.push(elseIfNode);
133
+ }
134
+ } else if (usedJSAPIList.length) {
135
+ var normalizedJSAPIList = _lodash.default.uniq(usedJSAPIList);
136
+
137
+ normalizedJSAPIList.forEach(function (name) {
138
+ targets.forEach(function (target) {
139
+ var localName = "".concat(identifierPrefix).concat(target, "_").concat(name, "__");
140
+ var targetSplit = target.split('|');
141
+ var n = babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(localName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name)));
142
+ nodes.push(n);
143
+ });
144
+ });
145
+
146
+ var _buildElseIf = function _buildElseIf(targetList) {
147
+ if (!targetList.length) {
148
+ return;
149
+ }
150
+
151
+ var target = targetList[0];
152
+ var targetSplit = target.split('|');
153
+ var propertyList = normalizedJSAPIList.map(function (name) {
154
+ return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__")));
155
+ });
156
+ return babelTypes.ifStatement(babelTypes.identifier("".concat(appEnvLocalName, ".").concat(_lodash.default.camelCase("is_".concat(targetSplit[0], "_").concat(targetSplit[1])))), babelTypes.blockStatement([babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier('my'), babelTypes.objectExpression(propertyList)))]), _buildElseIf(targetList.slice(1)));
157
+ };
158
+
159
+ nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier('my'))])); // Do not generate the `if statement` when there is just one target.
160
+
161
+ if (targets.length === 1) {
162
+ var _targetSplit = targets[0].split('|');
163
+
164
+ var propertyList = normalizedJSAPIList.map(function (name) {
165
+ return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(_targetSplit[0], "_").concat(_targetSplit[1], "_").concat(name, "__")));
166
+ });
167
+ nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier('my'), babelTypes.objectExpression(propertyList))));
168
+ } else {
169
+ var _elseIfNode = _buildElseIf(targets);
170
+
171
+ _elseIfNode && nodes.push(_elseIfNode);
172
+ }
173
+ }
174
+
175
+ if (nodes.length) {
176
+ path.unshiftContainer('body', nodes);
177
+ }
178
+ }
179
+ }
180
+ }
181
+ };
182
+ };
183
+
184
+ exports.default = _default;
package/lib/utils.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare function log(...args: Parameters<typeof console.log>): void;
2
+ export declare function error(...args: Parameters<typeof console.error>): void;
3
+ export declare function warn(...args: Parameters<typeof console.error>): void;
package/lib/utils.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.error = error;
9
+ exports.log = log;
10
+ exports.warn = warn;
11
+
12
+ var _chalk = _interopRequireDefault(require("chalk"));
13
+
14
+ function log() {
15
+ var _console;
16
+
17
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
18
+ args[_key] = arguments[_key];
19
+ }
20
+
21
+ (_console = console).log.apply(_console, [_chalk.default.bold.green('[LOG]')].concat(args));
22
+ }
23
+
24
+ function error() {
25
+ var _console2;
26
+
27
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
28
+ args[_key2] = arguments[_key2];
29
+ }
30
+
31
+ (_console2 = console).error.apply(_console2, [_chalk.default.bold.red('[ERROR]')].concat(args));
32
+ }
33
+
34
+ function warn() {
35
+ var _console3;
36
+
37
+ for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
38
+ args[_key3] = arguments[_key3];
39
+ }
40
+
41
+ (_console3 = console).error.apply(_console3, [_chalk.default.bold.yellow('[WARN]')].concat(args));
42
+ }
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "babel-plugin-hylid-bridge",
3
+ "version": "0.0.1",
4
+ "description": "babel-plugin-hylid-bridge",
5
+ "main": "lib/index.js",
6
+ "scripts": {
7
+ "build": "gulp build",
8
+ "dev": "gulp dev"
9
+ },
10
+ "files": [
11
+ "lib"
12
+ ],
13
+ "keywords": [],
14
+ "license": "MIT",
15
+ "dependencies": {
16
+ "@babel/core": "^7.15.6",
17
+ "@babel/types": "^7.15.6",
18
+ "@types/fs-extra": "^9.0.13",
19
+ "@types/lodash": "^4.14.176",
20
+ "chalk": "^4.1.2",
21
+ "fs-extra": "^10.0.0",
22
+ "lodash": "^4.17.21"
23
+ },
24
+ "devDependencies": {
25
+ "gulp": "4.0.2",
26
+ "gulp-plumber": "^1.2.1",
27
+ "gulp-babel": "8.0.0",
28
+ "gulp-typescript": "5.0.1"
29
+ },
30
+ "sideEffects": false,
31
+ "registry": "https://registry.npmjs.org/"
32
+ }