babel-plugin-hylid-bridge
Advanced tools
Comparing version 2.12.0-alpha.7 to 2.12.0-alpha.9
@@ -1,1 +0,1 @@ | ||
export default function combileExports(index1Path: string, index2Path: string): string; | ||
export default function combieExports(index1Path: string, index2Path: string): string; |
@@ -7,8 +7,9 @@ "use strict"; | ||
}); | ||
exports.default = combileExports; | ||
exports.default = combieExports; | ||
var _path = _interopRequireDefault(require("path")); | ||
var _fsExtra = _interopRequireDefault(require("fs-extra")); | ||
var _hashIt = _interopRequireDefault(require("hash-it")); | ||
function combileExports(index1Path, index2Path) { | ||
var indexPath = _path.default.resolve(__dirname, '..', '.temp', (0, _hashIt.default)("".concat(index1Path).concat(index2Path)) + '.js'); | ||
var isTest = process.env.NODE_ENV === 'test'; | ||
function combieExports(index1Path, index2Path) { | ||
var indexPath = _path.default.resolve(__dirname, '..', '.temp', (isTest ? 'test' : (0, _hashIt.default)("".concat(index1Path).concat(index2Path))) + '.js'); | ||
_fsExtra.default.ensureFileSync(indexPath); | ||
@@ -15,0 +16,0 @@ _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 ")); |
@@ -11,3 +11,4 @@ import type babelCore from '@babel/core'; | ||
polyfillDir?: string; | ||
transformMy?: boolean; | ||
}) => babelCore.PluginObj<IState>; | ||
export default _default; |
145
lib/index.js
@@ -17,4 +17,6 @@ "use strict"; | ||
var _combineExports = _interopRequireDefault(require("./combineExports")); | ||
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); } | ||
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; } | ||
var _helper = require("./helper"); | ||
var _generateJSAPIs = _interopRequireDefault(require("./generateJSAPIs")); | ||
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); } | ||
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; } | ||
/** | ||
@@ -26,99 +28,31 @@ * https://astexplorer.net/ | ||
var libraryName = 'hylid-bridge'; | ||
function snakeCaseTarget(value) { | ||
if (value === 'H5') { | ||
return 'h5'; | ||
} | ||
return _lodash.default.snakeCase(value); | ||
} | ||
function isTargetSupported(target, libraryDir) { | ||
return ['MpWeb', 'Mp', 'Web'].some(function (prefix) { | ||
if (target.startsWith(prefix)) { | ||
var targetClientDir = _path.default.resolve(libraryDir, 'lib/clients', snakeCaseTarget(prefix), snakeCaseTarget(target.replace(prefix, ''))); | ||
return _fsExtra.default.existsSync(targetClientDir); | ||
} | ||
return false; | ||
}); | ||
} | ||
function getTargets(libraryDir, customTargets) { | ||
var targets = customTargets || (process.env.HYLID_BRIDGE_TARGETS ? process.env.HYLID_BRIDGE_TARGETS.split(',') : []); | ||
return targets.filter(function (target) { | ||
return isTargetSupported(target, libraryDir); | ||
}); | ||
} | ||
function isApiExists(name, target, libraryDir, polyfillDir) { | ||
var targetSplit = splitTarget(target); | ||
if (targetSplit.length !== 2) { | ||
throw new Error("Wrong target configuration: ".concat(target, ".")); | ||
} | ||
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")))); | ||
} | ||
function getUserAppEnvPath(appEnvPath) { | ||
var realAppEnvPath = appEnvPath !== null && appEnvPath !== void 0 ? appEnvPath : _path.default.resolve(process.cwd(), './src/appEnv.ts'); | ||
if (!_fsExtra.default.existsSync(realAppEnvPath)) { | ||
utils.log("Can not find the appEnv module at the path: ".concat(realAppEnvPath, ".")); | ||
return; | ||
} | ||
return realAppEnvPath; | ||
} | ||
function splitTarget(target) { | ||
var prefixList = ['MpWeb', 'Mp', 'Web']; | ||
for (var i = 0, il = prefixList.length; i < il; i++) { | ||
var prefix = prefixList[i]; | ||
if (target.startsWith(prefix)) { | ||
return [snakeCaseTarget(prefix), snakeCaseTarget(target.replace(prefix, ''))]; | ||
} | ||
} | ||
throw new Error("Invalid bridge target: ".concat(target, ".")); | ||
} | ||
var myLocalName = "".concat(identifierPrefix, "my__"); | ||
function getNames(node, scope, state, targets) { | ||
var _state$filename; | ||
// my.alert || my[a] | ||
if (node.object.type === 'Identifier' && node.property.type === 'Identifier' && | ||
// Remix 中会编译所有 node_modules 下的模块 | ||
!((_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')) { | ||
return node.computed ? { | ||
objectName: node.object.name, | ||
propertyName: undefined | ||
} : { | ||
objectName: node.object.name, | ||
propertyName: node.property.name | ||
}; | ||
} | ||
// my['alert'] | ||
if (node.object.type === 'Identifier' && node.property.type === 'StringLiteral' && node.computed && targets && targets.length && !scope.hasBinding('my')) { | ||
return { | ||
objectName: node.object.name, | ||
propertyName: node.property.value | ||
}; | ||
} | ||
return { | ||
objectName: undefined, | ||
propertyName: undefined | ||
}; | ||
} | ||
function isJSAPIInTargets(jsapiName, targets, filename, libraryDir, polyfillDir) { | ||
return targets.every(function (target) { | ||
var targetSplit = splitTarget(target); | ||
if (!isApiExists(jsapiName, target, libraryDir, polyfillDir)) { | ||
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, "`.")); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
} | ||
var _default = function _default(core, opts) { | ||
var _opts$libraryDir; | ||
var _default = exports.default = function _default(core, opts) { | ||
var _opts$libraryDir, _opts$transformMy; | ||
var libraryDir = (_opts$libraryDir = opts === null || opts === void 0 ? void 0 : opts.libraryDir) !== null && _opts$libraryDir !== void 0 ? _opts$libraryDir : (0, _getLibraryDir.default)(); | ||
var targets = getTargets(libraryDir, opts === null || opts === void 0 ? void 0 : opts.targets); | ||
var userAppEnvPath = getUserAppEnvPath(opts === null || opts === void 0 ? void 0 : opts.appEnvPath); | ||
var targets = (0, _helper.getTargets)(libraryDir, opts === null || opts === void 0 ? void 0 : opts.targets); | ||
var userAppEnvPath = (0, _helper.getUserAppEnvPath)(opts === null || opts === void 0 ? void 0 : opts.appEnvPath); | ||
var polyfillDir = opts === null || opts === void 0 ? void 0 : opts.polyfillDir; | ||
var transformMy = (_opts$transformMy = opts === null || opts === void 0 ? void 0 : opts.transformMy) !== null && _opts$transformMy !== void 0 ? _opts$transformMy : false; | ||
// 生成 babel-compact 文件 | ||
(0, _generateJSAPIs.default)(targets); | ||
return { | ||
name: 'babel-plugin-hylid-bridge', | ||
visitor: { | ||
ImportDeclaration: function ImportDeclaration(path) { | ||
if (path.node.source.value === libraryName) { | ||
path.node.source = core.types.stringLiteral("".concat(libraryName, "/babel-compact")); | ||
} | ||
}, | ||
ExportNamedDeclaration: function ExportNamedDeclaration(path) { | ||
var _path$node$source; | ||
if (((_path$node$source = path.node.source) === null || _path$node$source === void 0 ? void 0 : _path$node$source.value) === libraryName) { | ||
path.node.source = core.types.stringLiteral("".concat(libraryName, "/babel-compact")); | ||
} | ||
}, | ||
MemberExpression: function MemberExpression(path, state) { | ||
var _getNames = getNames(path.node, path.scope, state, targets), | ||
var _getNames = (0, _helper.getNames)(path.node, path.scope, state, targets), | ||
objectName = _getNames.objectName, | ||
propertyName = _getNames.propertyName; | ||
if (propertyName === 'call' || objectName !== 'my') { | ||
if (propertyName === 'call' || objectName !== 'my' || !transformMy) { | ||
return; | ||
@@ -141,3 +75,3 @@ } | ||
var name = propertyName; | ||
if (isJSAPIInTargets(name, targets, state.filename, libraryDir, polyfillDir)) { | ||
if ((0, _helper.isJSAPIInTargets)(name, targets, state.filename, libraryDir, polyfillDir)) { | ||
state.usedJSAPIList.push(name); | ||
@@ -152,6 +86,6 @@ } else { | ||
CallExpression: function CallExpression(path, state) { | ||
if (path.node.callee.type !== 'MemberExpression') { | ||
if (path.node.callee.type !== 'MemberExpression' || !transformMy) { | ||
return; | ||
} | ||
var _getNames2 = getNames(path.node.callee, path.scope, state, targets), | ||
var _getNames2 = (0, _helper.getNames)(path.node.callee, path.scope, state, targets), | ||
objectName = _getNames2.objectName, | ||
@@ -162,3 +96,5 @@ propertyName = _getNames2.propertyName; | ||
} | ||
var newNode = core.types.callExpression(babelTypes.memberExpression(babelTypes.identifier(myLocalName), babelTypes.identifier(path.node.arguments[0].value)), path.node.arguments.slice(1)); | ||
var newNode = core.types.callExpression( | ||
// @ts-ignore | ||
babelTypes.memberExpression(babelTypes.identifier(myLocalName), babelTypes.identifier(path.node.arguments[0].value)), path.node.arguments.slice(1)); | ||
if (state.usedJSAPIList === 'all') { | ||
@@ -170,3 +106,3 @@ path.replaceWith(newNode); | ||
var name = path.node.arguments[0].value; | ||
if (isJSAPIInTargets(name, targets, state.filename, libraryDir, polyfillDir)) { | ||
if ((0, _helper.isJSAPIInTargets)(name, targets, state.filename, libraryDir, polyfillDir)) { | ||
state.usedJSAPIList.push(name); | ||
@@ -201,3 +137,3 @@ } else { | ||
nodes.push.apply(nodes, (0, _toConsumableArray2.default)(targets.map(function (target) { | ||
var targetSplit = splitTarget(target); | ||
var targetSplit = (0, _helper.splitTarget)(target); | ||
var innerIndexPath = "".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/index"); | ||
@@ -213,4 +149,4 @@ var polyfillIndexPath = polyfillDir && _path.default.resolve(polyfillDir, targetSplit[0], targetSplit[1], 'index'); | ||
var target = targetList[0]; | ||
var targetSplit = splitTarget(target); | ||
return babelTypes.ifStatement(babelTypes.identifier("".concat(appEnvLocalName, ".").concat(_lodash.default.camelCase("is_".concat(targetSplit[0], "_").concat(targetSplit[1])))), babelTypes.blockStatement([babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__"))))]), buildElseIf(targetList.slice(1))); | ||
var targetSplit = (0, _helper.splitTarget)(target); | ||
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))); | ||
}; | ||
@@ -220,3 +156,3 @@ nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(myLocalName))])); | ||
if (targets.length === 1) { | ||
var targetSplit = splitTarget(targets[0]); | ||
var targetSplit = (0, _helper.splitTarget)(targets[0]); | ||
nodes.push(babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_my__"))))); | ||
@@ -231,3 +167,3 @@ } else { | ||
targets.forEach(function (target) { | ||
var targetSplit = splitTarget(target); | ||
var targetSplit = (0, _helper.splitTarget)(target); | ||
var localName = "".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__"); | ||
@@ -245,7 +181,7 @@ var innerSourcePath = "".concat(libraryName, "/lib/clients/").concat(targetSplit[0], "/").concat(targetSplit[1], "/").concat(name); | ||
var target = targetList[0]; | ||
var targetSplit = splitTarget(target); | ||
var targetSplit = (0, _helper.splitTarget)(target); | ||
var propertyList = normalizedJSAPIList.map(function (name) { | ||
return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(targetSplit[0], "_").concat(targetSplit[1], "_").concat(name, "__"))); | ||
}); | ||
return babelTypes.ifStatement(babelTypes.identifier("".concat(appEnvLocalName, ".").concat(_lodash.default.camelCase("is_".concat(targetSplit[0], "_").concat(targetSplit[1])))), babelTypes.blockStatement([babelTypes.expressionStatement(babelTypes.assignmentExpression('=', babelTypes.identifier(myLocalName), babelTypes.objectExpression(propertyList)))]), _buildElseIf(targetList.slice(1))); | ||
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))); | ||
}; | ||
@@ -255,3 +191,3 @@ nodes.push(babelTypes.variableDeclaration('var', [babelTypes.variableDeclarator(babelTypes.identifier(myLocalName))])); | ||
if (targets.length === 1) { | ||
var _targetSplit = splitTarget(targets[0]); | ||
var _targetSplit = (0, _helper.splitTarget)(targets[0]); | ||
var propertyList = normalizedJSAPIList.map(function (name) { | ||
@@ -276,3 +212,2 @@ return babelTypes.objectProperty(babelTypes.identifier(name), babelTypes.identifier("".concat(identifierPrefix).concat(_targetSplit[0], "_").concat(_targetSplit[1], "_").concat(name, "__"))); | ||
}; | ||
}; | ||
exports.default = _default; | ||
}; |
{ | ||
"name": "babel-plugin-hylid-bridge", | ||
"version": "2.12.0-alpha.7", | ||
"version": "2.12.0-alpha.9", | ||
"description": "babel-plugin-hylid-bridge", | ||
@@ -23,4 +23,5 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@types/babel-plugin-tester": "^9.0.7", | ||
"babel-plugin-tester": "^10.1.0", | ||
"hylid-bridge": "^2.12.0-alpha.7" | ||
"hylid-bridge": "^2.12.0-alpha.9" | ||
}, | ||
@@ -27,0 +28,0 @@ "sideEffects": false, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27730
13
482
3
10