Skip to content

Commit

Permalink
lib: lazy load necessary loaders
Browse files Browse the repository at this point in the history
PR-URL: #20567
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
  • Loading branch information
BridgeAR authored and MylesBorins committed May 22, 2018
1 parent 486ac23 commit 3aab6ce
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

const { NativeModule } = require('internal/bootstrap/loaders');
const util = require('util');
const { decorateErrorStack } = require('internal/util');
const { getURLFromFilePath } = require('internal/url');
const vm = require('vm');
const assert = require('assert').ok;
const fs = require('fs');
Expand Down Expand Up @@ -53,11 +51,21 @@ const {

module.exports = Module;

// these are below module.exports for the circular reference
const asyncESM = require('internal/process/esm_loader');
const ModuleJob = require('internal/modules/esm/module_job');
const createDynamicModule = require(
'internal/modules/esm/create_dynamic_module');
let asyncESM;
let ModuleJob;
let createDynamicModule;
let getURLFromFilePath;
let decorateErrorStack;

function lazyLoadESM() {
asyncESM = require('internal/process/esm_loader');
ModuleJob = require('internal/modules/esm/module_job');
createDynamicModule = require(
'internal/modules/esm/create_dynamic_module');
decorateErrorStack = require('internal/util').decorateErrorStack;
getURLFromFilePath = require('internal/url').getURLFromFilePath;
}

const {
CHAR_UPPERCASE_A,
CHAR_LOWERCASE_A,
Expand Down Expand Up @@ -497,6 +505,7 @@ Module._load = function(request, parent, isMain) {
}

if (experimentalModules && isMain) {
if (asyncESM === undefined) lazyLoadESM();
asyncESM.loaderPromise.then((loader) => {
return loader.import(getURLFromFilePath(request).pathname);
})
Expand Down Expand Up @@ -604,6 +613,7 @@ Module.prototype.load = function(filename) {
this.loaded = true;

if (experimentalModules) {
if (asyncESM === undefined) lazyLoadESM();
const ESMLoader = asyncESM.ESMLoader;
const url = getURLFromFilePath(filename);
const urlString = `${url}`;
Expand Down Expand Up @@ -722,6 +732,7 @@ Module._extensions['.node'] = function(module, filename) {
};

if (experimentalModules) {
if (asyncESM === undefined) lazyLoadESM();
Module._extensions['.mjs'] = function(module, filename) {
throw new ERR_REQUIRE_ESM(filename);
};
Expand Down Expand Up @@ -797,5 +808,5 @@ Module._preloadModules = function(requests) {

Module._initPaths();

// backwards compatibility
// Backwards compatibility
Module.Module = Module;

0 comments on commit 3aab6ce

Please sign in to comment.