Skip to content

Commit

Permalink
2.0.6 - Added support for automatic .js, .cjs, .mjs file matches in i…
Browse files Browse the repository at this point in the history
…mport
  • Loading branch information
nktnet committed Oct 21, 2023
1 parent dc90d96 commit de52fe0
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ Module name or relative path similar to CommonJS [require](https://nodejs.org/ap
- `'./dogs.mjs'`
- `'./minimal'`
- `importSync` will look for `'./minimal.js'` before `'./minimal.mjs'`
- `importSync` will look for matching extensions in the order `[.js, .mjs, .cjs]`
- `'node-fetch'`
- `importSync` can import pure-esm [node-fetch](https://github.com/node-fetch/node-fetch) (v3) in your cjs project

Expand Down
51 changes: 29 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions 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.5",
"version": "2.0.6",
"files": [
"dist"
],
Expand Down Expand Up @@ -47,8 +47,8 @@
"@types/node": "^20.8.7",
"@typescript-eslint/eslint-plugin": "^6.8.0",
"@typescript-eslint/parser": "^6.8.0",
"eslint": "^8.51.0",
"eslint-plugin-jest": "^27.4.2",
"eslint": "^8.52.0",
"eslint-plugin-jest": "^27.4.3",
"jest": "^29.7.0",
"node-fetch": "^3.3.2",
"ts-jest": "^29.1.1",
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const VALID_FILE_EXTENSIONS = Object.freeze(['.js', '.mjs', '.cjs']);
31 changes: 31 additions & 0 deletions src/files.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import fs from 'fs';
import path from 'path';
import { VALID_FILE_EXTENSIONS } from './config';

/**
* Get the file path of the caller function.
*
Expand All @@ -15,3 +19,30 @@ export const getCallerFilePath = (): string => {
Error.prepareStackTrace = orig;
return stack[1].getFileName();
};

/**
* Find the module file path by checking for available extensions
* as defined by VALID_FILE_EXTENSIONS
*
* @param {string} modulePath - The path to the module
* @param {string} basePath - The base path for the module
* @returns {string} The resolved file path
* @throws {Error} If the file is not found
*/
export const findModuleFile = (modulePath: string, basePath: string): string => {
const filePath = path.join(basePath, modulePath);
if (fs.existsSync(filePath)) {
return filePath;
}
if (!path.extname(filePath)) {
for (const ext of VALID_FILE_EXTENSIONS) {
const extFilePath = path.join(basePath, `${modulePath}${ext}`);
if (fs.existsSync(extFilePath)) {
return extFilePath;
}
}
}
throw new Error(
`No such file '${filePath}' with matching extensions [${VALID_FILE_EXTENSIONS}]`
);
};
7 changes: 2 additions & 5 deletions src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import esm from 'esm';
import path from 'path';
import fs from 'fs';
import { ESMOptions, Options } from './types';
import { getCallerFilePath } from './files';
import { findModuleFile, getCallerFilePath } from './files';

/**
* Creates an esm-compatible require function that can import ES Modules
Expand Down Expand Up @@ -48,10 +48,7 @@ const importSync = (id: string, options: Options = {}) => {
esmOptions: {},
};
const opts = { ...defaultOptions, ...options };
let modulePath = id;
if (/^\.\.?\//.test(id)) {
modulePath = path.join(opts.basePath, id);
}
const modulePath = /^\.\.?\//.test(id) ? findModuleFile(id, opts.basePath) : id;
const es6Require = createEs6Require(opts.esmOptions);
try {
return es6Require(modulePath);
Expand Down

0 comments on commit de52fe0

Please sign in to comment.