Skip to content

Commit

Permalink
Reverted back to fixing _extensions at top level
Browse files Browse the repository at this point in the history
  • Loading branch information
nktnet committed Oct 20, 2023
1 parent 689028f commit 6ac0b48
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 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 @@ -4,7 +4,7 @@
"type": "git",
"url": "https://github.com/nktnet1/import-sync"
},
"version": "2.0.3",
"version": "2.0.4",
"files": [
"dist"
],
Expand Down
16 changes: 8 additions & 8 deletions src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ import { getCallerFilePath } from './files';
*/
/* istanbul ignore next */
const createEs6Require = (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;
/**
Expand All @@ -23,14 +31,6 @@ const createEs6Require = (esmOptions: ESMOptions) => {
const newRequest = request.startsWith('node:') ? request.substring(5) : request;
return oldResolveFilename.call(this, newRequest, parent, isMain);
};
/**
* Removes the error for "[ERR_REQUIRE_ESM]: require() of ES Module", as per
* - https://github.com/standard-things/esm/issues/855#issuecomment-657982788
*/
newModule._extensions['.js'] = (m: any, filename: string) => {
m._compile(fs.readFileSync(filename, 'utf-8'), filename);
};

return loader;
};

Expand Down

0 comments on commit 6ac0b48

Please sign in to comment.