You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hasExportWithName = hasExportWithName;
exports.shouldPluginEnable = shouldPluginEnable;
function _react() {
const data = _interopRequireDefault(require("react"));
_react = function _react() {
return data;
};
return data;
}
function _fs() {
const data = require("fs");
_fs = function _fs() {
return data;
};
return data;
}
function _umi() {
const data = require("umi");
_umi = function _umi() {
return data;
};
return data;
}
function _path() {
const data = require("path");
_path = function _path() {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const t = _umi().utils.t,
parser = _umi().utils.parser;
function hasExportWithName(opts) {
const isTS = (0, _path().extname)(opts.fileName) === '.ts';
const isTSX = (0, _path().extname)(opts.fileName) === '.tsx'; // @ts-ignore
const ast = parser.parse(opts.content, {
sourceType: 'module',
plugins: [// .ts 不能加 jsx因为里面可能有 `<Type>{}` 这种写法
// .tsx, .js, .jsx 可以加
isTS ? false : 'jsx', // 非 ts 不解析 typescript
isTS || isTSX ? 'typescript' : false, // 支持更多语法
'classProperties', 'dynamicImport', 'exportDefaultFrom', 'exportNamespaceFrom', 'functionBind', 'nullishCoalescingOperator', 'objectRestSpread', 'optionalChaining', 'decorators-legacy'].filter(Boolean)
});
let hasExport = false;
ast.program.body.forEach(node => {
if (t.isExportNamedDeclaration(node)) {
if (node.declaration) {
// export function xxx(){};
if (t.isFunctionDeclaration(node.declaration)) {
const id = node.declaration.id;
if (t.isIdentifier(id) && id.name === opts.name) {
hasExport = true;
}
} // export const xxx = () => {};
if (t.isVariableDeclaration(node.declaration) && node.declaration.declarations) {
if (node.declaration.declarations.some(declaration => {
return t.isVariableDeclarator(declaration) && t.isIdentifier(declaration.id) && declaration.id.name === opts.name;
})) {
hasExport = true;
}
}
} // export { getInitialState };
if (node.specifiers && node.specifiers.some(specifier => specifier.exported.name === opts.name)) {
hasExport = true;
}
}
});
return hasExport;
}
function shouldPluginEnable(entryFile) {
let hasExport = false;
if (entryFile) {
const fileContent = (0, _fs().readFileSync)(entryFile, 'utf-8');
hasExport = hasExportWithName({
name: 'getInitialState',
fileName: entryFile,
content: fileContent
});
}
return hasExport;
}