Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
esm: lazy load necessary loaders
  • Loading branch information
BridgeAR committed May 14, 2018
commit 1dfa4ec6b04a834de2431cca8023e0c53adefca2
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) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this one requires everything that loads? Mind you, it might just end up getting loaded anyways.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not specifically test for this case but if esm is used everything should be necessary (at least if more than one file is imported). This is just convenience in this case.

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;