Skip to content

Commit 0262d10

Browse files
committed
esm,loader: create parentURL from the main thread
Create the parentURL from the main thread to get the user dirname where the application will be running. With this implementation the user can register loaders using partial imports "./...", "../...".
1 parent 64f3bb8 commit 0262d10

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

lib/internal/modules/esm/hooks.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ const {
4949
} = require('internal/validators');
5050

5151
const { kEmptyObject } = require('internal/util');
52-
const { pathToFileURL } = require('internal/url');
5352

5453
const {
5554
defaultResolve,
@@ -128,18 +127,16 @@ class Hooks {
128127
* Import and register custom/user-defined module loader hook(s).
129128
* @param {string} urlOrSpecifier
130129
*/
131-
async registerLoader(urlOrSpecifier) {
130+
async registerLoader(urlOrSpecifier, parentURL) {
132131
const { DefaultModuleLoader } = require('internal/modules/esm/loader')
133132

134133
const moduleLoader = new DefaultModuleLoader();
135-
const parentURL = pathToFileURL(process.cwd() + '/').href;
136134
const keyedExportsSublist = await moduleLoader.import(
137135
[urlOrSpecifier],
138136
parentURL,
139137
kEmptyObject,
140138
);
141139

142-
143140
this.addCustomLoaders(keyedExportsSublist);
144141
}
145142

lib/internal/modules/esm/register.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const { dirname } = require('path');
4+
const { pathToFileURL } = require('internal/url');
35
const { getHooksProxy } = require('internal/modules/esm/loader');
46

57
/**
@@ -14,8 +16,9 @@ const { getHooksProxy } = require('internal/modules/esm/loader');
1416
*/
1517
async function registerLoader(urlOrSpecifier) {
1618
const hooksProxy = getHooksProxy();
19+
const parentURL = pathToFileURL(dirname(process.argv[1]) + '/').href
1720

18-
await hooksProxy.makeAsyncRequest('registerLoader', urlOrSpecifier)
21+
await hooksProxy.makeAsyncRequest('registerLoader', urlOrSpecifier, parentURL)
1922
}
2023

2124
module.exports = {

0 commit comments

Comments
 (0)