Skip to content

Commit

Permalink
Merge pull request #38 from httptoolkit/switch-esm
Browse files Browse the repository at this point in the history
Switch from esm to @httptoolkit/esm
  • Loading branch information
nktnet1 committed Dec 15, 2023
2 parents 3013682 + 1ee105d commit f60e076
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 34 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@
"typescript": "^5.3.3"
},
"dependencies": {
"esm": "^3.2.25"
"@httptoolkit/esm": "^3.3.0"
}
}
4 changes: 4 additions & 0 deletions src/httptoolkit-esm-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '@httptoolkit/esm' {
import * as Esm from 'esm';
export = Esm;
}
26 changes: 3 additions & 23 deletions src/import.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import esm from 'esm';
import fs from 'fs';
import esm from '@httptoolkit/esm';

import { ESMOptions, Options } from './types';
import { findModuleFile, getCallerDirname } from './files';

Expand All @@ -10,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
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import esm from 'esm';
import esm from '@httptoolkit/esm';

export type ESMOptions = Parameters<typeof esm>[1];

Expand Down

0 comments on commit f60e076

Please sign in to comment.