Skip to content

Commit

Permalink
Implementation and documentation updated - now allows for pure-esm to…
Browse files Browse the repository at this point in the history
… be imported into cjs
  • Loading branch information
nktnet committed Oct 20, 2023
1 parent 3852227 commit ee6e0ac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Synchronously import dynamic ECMAScript Modules similar to CommonJS [require](ht

Basic wrapper around [esm](https://github.com/standard-things/esm) for compatibility with both ES6 and CJS projects in NodeJS

Capable of importing pure-esm libraries such as node-fetch@3 in CJS projects

[![Try with Replit](https://replit.com/badge?caption=Try%20with%20Replit)](https://replit.com/@nktnet1/import-sync-example#index.js)

</div>
Expand Down
17 changes: 0 additions & 17 deletions src/files.ts

This file was deleted.

35 changes: 25 additions & 10 deletions src/import.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import esm from 'esm';
import path from 'path';
import { getCallerFilePath } from './files';
import fs from 'fs';
import { ESMOptions, Options } from './types';

/**
* Creates an esm-compatible require function that can import ES Modules
*
* Removes the error for "[ERR_REQUIRE_ESM]: require() of ES Module", as per
* - https://github.com/standard-things/esm/issues/855#issuecomment-657982788
*
* @returns an ESM-compatible require function
*/
/* istanbul ignore next */
const createEs6Require = (esmOptions: ESMOptions) => {
/* istanbul ignore next */
/**
* 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(require('fs').readFileSync(filename, 'utf-8'), filename);
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 require('esm')(module, esmOptions);
return loader;
};

/**
Expand All @@ -26,13 +41,13 @@ const createEs6Require = (esmOptions: ESMOptions) => {
* @param {Options} [options] - options as defined in types.ts
* @returns the imported module
*/
const importSync = (relativePath: string, options: Options = {}) => {
const importSync = (id: string, options: Options = {}) => {
const defaultOptions: Required<Options> = {
basePath: path.dirname(getCallerFilePath()),
basePath: require.main?.path || '',
esmOptions: {},
};
const opts = { ...defaultOptions, ...options };
const filePath = path.join(opts.basePath, relativePath);
const filePath = path.join(opts.basePath, id);
const es6Require = createEs6Require(opts.esmOptions);
try {
return es6Require(filePath);
Expand Down

0 comments on commit ee6e0ac

Please sign in to comment.