justrequire
v1.0.6
Published
just require `.ts` or `ESM` format js file as `require()`
Downloads
7
Readme
justrequire
just require .ts
or ESM
format js file as require()
Usage
const justrequire = require('justrequire');
const ret = justrequire('./test.ts');
console.log(ret);
const chalkesm = justrequire('chalk');
console.log(chalkesm);
How
- transform
x.ts
tox.js
by esbuild - then
var ret = require('x.js')
- before return, delete the file
x.js
- return
ret
Source Code
var path = require('path');
var fs = require('fs');
function justrequire (moduleName, pathPrefix) {
var prefix = pathPrefix || process.cwd()
if (/\.json$/.test(moduleName)) return require(moduleName);
try {
var filename = require.resolve(moduleName);
var jsFilename = filename.replace(/\.(ts|tsx|cts|mts|js|cjs|mjs)$/, '__requirets__.cjs');
// console.log('------------------------------------');
// console.log('--require.resolve("'+moduleName+'");')
// console.log(filename);
// console.log('------------------------------------');
// console.log(jsFilename);
// console.log('------------------------------------');
require('esbuild').buildSync({
entryPoints: [filename],
bundle: true,
platform: 'node',
format: "cjs",
outfile: jsFilename,
});
var ret = require(jsFilename);
fs.unlinkSync(jsFilename);
return ret;
} catch (_) {
var filename = path.isAbsolute(moduleName)
? moduleName
: path.resolve(prefix, moduleName);
var exist = fs.existsSync(filename);
// console.log('------------------------------------');
// console.log('--path.resolve----');
// console.log('------------------------------------');
// console.log(filename);
// console.log('------------------------------------');
// console.log(jsFilename);
// console.log('------------------------------------');
if (!exist) throw new Error(`justrequire module ${moduleName} not exist`);
var jsFilename = filename.replace(/\.(ts|tsx|cts|mts|js|cjs|mjs)$/, '__requirets__.cjs');
require('esbuild').buildSync({
entryPoints: [filename],
bundle: true,
platform: 'node',
format: "cjs",
outfile: jsFilename,
});
var ret = require(jsFilename);
fs.unlinkSync(jsFilename);
return ret;
}
}
module.exports = justrequire;