Skip to content

Commit

Permalink
2.0.3 - exposing ESMOptions, refactor to use newModule._extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
nktnet committed Oct 20, 2023
1 parent 9723b65 commit 689028f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 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.2",
"version": "2.0.3",
"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,15 +11,7 @@ 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 @@ -31,6 +23,14 @@ 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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import importSync from './import';

export default importSync;
export type { Options } from './types';
export type { Options, ESMOptions } from './types';

module.exports = importSync;
module.exports.default = importSync;

0 comments on commit 689028f

Please sign in to comment.