UNPKG

st-bundle

Version:

CLI for watching and bundling SpringType projects.

115 lines (112 loc) 4.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const resolver_1 = require("../resolver/resolver"); const fastAstAnalysis_1 = require("./fastAstAnalysis"); const tokenizer_1 = require("./tokenizer"); /** * AN extremely simple and reliable module extractor based on one very well * optmised Regular expression * * A benchmark against acorn shows some dramatic difference which should impact the build time by * the amount of gratitude * * acorn.parse: 17.796312283873558ms (100 runs) fastAnalysis: 2.9928058910369875ms (100 runs) This benchmark includes only parsing for acorn (without traversing the tree and extracting the statements propertly) Which could potentially add a few more ms to the analysis using acorn fastAnalysis is at least 9x faster. IF we imagine 100 files that need to be analysed, it will take 300ms with fastAnalysis, and with acorn that would be 5.7 seconds, and here traversing (not to mention the CPI load).And you will get some 7 seconds of just pointless manipulation. * @export * @param {IFastAnalysisProps} props * @returns {Partial<IFastAnalysis>} */ const MODULE_VARS = { buffer: 'buffer', }; const MODULE_OBJECTS = { buffer: 'Buffer', }; function fastAnalysis(props) { if (props.parseUsingAst) { return fastAstAnalysis_1.fastAstAnalysis({ packageName: props.packageName, input: props.input, locations: props.locations }); } const result = { imports: [], report: {}, }; let skip = false; const bannedSystemVariables = []; tokenizer_1.tokenize(props.input, token => { if (token.commentStart) { skip = true; } else if (token.commentEnd) { skip = false; } else { if (skip) { return; } } if (token.exportsKeyword) { result.report.es6Syntax = true; return; } if (token.systemVariable) { const name = token.systemVariable.name; if (token.systemVariable.declaration) { bannedSystemVariables.push(name); return; } if (bannedSystemVariables.includes(name)) { return; } if (name === '__dirname') { result.report.contains__dirname = true; return; } if (name === '__filename') { result.report.contains__filename = true; return; } if (!result.report.browserEssentials) { result.report.browserEssentials = []; } if (!result.report.browserEssentials.find(i => i.variable === name)) { const lowcase = name.toLowerCase(); result.report.browserEssentials.push({ obj: MODULE_OBJECTS[lowcase], moduleName: MODULE_VARS[lowcase] ? MODULE_VARS[lowcase] : name, variable: name, }); } } if (token.requireStatement) { result.imports.push({ type: resolver_1.ImportType.REQUIRE, statement: token.requireStatement }); } if (token.workerImport) { if (!result.workers) result.workers = []; result.workers.push({ path: token.workerImport, type: 'Worker' }); } if (token.sharedWorkerImport) { if (!result.workers) result.workers = []; result.workers.push({ path: token.sharedWorkerImport, type: 'SharedWorker' }); } if (token.importFrom) { result.report.es6Syntax = true; result.imports.push({ type: resolver_1.ImportType.FROM, statement: token.importFrom }); } if (token.importModule) { result.report.es6Syntax = true; result.imports.push({ type: resolver_1.ImportType.RAW_IMPORT, statement: token.importModule }); } if (token.dynamicImport) { result.report.es6Syntax = true; result.report.dynamicImports = true; result.imports.push({ type: resolver_1.ImportType.DYNAMIC, statement: token.dynamicImport }); } }, props.debug); return result; } exports.fastAnalysis = fastAnalysis;