[add] same import primitive

This commit is contained in:
2025-08-25 16:52:49 +01:00
parent 51a859adf1
commit 09adbb78f1
5 changed files with 53 additions and 26 deletions

36
lib/safeImport.cjs Normal file
View File

@@ -0,0 +1,36 @@
// import path from 'node:path';
// import {} from '../src/index.mjs'
// import {createRequire} from 'node:module'
const path = require('node:path')
const {pathToFileURL} = require('node:url')
const {createRequire,findPackageJSON,} = require('node:module');
/*
Base Requirements:
- Should work in atleast CJS contexts.
*/
/**
*
* @param {string} modulePath
*/
function safeImport(modulePath, projectRoot=process.cwd()) {
const moduleDistPath = path.resolve('/home/atreyab/Documents/Docs/SlicingImport/repos-js/safeImport/dist');
// const executingModuleName = path.basename(path.resolve(modulePath));
// console.log(requestedModulePath)
// TODO - use something other than the cwd
const packageFileLocation = projectRoot?? findPackageJSON(pathToFileURL( require.main.filename))
if(packageFileLocation===null||packageFileLocation===undefined) {
throw new Error("Could not find package.json in the current working directory or any of its parent directories. Please provide a valid project root.");
}
const dirname = path.basename( path.dirname(packageFileLocation))
const requestedModulePath = path.resolve(moduleDistPath, dirname, 'index.cjs');
console.log("requesting module",requestedModulePath)
const x = createRequire(requestedModulePath);
return x(modulePath);
}
module.exports.safeImport = safeImport;
// const x = safeImport('parsejson');
// console.log("x",x);