feat(compartment-mapper): dancing skeleton require.resolve implementation
MODIFY
packages/compartment-mapper/src/import-hook.js
packages/compartment-mapper/src/import-hook.js
@@ -169,6 +169,12 @@ export const makeImportHookMaker = (
const moduleBytes = await read(moduleLocation).catch(
_error => undefined,
);
+ const requireResolve = specifier => {
+ console.warn('Warning: Getting correct result form require.resolve in Endo is only possible if the package was resolved earlier.')
+ return packageSources[specifier]
+ ? packageSources[specifier].sourceLocation
+ : null;
+ };
if (moduleBytes !== undefined) {
// eslint-disable-next-line no-await-in-loop
const envelope = await parse(
@@ -177,6 +183,7 @@ export const makeImportHookMaker = (
moduleLocation,
packageLocation,
readPowers,
+ requireResolve,
);
const {
parser,

MODIFY
packages/compartment-mapper/src/link.js
packages/compartment-mapper/src/link.js
@@ -77,7 +77,7 @@ const makeExtensionParser = (
parserForLanguage,
transforms,
) => {
- return async (bytes, specifier, location, packageLocation, readPowers) => {
+ return async (bytes, specifier, location, packageLocation, readPowers, requireResolve) => {
let language;
if (has(languageForModuleSpecifier, specifier)) {
language = languageForModuleSpecifier[specifier];
@@ -106,7 +106,7 @@ const makeExtensionParser = (
);
}
const { parse } = parserForLanguage[language];
- return parse(bytes, specifier, location, packageLocation, readPowers);
+ return parse(bytes, specifier, location, packageLocation, readPowers, requireResolve);
};
};

MODIFY
packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js
packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js
@@ -74,7 +74,7 @@ export const getModulePaths = (readPowers, location) => {
* require: Function,
* }}
*/
-export const wrap = (moduleEnvironmentRecord, compartment, resolvedImports) => {
+export const wrap = (moduleEnvironmentRecord, compartment, resolvedImports, requireResolve) => {
// This initial default value makes things like exports.hasOwnProperty() work in cjs.
moduleEnvironmentRecord.default = create(
compartment.globalThis.Object.prototype,
@@ -122,7 +122,7 @@ export const wrap = (moduleEnvironmentRecord, compartment, resolvedImports) => {
},
});
- const require = freeze((/** @type {string} */ importSpecifier) => {
+ const require = (/** @type {string} */ importSpecifier) => {
const namespace = compartment.importNow(resolvedImports[importSpecifier]);
// If you read this file carefully, you'll see it's not possible for a cjs module to not have the default anymore.
// It's currently possible to require modules that were not created by this file though.
@@ -131,7 +131,10 @@ export const wrap = (moduleEnvironmentRecord, compartment, resolvedImports) => {
} else {
return namespace;
}
- });
+ };
+ require.resolve = freeze(requireResolve)
+
+ freeze(require)
const afterExecute = () => {
const exportsHaveBeenOverwritten = finalExports !== originalExports;

MODIFY
packages/compartment-mapper/src/parse-cjs.js
packages/compartment-mapper/src/parse-cjs.js
@@ -14,6 +14,7 @@ export const parseCjs = async (
location,
_packageLocation,
readPowers,
+ requireResolve,
) => {
const source = textDecoder.decode(bytes);
@@ -42,6 +43,7 @@ export const parseCjs = async (
moduleEnvironmentRecord,
compartment,
resolvedImports,
+ requireResolve,
);
functor(require, moduleExports, module, filename, dirname);

feat(compartment-mapper): dancing skeleton require.resolve implementation