import-sync
Advanced tools
Comparing version 2.0.5 to 2.0.6
@@ -10,1 +10,11 @@ /** | ||
export declare const getCallerFilePath: () => string; | ||
/** | ||
* 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 declare const findModuleFile: (modulePath: string, basePath: string) => string; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getCallerFilePath = void 0; | ||
exports.findModuleFile = exports.getCallerFilePath = void 0; | ||
const fs_1 = __importDefault(require("fs")); | ||
const path_1 = __importDefault(require("path")); | ||
const config_1 = require("./config"); | ||
/** | ||
@@ -22,2 +28,27 @@ * Get the file path of the caller function. | ||
exports.getCallerFilePath = getCallerFilePath; | ||
/** | ||
* 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 | ||
*/ | ||
const findModuleFile = (modulePath, basePath) => { | ||
const filePath = path_1.default.join(basePath, modulePath); | ||
if (fs_1.default.existsSync(filePath)) { | ||
return filePath; | ||
} | ||
if (!path_1.default.extname(filePath)) { | ||
for (const ext of config_1.VALID_FILE_EXTENSIONS) { | ||
const extFilePath = path_1.default.join(basePath, `${modulePath}${ext}`); | ||
if (fs_1.default.existsSync(extFilePath)) { | ||
return extFilePath; | ||
} | ||
} | ||
} | ||
throw new Error(`No such file '${filePath}' with matching extensions [${config_1.VALID_FILE_EXTENSIONS}]`); | ||
}; | ||
exports.findModuleFile = findModuleFile; | ||
//# sourceMappingURL=files.js.map |
@@ -52,6 +52,3 @@ "use strict"; | ||
const opts = Object.assign(Object.assign({}, defaultOptions), options); | ||
let modulePath = id; | ||
if (/^\.\.?\//.test(id)) { | ||
modulePath = path_1.default.join(opts.basePath, id); | ||
} | ||
const modulePath = /^\.\.?\//.test(id) ? (0, files_1.findModuleFile)(id, opts.basePath) : id; | ||
const es6Require = createEs6Require(opts.esmOptions); | ||
@@ -58,0 +55,0 @@ try { |
@@ -10,1 +10,11 @@ /** | ||
export declare const getCallerFilePath: () => string; | ||
/** | ||
* 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 declare const findModuleFile: (modulePath: string, basePath: string) => string; |
@@ -0,1 +1,4 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { VALID_FILE_EXTENSIONS } from './config'; | ||
/** | ||
@@ -18,2 +21,26 @@ * Get the file path of the caller function. | ||
}; | ||
/** | ||
* 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, basePath) => { | ||
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}]`); | ||
}; | ||
//# sourceMappingURL=files.js.map |
import esm from 'esm'; | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import { getCallerFilePath } from './files'; | ||
import { findModuleFile, getCallerFilePath } from './files'; | ||
/** | ||
@@ -47,6 +47,3 @@ * Creates an esm-compatible require function that can import ES Modules | ||
const opts = Object.assign(Object.assign({}, 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); | ||
@@ -53,0 +50,0 @@ try { |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "2.0.5", | ||
"version": "2.0.6", | ||
"files": [ | ||
@@ -51,4 +51,4 @@ "dist" | ||
"@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", | ||
@@ -55,0 +55,0 @@ "node-fetch": "^3.3.2", |
@@ -39,3 +39,3 @@ <div align="center"> | ||
| ||
[![Downloads Total](https://badgen.net/npm/dy/import-sync)](https://moiva.io/?npm=import-sync) | ||
[![Downloads Yearly](https://badgen.net/npm/dy/import-sync)](https://moiva.io/?npm=import-sync) | ||
| ||
@@ -148,2 +148,3 @@ [![Downloads Monthly](https://badgen.net/npm/dm/import-sync)](https://moiva.io/?npm=import-sync) | ||
- `importSync` will look for `'./minimal.js'` before `'./minimal.mjs'` | ||
- `importSync` will look for matching extensions in the order `[.js, .mjs, .cjs]` | ||
- `'node-fetch'` | ||
@@ -150,0 +151,0 @@ - `importSync` can import pure-esm [node-fetch](https://github.com/node-fetch/node-fetch) (v3) in your cjs project |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
36204
33
328
322
6