Skip to content

Commit

Permalink
Remove fixes now included directly within @httptoolkit/esm
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Dec 15, 2023
1 parent 240c8bb commit 1ee105d
Showing 1 changed file with 1 addition and 22 deletions.
23 changes: 1 addition & 22 deletions src/import.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import esm from '@httptoolkit/esm';
import fs from 'fs';

import { ESMOptions, Options } from './types';
import { findModuleFile, getCallerDirname } from './files';
Expand All @@ -11,27 +10,7 @@ import { findModuleFile, getCallerDirname } from './files';
*/
/* istanbul ignore next */
const createEsmRequire = (esmOptions: ESMOptions) => {
/**
* Removes the error for "[ERR_REQUIRE_ESM]: require() of ES Module", as per
* - https://github.com/standard-things/esm/issues/855#issuecomment-657982788
*/
require('module').Module._extensions['.js'] = (m: any, filename: string) => {
m._compile(fs.readFileSync(filename, 'utf-8'), filename);
};
const loader = esm(module, esmOptions);

const newModule = module.constructor.length > 1 ? module.constructor : loader('module');
const oldResolveFilename = newModule._resolveFilename;
/**
* Referencing
* - https://github.com/kenotron/esm-jest/commit/624b9524ee698f5cbd16ee2481dc4cd0dec52e42
* - https://github.com/standard-things/esm/issues/331#issuecomment-377056717
*/
newModule._resolveFilename = function(request: string, parent: any, isMain: boolean) {
const newRequest = request.startsWith('node:') ? request.substring(5) : request;
return oldResolveFilename.call(this, newRequest, parent, isMain);
};
return loader;
return esm(module, esmOptions);
};

/**
Expand Down

0 comments on commit 1ee105d

Please sign in to comment.