youzanyun-devtool-worker
Version:
44 lines (43 loc) • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const acorn_1 = require("acorn");
function compCompile(source) {
const startReg = /<script[^>]*>/g;
const matches = [];
let match;
while ((match = startReg.exec(source)) !== null) {
matches.push(match);
}
if (matches.length === 0) {
return {};
}
const scripts = matches.map(item => {
const startIndex = item.index + item[0].length;
const newStr = source.substring(startIndex);
const endReg = /<\/script>/;
const endMatch = endReg.exec(newStr);
const endIndex = endMatch.index;
const content = newStr.substring(0, endIndex);
return content;
});
const fileSource = scripts.join(';');
const componentProperties = {};
const astTree = (0, acorn_1.parse)(fileSource, {
sourceType: 'module'
});
const ast = astTree.body;
const exportDefaultAst = ast.filter((astItem) => astItem.type === 'ExportDefaultDeclaration');
exportDefaultAst.map((astItem) => {
if (astItem.declaration.type === 'ObjectExpression') {
const propAst = astItem.declaration.properties;
propAst.map((item) => {
if (item.key.type === 'Identifier' && item.value.type === 'Literal' && (item.key.name === 'name' || item.key.name === 'title')) {
componentProperties[item.key.name] = item.value.value;
}
});
}
});
return componentProperties;
}
exports.default = compCompile;
;