@areslabs/alita-core
Version:
alita-core
119 lines (92 loc) • 4.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var npath = _interopRequireWildcard(require("path"));
var _ErrorLogTraverse = _interopRequireDefault(require("../util/ErrorLogTraverse"));
var t = _interopRequireWildcard(require("@babel/types"));
var _util = require("../util/util");
var _cacheImageInfos = require("../util/cacheImageInfos");
var _configure = _interopRequireDefault(require("../configure"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }
/**
* Copyright (c) Areslabs.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
/**
* 1. 移除unused的import/require
* 2. 处理RN和小程序的import/require的差异
* @param ast
* @param info
* @returns {*}
*/
function _default(ast, info) {
const {
filepath
} = info;
(0, _ErrorLogTraverse.default)(ast, {
exit: path => {
// import 定义 React
if (path.type === 'ImportDeclaration' && path.node.source.value === 'react' && isImportDecReact(path)) {
const hDec = t.identifier('const h = React.h');
path.insertAfter(hDec);
} // require 定义 React
if (isTopRequire(path, 'react') && isRequireDecReact(path)) {
const hDec = t.identifier('const h = React.h');
path.parentPath.parentPath.insertAfter(hDec);
} // import 静态资源
if (path.type === 'ImportDeclaration' && (0, _util.isStaticRes)(path.node.source.value)) {
const pnode = path.node;
const picName = pnode.specifiers[0].local.name;
const imagePathWithSize = getImagePath(filepath, pnode.source.value);
path.replaceWith(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(picName), t.stringLiteral(imagePathWithSize))]));
return;
} // require 静态资源
// @ts-ignore
if (path.type === 'CallExpression' && path.node.callee.type === 'Identifier' && path.node.callee.name === 'require') {
// @ts-ignore
const source = path.node.arguments[0].value;
if ((0, _util.isStaticRes)(source)) {
const pp = path.parentPath;
const imagePathWithSize = getImagePath(filepath, source);
if (pp.type === 'JSXExpressionContainer') {
// image source
pp.replaceWith(t.stringLiteral(imagePathWithSize));
} else {
path.replaceWith(t.stringLiteral(imagePathWithSize));
}
return;
}
} // 移除动画的注解, 小程序天然支持
// @ts-ignore
if (path.type === 'Decorator' && path.node.expression && path.node.expression.name === 'AnimationEnable') {
path.remove();
return;
}
}
});
return ast;
}
function getImagePath(filepath, source) {
let finals = npath.resolve(npath.dirname(filepath), source);
(0, _cacheImageInfos.addUsedImage)(finals);
finals = finals.replace(_configure.default.inputFullpath, '').replace('node_modules', 'npm').replace(/\\/g, '/');
return finals;
}
function isTopRequire(nodepath, moduleName) {
const node = nodepath.node;
return node.type === 'CallExpression' && node.callee.name === 'require' && node.arguments.length === 1 && node.arguments[0].type === 'StringLiteral' && node.arguments[0].value === moduleName;
}
function isImportDecReact(path) {
return path.node.specifiers.some(spe => spe.local.name === 'React');
}
function isRequireDecReact(path) {
const pp = path.parentPath;
return pp.type === 'VariableDeclarator' && pp.node.id.name === 'React';
}