babel-plugin-hylid-bridge 2.12.0-alpha.2 → 2.12.0-alpha.4

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 CHANGED
@@ -3,6 +3,24 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [2.12.0-alpha.4] (2023-07-20)
7
+
8
+
9
+ ### Features
10
+
11
+ * 指定编译 src 目录
12
+
13
+
14
+
15
+ ## [2.12.0-alpha.3] (2023-07-20)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * type error
21
+
22
+
23
+
6
24
  ## [2.12.0-alpha.2] (2023-07-20)
7
25
 
8
26
  **Note:** Version bump only for package babel-plugin-hylid-bridge
@@ -0,0 +1 @@
1
+ export default function combileExports(index1Path: string, index2Path: string): string;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = combileExports;
8
+ var _path = _interopRequireDefault(require("path"));
9
+ var _fsExtra = _interopRequireDefault(require("fs-extra"));
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');
13
+ _fsExtra.default.ensureFileSync(indexPath);
14
+ _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
+ return indexPath;
16
+ }
package/lib/index.d.ts CHANGED
@@ -3,5 +3,10 @@ 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?: Record<string, any>) => babelCore.PluginObj<IState>;
6
+ declare const _default: (core: typeof babelCore, opts?: {
7
+ targets?: string[];
8
+ appEnvPath?: string;
9
+ libraryDir?: string;
10
+ polyfillDir?: string;
11
+ }) => babelCore.PluginObj<IState>;
7
12
  export default _default;
package/lib/index.js CHANGED
@@ -13,6 +13,7 @@ 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
+ var _combineExports = _interopRequireDefault(require("./combineExports"));
16
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); }
17
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; }
18
19
  /**
@@ -21,15 +22,13 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
21
22
 
22
23
  var identifierPrefix = '__hylid_bridge__';
23
24
  var libraryName = 'hylid-bridge';
24
- var libraryDir = (0, _getLibraryDir.default)();
25
25
  function snakeCaseTarget(value) {
26
26
  if (value === 'H5') {
27
27
  return 'h5';
28
28
  }
29
29
  return _lodash.default.snakeCase(value);
30
30
  }
31
- function isTargetSupported(target) {
32
- var libraryDir = (0, _getLibraryDir.default)();
31
+ function isTargetSupported(target, libraryDir) {
33
32
  return ['MpWeb', 'Mp', 'Web'].some(function (prefix) {
34
33
  if (target.startsWith(prefix)) {
35
34
  var targetClientDir = _path.default.resolve(libraryDir, 'lib/clients', snakeCaseTarget(prefix), snakeCaseTarget(target.replace(prefix, '')));
@@ -38,16 +37,18 @@ function isTargetSupported(target) {
38
37
  return false;
39
38
  });
40
39
  }
41
- function getTargets(customTargets) {
40
+ function getTargets(libraryDir, customTargets) {
42
41
  var targets = customTargets || (process.env.HYLID_BRIDGE_TARGETS ? process.env.HYLID_BRIDGE_TARGETS.split(',') : []);
43
- return targets.filter(isTargetSupported);
42
+ return targets.filter(function (target) {
43
+ return isTargetSupported(target, libraryDir);
44
+ });
44
45
  }
45
- function isApiExists(name, target) {
46
+ function isApiExists(name, target, libraryDir, polyfillDir) {
46
47
  var targetSplit = splitTarget(target);
47
48
  if (targetSplit.length !== 2) {
48
49
  throw new Error("Wrong target configuration: ".concat(target, "."));
49
50
  }
50
- return _fsExtra.default.existsSync(_path.default.resolve(libraryDir, "./lib/clients/".concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name, ".js")));
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")));
51
52
  }
52
53
  function getUserAppEnvPath(appEnvPath) {
53
54
  var realAppEnvPath = appEnvPath !== null && appEnvPath !== void 0 ? appEnvPath : _path.default.resolve(process.cwd(), './src/appEnv.ts');
@@ -70,23 +71,34 @@ function splitTarget(target) {
70
71
  var myLocalName = "".concat(identifierPrefix, "my__");
71
72
  function getNames(node, scope, state, targets) {
72
73
  var _state$filename;
74
+ // my.alert || my[a]
73
75
  if (node.object.type === 'Identifier' && node.property.type === 'Identifier' &&
74
76
  // Remix 中会编译所有 node_modules 下的模块
75
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')) {
76
- return {
78
+ return node.computed ? {
79
+ objectName: node.object.name,
80
+ propertyName: undefined
81
+ } : {
77
82
  objectName: node.object.name,
78
83
  propertyName: node.property.name
79
84
  };
80
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
+ }
81
93
  return {
82
94
  objectName: undefined,
83
95
  propertyName: undefined
84
96
  };
85
97
  }
86
- function isJSAPIInTargets(jsapiName, targets, filename) {
98
+ function isJSAPIInTargets(jsapiName, targets, filename, libraryDir, polyfillDir) {
87
99
  return targets.every(function (target) {
88
100
  var targetSplit = splitTarget(target);
89
- if (!isApiExists(jsapiName, target)) {
101
+ if (!isApiExists(jsapiName, target, libraryDir, polyfillDir)) {
90
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, "`."));
91
103
  return false;
92
104
  }
@@ -94,8 +106,11 @@ function isJSAPIInTargets(jsapiName, targets, filename) {
94
106
  });
95
107
  }
96
108
  var _default = function _default(core, opts) {
97
- var targets = getTargets(opts === null || opts === void 0 ? void 0 : opts.targets);
109
+ var _opts$libraryDir;
110
+ 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);
98
112
  var userAppEnvPath = getUserAppEnvPath(opts === null || opts === void 0 ? void 0 : opts.appEnvPath);
113
+ var polyfillDir = opts === null || opts === void 0 ? void 0 : opts.polyfillDir;
99
114
  return {
100
115
  name: 'babel-plugin-hylid-bridge',
101
116
  visitor: {
@@ -103,7 +118,7 @@ var _default = function _default(core, opts) {
103
118
  var _getNames = getNames(path.node, path.scope, state, targets),
104
119
  objectName = _getNames.objectName,
105
120
  propertyName = _getNames.propertyName;
106
- if (!propertyName || propertyName === 'call' || objectName !== 'my') {
121
+ if (propertyName === 'call' || objectName !== 'my') {
107
122
  return;
108
123
  }
109
124
  var newNode = core.types.cloneNode(path.node, true, true);
@@ -112,19 +127,23 @@ var _default = function _default(core, opts) {
112
127
  path.replaceWith(newNode);
113
128
  return;
114
129
  }
115
- if (path.node.computed) {
130
+ if (!propertyName && path.node.computed) {
116
131
  state.usedJSAPIList = 'all';
117
- 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(state.filename, "`."));
118
- } else {
132
+ 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, "`."));
133
+ path.replaceWith(newNode);
134
+ return;
135
+ }
136
+ if (propertyName) {
119
137
  state.usedJSAPIList = state.usedJSAPIList || [];
120
138
  var name = propertyName;
121
- if (isJSAPIInTargets(name, targets, state.filename)) {
139
+ if (isJSAPIInTargets(name, targets, state.filename, libraryDir, polyfillDir)) {
122
140
  state.usedJSAPIList.push(name);
123
141
  } else {
124
142
  state.usedJSAPIList = 'all';
125
143
  }
144
+ path.replaceWith(newNode);
145
+ return;
126
146
  }
127
- path.replaceWith(newNode);
128
147
  },
129
148
  CallExpression: function CallExpression(path, state) {
130
149
  if (path.node.callee.type !== 'MemberExpression') {
@@ -143,7 +162,7 @@ var _default = function _default(core, opts) {
143
162
  }
144
163
  state.usedJSAPIList = state.usedJSAPIList || [];
145
164
  var name = path.node.arguments[0].value;
146
- if (isJSAPIInTargets(name, targets, state.filename)) {
165
+ if (isJSAPIInTargets(name, targets, state.filename, libraryDir, polyfillDir)) {
147
166
  state.usedJSAPIList.push(name);
148
167
  } else {
149
168
  state.usedJSAPIList = 'all';
@@ -174,7 +193,9 @@ var _default = function _default(core, opts) {
174
193
  if (usedJSAPIList === 'all') {
175
194
  nodes.push.apply(nodes, (0, _toConsumableArray2.default)(targets.map(function (target) {
176
195
  var targetSplit = splitTarget(target);
177
- 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")));
196
+ var innerIndexPath = "".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/index");
197
+ var normalizedIndexPath = polyfillDir ? (0, _combineExports.default)(require.resolve(innerIndexPath), _path.default.resolve(polyfillDir, targetSplit[0], targetSplit[1])) : innerIndexPath;
198
+ return babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__")))], babelTypes.stringLiteral(normalizedIndexPath));
178
199
  })));
179
200
  var buildElseIf = function buildElseIf(targetList) {
180
201
  if (!targetList.length) {
@@ -199,7 +220,9 @@ var _default = function _default(core, opts) {
199
220
  targets.forEach(function (target) {
200
221
  var targetSplit = splitTarget(target);
201
222
  var localName = "".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__");
202
- var n = babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(localName))], babelTypes.stringLiteral("".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name)));
223
+ var innerSourcePath = "".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name);
224
+ var userSourcePath = polyfillDir ? _path.default.resolve(polyfillDir, targetSplit[0], targetSplit[1], name) : undefined;
225
+ 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
226
  nodes.push(n);
204
227
  });
205
228
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-hylid-bridge",
3
- "version": "2.12.0-alpha.2",
3
+ "version": "2.12.0-alpha.4",
4
4
  "description": "babel-plugin-hylid-bridge",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -16,8 +16,13 @@
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
+ "babel-plugin-tester": "^10.1.0",
24
+ "hylid-bridge": "^2.12.0-alpha.4"
25
+ },
21
26
  "sideEffects": false,
22
27
  "registry": "https://registry.npmjs.org/"
23
28
  }