From 09adbb78f14a78202b61546e07f925f9a0f7424a Mon Sep 17 00:00:00 2001 From: Atreya Bain Date: Mon, 25 Aug 2025 16:52:49 +0100 Subject: [PATCH] [add] same import primitive --- lib/safeImport.cjs | 36 ++++++++++++++++++++++++++++++++++++ lib/safeImport.mjs | 25 ------------------------- libtest/index.cjs | 9 +++++++++ libtest/index.mjs | 7 +++++++ src/index.mjs | 2 +- 5 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 lib/safeImport.cjs delete mode 100644 lib/safeImport.mjs create mode 100644 libtest/index.cjs create mode 100644 libtest/index.mjs diff --git a/lib/safeImport.cjs b/lib/safeImport.cjs new file mode 100644 index 0000000..3c3be2e --- /dev/null +++ b/lib/safeImport.cjs @@ -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); \ No newline at end of file diff --git a/lib/safeImport.mjs b/lib/safeImport.mjs deleted file mode 100644 index b1cd34d..0000000 --- a/lib/safeImport.mjs +++ /dev/null @@ -1,25 +0,0 @@ -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 diff --git a/libtest/index.cjs b/libtest/index.cjs new file mode 100644 index 0000000..a8ce6cf --- /dev/null +++ b/libtest/index.cjs @@ -0,0 +1,9 @@ +// import {safeImport} from '../lib/safeImport.cjs' +const {safeImport} = require('../lib/safeImport.cjs') + + + +const classnames = safeImport('classnames','test_src/index.cjs'); + +console.log(classnames('hello',"bruh")); + diff --git a/libtest/index.mjs b/libtest/index.mjs new file mode 100644 index 0000000..34baf78 --- /dev/null +++ b/libtest/index.mjs @@ -0,0 +1,7 @@ +import {safeImport} from '../lib/safeImport.cjs' +// const {safeImport} = require('../lib/safeImport.cjs') + + + +const x = safeImport('classnames','test_src/index/index.cjs'); +console.log(x('hello')); \ No newline at end of file diff --git a/src/index.mjs b/src/index.mjs index d2c108b..f9c158a 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -94,7 +94,7 @@ function createPackageJsonForModule(moduleName, writePath) { * * @param {string} filePath */ -function driver(folderPath = './candidates/braces') { +function driver(folderPath = './test_src') { // const FILE_PATH = './test_src/index.cjs'; const project = new Project({ compilerOptions: { allowJs: true, checkJs: false, } });