st-bundle
Version:
CLI for watching and bundling SpringType projects.
132 lines (131 loc) • 5.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const resolver_1 = require("../resolver/resolver");
const fastTransform_1 = require("../transform/fastTransform/fastTransform");
const ast_1 = require("../utils/ast");
const TRACED_VARIABLES = [
'__dirname',
'__filename',
'stream',
'process',
'buffer',
'Buffer',
'http',
'https',
'Worker',
'SharedWorker',
];
const MODULE_VARS = {
buffer: 'buffer',
};
const MODULE_OBJECTS = {
buffer: 'Buffer',
};
function fastAstAnalysis(props) {
const ctx = {
imports: [],
report: {},
};
const ast = fastTransform_1.parseAst(props.input, { loc: props.locations });
ctx.ast = ast;
const bannedVariables = [];
ast_1.fastWalk(ast, {
visit: (node, props) => {
let { parent } = props;
if (node.type === 'ImportDeclaration' ||
node.type === 'ExportNamedDeclaration' ||
node.type === 'ExportAllDeclaration') {
ctx.report.es6Syntax = true;
if (node.source) {
ctx.imports.push({ type: resolver_1.ImportType.RAW_IMPORT, statement: node.source.value });
}
}
else if (node.type === 'JSXElement') {
ctx.report.containsJSX = true;
}
else if (node.type === 'ImportExpression') {
if (node.source && node.source.type === 'Literal') {
ctx.report.es6Syntax = true;
ctx.report.dynamicImports = true;
ctx.imports.push({ type: resolver_1.ImportType.DYNAMIC, statement: node.source.value });
}
else {
throw new Error('Dynamic imports are only supported with static Literals. Please avoid variables to it');
}
}
else if (node.type === 'ExportDefaultDeclaration') {
ctx.report.es6Syntax = true;
}
else if (node.type === 'CallExpression' && node.callee) {
if (node.callee.type === 'Identifier' && node.callee.name === 'require') {
let arg1 = node.arguments[0];
if (node.arguments.length === 1 && ast_1.nodeIsString(arg1)) {
ctx.imports.push({ type: resolver_1.ImportType.REQUIRE, statement: arg1.value });
}
}
}
else if (node.type === 'VariableDeclarator' && node.id && node.id.type === 'Identifier') {
if (TRACED_VARIABLES.indexOf(node.id.name) > -1) {
bannedVariables.push(node.id.name);
}
}
else if (node.type === 'FunctionDeclaration' && node.id && node.id.type === 'Identifier') {
if (TRACED_VARIABLES.indexOf(node.id.name) > -1) {
bannedVariables.push(node.id.name);
// hoisted function
// we need to remove all existing browser essentials with the same name;
if (ctx.report.browserEssentials) {
const foundIndex = ctx.report.browserEssentials.findIndex(i => i.variable === node.id.name);
if (foundIndex > -1) {
ctx.report.browserEssentials.splice(foundIndex, 1);
}
}
}
}
else {
if (node.type === 'Identifier') {
const name = node.name;
if (TRACED_VARIABLES.indexOf(node.name) > -1 && bannedVariables.indexOf(node.name) === -1) {
if (parent && parent.property === node) {
return;
}
if (name === 'Worker' || name === 'SharedWorker') {
if (parent.type === 'NewExpression' && parent.callee === node) {
if (parent.arguments[0] && parent.arguments[0].type === 'Literal') {
if (!ctx.workers)
ctx.workers = [];
ctx.workers.push({ path: parent.arguments[0].value, type: name });
}
// else {
// throw new Error(
// 'Workers are only supported with static Literals. Please avoid the use variable, Since a Webworker will start a new process with its entry point',
// );
// }
}
}
else if (name === '__dirname') {
ctx.report.contains__dirname = true;
}
else if (name === '__filename') {
ctx.report.contains__filename = true;
}
else {
if (!ctx.report.browserEssentials)
ctx.report.browserEssentials = [];
if (!ctx.report.browserEssentials.find(i => i.variable === name)) {
const lowcase = name.toLowerCase();
ctx.report.browserEssentials.push({
obj: MODULE_OBJECTS[lowcase],
moduleName: MODULE_VARS[lowcase] ? MODULE_VARS[lowcase] : name,
variable: name,
});
}
}
}
}
}
},
});
return ctx;
}
exports.fastAstAnalysis = fastAstAnalysis;