From 0e30210785a3f34d425415b89061989326ab7b7a Mon Sep 17 00:00:00 2001 From: Atreya Bain Date: Fri, 22 Aug 2025 17:45:18 +0100 Subject: [PATCH] [add] safeImport primitive --- lib/safeImport.mjs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 lib/safeImport.mjs diff --git a/lib/safeImport.mjs b/lib/safeImport.mjs new file mode 100644 index 0000000..b1cd34d --- /dev/null +++ b/lib/safeImport.mjs @@ -0,0 +1,25 @@ +import path from 'node:path'; +import {} from '../src/index.mjs' +import {createRequire} from 'node:module' + + + +/* + Base Requirements: + - Should work in atleast CJS contexts. + */ + +/** + * + * @param {string} modulePath + */ +export function safeImport(modulePath) { + const moduleDistPath = path.resolve('/home/atreyab/Documents/Docs/SlicingImport/repos-js/safeImport/dist'); + const requestedModulePath = path.resolve(moduleDistPath, modulePath); + // const executingModuleName = path.basename(path.resolve(modulePath)); + // console.log(requestedModulePath) + const x = createRequire(moduleDistPath); + return x; +} +const x = safeImport('parsejson'); +console.log("x",x); \ No newline at end of file