[add] same import primitive
This commit is contained in:
36
lib/safeImport.cjs
Normal file
36
lib/safeImport.cjs
Normal 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);
|
@@ -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);
|
9
libtest/index.cjs
Normal file
9
libtest/index.cjs
Normal file
@@ -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"));
|
||||
|
7
libtest/index.mjs
Normal file
7
libtest/index.mjs
Normal file
@@ -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'));
|
@@ -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, } });
|
||||
|
Reference in New Issue
Block a user