UNPKG

st-bundle

Version:

CLI for watching and bundling SpringType projects.

236 lines (235 loc) 7.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ast_1 = require("../utils/ast"); function getVariableDeclarations(node) { if (node.declaration && node.declaration.type === 'VariableDeclaration') { if (node.declaration.declarations) { return node.declaration.declarations; } } } exports.getVariableDeclarations = getVariableDeclarations; function getSpecifiers(node) { if (node.declaration && node.declaration.type === 'VariableDeclaration') { if (node.declaration.declarations) { return node.declaration.declarations; } } } exports.getSpecifiers = getSpecifiers; function isExportDefaultDeclaration(node) { return node.type === 'ExportDefaultDeclaration'; } exports.isExportDefaultDeclaration = isExportDefaultDeclaration; function isExportNamedDeclaration(node) { return node.type === 'ExportNamedDeclaration'; } exports.isExportNamedDeclaration = isExportNamedDeclaration; function isFunctionDeclaration(node) { return node.type === 'FunctionDeclaration'; } exports.isFunctionDeclaration = isFunctionDeclaration; function isClassDeclaration(node) { return node.type === 'ClassDeclaration'; } exports.isClassDeclaration = isClassDeclaration; function isWorkerStatement(node) { if (node.type === 'NewExpression' && node.callee) { if (node.callee.type === 'Identifier' && (node.callee.name === 'Worker' || node.callee.name === 'SharedWorker')) { if (node.arguments && node.arguments.length === 1) { if (node.arguments[0].type === 'Literal') { return { value: node.arguments[0].value, type: node.callee.name }; } } } } } exports.isWorkerStatement = isWorkerStatement; function isRequireStatement(node, parent) { 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)) { return arg1.value; } } } } exports.isRequireStatement = isRequireStatement; function isShortHandIdentifier(node, parent) { if (node.type === 'Property' && node.shorthand === true) { if (node.value && node.value.type === 'Identifier') { return { node: node.value, parent: node }; } } } exports.isShortHandIdentifier = isShortHandIdentifier; function isLocalIdentifier(node, parent) { if (node.type === 'Identifier' && parent) { return ((parent.computed === true || (parent.property !== node && !parent.computed)) && parent.key !== node && parent.type !== 'FunctionDeclaration' && parent.type !== 'FunctionExpression'); } } exports.isLocalIdentifier = isLocalIdentifier; function createTracedExpression(local, property) { return { type: 'MemberExpression', object: { type: 'Identifier', name: local, }, computed: false, property: property, }; } exports.createTracedExpression = createTracedExpression; function createRequireStatement(local, source) { const reqStatement = { type: 'CallExpression', callee: { type: 'Identifier', name: 'require', }, arguments: [ { type: 'Literal', value: source, }, ], }; if (!local) { return { type: 'ExpressionStatement', expression: reqStatement, }; } return { type: 'VariableDeclaration', declarations: [ { type: 'VariableDeclarator', id: { type: 'Identifier', start: 7, end: 8, name: local, }, init: reqStatement, }, ], kind: 'const', }; } exports.createRequireStatement = createRequireStatement; function createLocalVariable(name, property) { return { type: 'VariableDeclaration', declarations: [ { type: 'VariableDeclarator', id: { type: 'Identifier', name: name, }, init: property, }, ], kind: 'const', }; } exports.createLocalVariable = createLocalVariable; function createMemberExpression(obj, target) { return { type: 'MemberExpression', object: { type: 'Identifier', name: obj, }, property: { type: 'Identifier', name: target, }, computed: false, }; } exports.createMemberExpression = createMemberExpression; function createExportsExpression(name) { return { type: 'MemberExpression', object: { type: 'Identifier', name: 'exports', }, computed: false, property: { type: 'Identifier', name: name, }, }; } exports.createExportsExpression = createExportsExpression; function createModuleExports(exportsVariableName, property) { return { type: 'ExpressionStatement', expression: { type: 'AssignmentExpression', operator: '=', left: { type: 'MemberExpression', object: { type: 'Identifier', name: 'exports', }, computed: false, property: { type: 'Identifier', name: exportsVariableName, }, }, right: property, }, }; } exports.createModuleExports = createModuleExports; function createModuleExportsAssign(fromSource) { return { type: 'ExpressionStatement', expression: { type: 'CallExpression', callee: { type: 'MemberExpression', object: { type: 'Identifier', name: 'Object', }, computed: false, property: { type: 'Identifier', name: 'assign', }, }, arguments: [ { type: 'Identifier', name: 'exports', }, { type: 'CallExpression', callee: { type: 'Identifier', name: 'require', }, arguments: [ { type: 'Literal', value: fromSource, }, ], }, ], }, }; } exports.createModuleExportsAssign = createModuleExportsAssign;