improve loading time by only doing require when needed

This commit is contained in:
Iuri Matias 2018-06-02 09:54:32 -04:00
parent 2c34477e67
commit 72e24f5e1b
2 changed files with 19 additions and 16 deletions

View File

@ -1,19 +1,5 @@
const async = require('async'); const async = require('async');
const Events = require('./events.js');
const Logger = require('./logger.js');
const Config = require('./config.js');
const Blockchain = require('../contracts/blockchain.js');
const Compiler = require('../contracts/compiler.js');
const ContractsManager = require('../contracts/contracts.js');
const ContractDeployer = require('../contracts/contract_deployer.js');
const DeployManager = require('../contracts/deploy_manager.js');
const CodeGenerator = require('../contracts/code_generator.js');
const ServicesMonitor = require('./services_monitor.js');
const Pipeline = require('../pipeline/pipeline.js');
const Watch = require('../pipeline/watch.js');
const LibraryManager = require('../versions/library_manager.js');
const CodeRunner = require('../coderunner/codeRunner.js');
const utils = require('../utils/utils'); const utils = require('../utils/utils');
class Engine { class Engine {
@ -32,6 +18,10 @@ class Engine {
} }
init(_options) { init(_options) {
const Events = require('./events.js');
const Logger = require('./logger.js');
const Config = require('./config.js');
let options = _options || {}; let options = _options || {};
this.events = options.events || this.events || new Events(); this.events = options.events || this.events || new Events();
this.logger = options.logger || new Logger({logLevel: options.logLevel || this.logLevel || 'debug', events: this.events, logFile: this.logFile}); this.logger = options.logger || new Logger({logLevel: options.logLevel || this.logLevel || 'debug', events: this.events, logFile: this.logFile});
@ -105,6 +95,7 @@ class Engine {
pipelineService(_options) { pipelineService(_options) {
const self = this; const self = this;
this.events.emit("status", "Building Assets"); this.events.emit("status", "Building Assets");
const Pipeline = require('../pipeline/pipeline.js');
const pipeline = new Pipeline({ const pipeline = new Pipeline({
buildDir: this.config.buildDir, buildDir: this.config.buildDir,
contractsFiles: this.config.contractsFiles, contractsFiles: this.config.contractsFiles,
@ -127,6 +118,7 @@ class Engine {
serviceMonitor() { serviceMonitor() {
const self = this; const self = this;
const ServicesMonitor = require('./services_monitor.js');
this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger, plugins: this.plugins}); this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger, plugins: this.plugins});
this.servicesMonitor.addCheck('embarkVersion', function (cb) { this.servicesMonitor.addCheck('embarkVersion', function (cb) {
return cb({name: 'Embark ' + self.version, status: 'on'}); return cb({name: 'Embark ' + self.version, status: 'on'});
@ -139,6 +131,7 @@ class Engine {
} }
codeRunnerService(_options) { codeRunnerService(_options) {
const CodeRunner = require('../coderunner/codeRunner.js');
this.codeRunner = new CodeRunner({ this.codeRunner = new CodeRunner({
plugins: this.plugins, plugins: this.plugins,
events: this.events, events: this.events,
@ -149,6 +142,7 @@ class Engine {
codeGeneratorService(_options) { codeGeneratorService(_options) {
let self = this; let self = this;
const CodeGenerator = require('../contracts/code_generator.js');
this.codeGenerator = new CodeGenerator({ this.codeGenerator = new CodeGenerator({
blockchainConfig: self.config.blockchainConfig, blockchainConfig: self.config.blockchainConfig,
contractsConfig: self.config.contractsConfig, contractsConfig: self.config.contractsConfig,
@ -182,6 +176,7 @@ class Engine {
deploymentService(options) { deploymentService(options) {
let self = this; let self = this;
const Compiler = require('../contracts/compiler.js');
let compiler = new Compiler({plugins: self.plugins, logger: self.logger}); let compiler = new Compiler({plugins: self.plugins, logger: self.logger});
this.events.setCommandHandler("compiler:contracts", function(contractFiles, cb) { this.events.setCommandHandler("compiler:contracts", function(contractFiles, cb) {
compiler.compile_contracts(contractFiles, cb); compiler.compile_contracts(contractFiles, cb);
@ -193,6 +188,7 @@ class Engine {
this.registerModule('deploytracker'); this.registerModule('deploytracker');
this.registerModule('specialconfigs'); this.registerModule('specialconfigs');
const ContractsManager = require('../contracts/contracts.js');
this.contractsManager = new ContractsManager({ this.contractsManager = new ContractsManager({
contractFiles: this.config.contractsFiles, contractFiles: this.config.contractsFiles,
contractsConfig: this.config.contractsConfig, contractsConfig: this.config.contractsConfig,
@ -202,6 +198,7 @@ class Engine {
events: this.events events: this.events
}); });
const DeployManager = require('../contracts/deploy_manager.js');
this.deployManager = new DeployManager({ this.deployManager = new DeployManager({
blockchain: this.blockchain, blockchain: this.blockchain,
config: this.config, config: this.config,
@ -211,6 +208,7 @@ class Engine {
onlyCompile: options.onlyCompile onlyCompile: options.onlyCompile
}); });
const ContractDeployer = require('../contracts/contract_deployer.js');
this.contractDeployer = new ContractDeployer({ this.contractDeployer = new ContractDeployer({
blockchain: this.blockchain, blockchain: this.blockchain,
logger: this.logger, logger: this.logger,
@ -240,6 +238,7 @@ class Engine {
fileWatchService(_options) { fileWatchService(_options) {
this.events.emit("status", "Watching for changes"); this.events.emit("status", "Watching for changes");
const Watch = require('../pipeline/watch.js');
this.watch = new Watch({logger: this.logger, events: this.events}); this.watch = new Watch({logger: this.logger, events: this.events});
this.watch.start(); this.watch.start();
} }
@ -264,6 +263,7 @@ class Engine {
} }
web3Service(options) { web3Service(options) {
const Blockchain = require('../contracts/blockchain.js');
this.blockchain = new Blockchain({ this.blockchain = new Blockchain({
contractsConfig: this.config.contractsConfig, contractsConfig: this.config.contractsConfig,
blockchainConfig: this.config.blockchainConfig, blockchainConfig: this.config.blockchainConfig,
@ -282,6 +282,7 @@ class Engine {
} }
libraryManagerService(_options) { libraryManagerService(_options) {
const LibraryManager = require('../versions/library_manager.js');
this.libraryManager = new LibraryManager({ this.libraryManager = new LibraryManager({
plugins: this.plugins, plugins: this.plugins,
config: this.config config: this.config

View File

@ -12,8 +12,6 @@ process.chdir = (...args) => {
realChdir(...args); realChdir(...args);
}; };
let Engine = require('./core/engine.js');
let version = require('../package.json').version; let version = require('../package.json').version;
class Embark { class Embark {
@ -69,6 +67,7 @@ class Embark {
self.context = options.context || [constants.contexts.run, constants.contexts.build]; self.context = options.context || [constants.contexts.run, constants.contexts.build];
let Dashboard = require('./dashboard/dashboard.js'); let Dashboard = require('./dashboard/dashboard.js');
let Engine = require('./core/engine.js');
let engine = new Engine({ let engine = new Engine({
env: options.env, env: options.env,
client: options.client, client: options.client,
@ -159,6 +158,7 @@ class Embark {
build(options) { build(options) {
this.context = options.context || [constants.contexts.build]; this.context = options.context || [constants.contexts.build];
let Engine = require('./core/engine.js');
let engine = new Engine({ let engine = new Engine({
env: options.env, env: options.env,
client: options.client, client: options.client,
@ -214,6 +214,7 @@ class Embark {
this.context = options.context || [constants.contexts.graph]; this.context = options.context || [constants.contexts.graph];
options.onlyCompile = true; options.onlyCompile = true;
let Engine = require('./core/engine.js');
let engine = new Engine({ let engine = new Engine({
env: options.env, env: options.env,
isDev: this.isDev(options.env), isDev: this.isDev(options.env),
@ -269,6 +270,7 @@ class Embark {
upload(options) { upload(options) {
this.context = options.context || [constants.contexts.upload, constants.contexts.build]; this.context = options.context || [constants.contexts.upload, constants.contexts.build];
let Engine = require('./core/engine.js');
let engine = new Engine({ let engine = new Engine({
env: options.env, env: options.env,
client: options.client, client: options.client,