diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml
index 63e6d4b..49fe9ef 100644
--- a/.github/workflows/pipeline.yml
+++ b/.github/workflows/pipeline.yml
@@ -43,3 +43,4 @@ jobs:
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
+
diff --git a/README.md b/README.md
index 47be766..f277189 100644
--- a/README.md
+++ b/README.md
@@ -193,7 +193,7 @@ process.cwd()
}
-
{} |
+ undefined |
diff --git a/package-lock.json b/package-lock.json
index 8bef315..4d4c433 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "import-sync",
- "version": "2.1.1",
+ "version": "2.1.2",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "import-sync",
- "version": "2.1.1",
+ "version": "2.1.2",
"license": "MIT",
"dependencies": {
"esm": "^3.2.25"
diff --git a/package.json b/package.json
index 4d5cd68..6b68d41 100644
--- a/package.json
+++ b/package.json
@@ -4,7 +4,7 @@
"type": "git",
"url": "https://github.com/nktnet1/import-sync"
},
- "version": "2.1.1",
+ "version": "2.1.2",
"files": [
"dist"
],
diff --git a/src/import.ts b/src/import.ts
index f50bfda..eb5c824 100644
--- a/src/import.ts
+++ b/src/import.ts
@@ -9,7 +9,7 @@ import { findModuleFile, getCallerDirname } from './files';
* @returns an ESM-compatible require function
*/
/* istanbul ignore next */
-const createEs6Require = (esmOptions: ESMOptions) => {
+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
@@ -34,26 +34,16 @@ const createEs6Require = (esmOptions: ESMOptions) => {
};
/**
- * Imports ES6 modules synchronously similar to require in CommonJS
- * Can be used in both ES6 and CommonJS projects
+ * Returns an ESM-imported module
*
- * @param relativePath - the name or relative path of the module, e.g. ./arrays
- * @param {Options} [options] - options as defined in types.ts
- * @returns the imported module
+ * @param modulePath absolute path or id of the module to import
+ * @param options same options as importSync
+ * @returns the esm imported module
*/
-const importSync = (id: string, options: Options = {}) => {
- const defaultOptions: Required = {
- basePath: getCallerDirname(),
- esmOptions: {},
- };
- const opts = { ...defaultOptions, ...options };
- const modulePath = /^\.\.?\//.test(id) ? findModuleFile(id, opts.basePath) : id;
-
- const es6Require = createEs6Require(opts.esmOptions);
-
- let importedModule: any;
+const esmImport = (modulePath: string, options: Options) => {
+ const esmRequire = createEsmRequire(options.esmOptions);
try {
- importedModule = es6Require(modulePath);
+ return esmRequire(modulePath);
} catch (error: any) {
throw new Error(`
Failed to import from:
@@ -64,14 +54,34 @@ const importSync = (id: string, options: Options = {}) => {
${error.stack}
`);
}
+};
+/**
+ * Imports ES6 modules synchronously similar to require in CommonJS
+ * Can be used in both ES6 and CommonJS projects
+ *
+ * @param relativePath - the name or relative path of the module, e.g. ./arrays
+ * @param {Options} [options] - options as defined in types.ts
+ * @returns the imported module
+ */
+const importSync = (id: string, options: Options = {}) => {
+ const basePath = options.basePath ?? getCallerDirname();
+ const modulePath = /^\.\.?\//.test(id) ? findModuleFile(id, basePath) : id;
+ const importedModule = esmImport(modulePath, options);
+
+ if (Object.keys(importedModule).length > 0) {
+ return importedModule;
+ }
// In case CJS shows up as empty, e.g. when importing CommonJS/CommonTS into Jest
- if (Object.keys(importedModule).length === 0) {
- try {
- const basicModule = require(modulePath);
- importedModule = Object.keys(basicModule).length > 0 ? basicModule : importedModule;
- } catch (error) { /* nothing to do */ }
+ try {
+ const basicModule = require(modulePath);
+ if (Object.keys(basicModule).length > 0) {
+ return basicModule;
+ }
+ } catch (error) {
+ /* nothing to do */
}
+
return importedModule;
};