Skip to content
This repository was archived by the owner on Apr 16, 2020. It is now read-only.
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
Next Next commit
module: add import.meta.require
This adds require to the import.meta object

This can be used as a mechanism to get interoperability between
common.js and esm.
  • Loading branch information
targos authored and MylesBorins committed Aug 22, 2018
commit 6c9f5ebedec05c8391b3f14c2502b1c05c3cb58f
27 changes: 26 additions & 1 deletion lib/internal/process/esm_loader.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
'use strict';

const { makeRequireFunction } = require('internal/modules/cjs/helpers');
const Module = require('module');
const { internalBinding } = require('internal/bootstrap/loaders');

const {
setImportModuleDynamicallyCallback,
setInitializeImportMetaObjectCallback
} = internalBinding('module_wrap');
const { defineProperty } = Object;

const { getURLFromFilePath } = require('internal/url');
const { getURLFromFilePath, getPathFromURL } = require('internal/url');
const Loader = require('internal/modules/esm/loader');
const path = require('path');
const { URL } = require('url');
Expand All @@ -27,6 +31,27 @@ function initializeImportMetaObject(wrap, meta) {
if (vmModule === undefined) {
// This ModuleWrap belongs to the Loader.
meta.url = wrap.url;
let req;
defineProperty(meta, 'require', {
enumerable: true,
configurable: true,
get() {
if (req !== undefined)
return req;
const url = new URL(meta.url);
const path = getPathFromURL(url);
const mod = new Module(path, null);
mod.filename = path;
req = makeRequireFunction(mod).bind(null);

// remove properties that don't affect ESM loading
// delete req.cache;
// delete req.extensions;
// delete req.main;
// delete req.resolve.paths;
return req;
}
});
} else {
const initializeImportMeta = initImportMetaMap.get(vmModule);
if (initializeImportMeta !== undefined) {
Expand Down