Skip to content

Commit

Permalink
2.1.2 - Refactored code to comply with code-climate requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
nktnet committed Oct 21, 2023
1 parent 4a8f017 commit b656617
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ jobs:
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}


2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ process.cwd()
}
</pre>
</td>
<td><code>{}</code></td>
<td><code>undefined</code></td>
</tr>

</table>
Expand Down
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.1.1",
"version": "2.1.2",
"files": [
"dist"
],
Expand Down
56 changes: 33 additions & 23 deletions src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Options> = {
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:
Expand All @@ -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;
};

Expand Down

0 comments on commit b656617

Please sign in to comment.