MCPcopy
hub / github.com/bytenode/bytenode / loaderCodeModule

Function loaderCodeModule

lib/index.js:558–605  ·  view source on GitHub ↗
(targetPath, sheBang, loaderFilePath)

Source from the content-addressed store, hash-verified

556};
557
558const loaderCodeModule = function (targetPath, sheBang, loaderFilePath) {
559 const lines = [
560 `import { createRequire } from 'node:module'`,
561 ``,
562 `import 'bytenode'`,
563 ``,
564 ``,
565 `const require = createRequire(import.meta.url)`,
566 ``
567 ];
568
569 if (sheBang) {
570 lines.unshift(sheBang, '');
571
572 lines.push(`require('${targetPath}')`);
573 } else {
574 // Only `require()` the module when not having a shebang, so we can be
575 // somewhat sure it's not an executable, to prevent running it. Also, an
576 // executable having exports is a code smell, they should be two different
577 // modules, using the executable the exported one as a library
578 let { default: defaultExport, ...namedExports } = require(loaderFilePath);
579
580 defaultExport = defaultExport ? 'default: defaultExport' : '';
581 namedExports = Object.keys(namedExports);
582
583 let exports = [];
584 if (defaultExport) {
585 exports.push(defaultExport);
586 }
587 exports = exports.concat(namedExports).join(', ');
588
589 if (!exports) {
590 lines.push(`require('${targetPath}')`);
591 }
592 else {
593 lines.push(`const {${exports}} = require('${targetPath}')`, ``, ``);
594
595 if (defaultExport) {
596 lines.push('export default defaultExport');
597 }
598 if (namedExports.length) {
599 lines.push(`export { ${namedExports} }`);
600 }
601 }
602 }
603
604 return lines.join('\n');
605};
606
607global.bytenode = {
608 compileCode,

Callers

nothing calls this directly

Calls 1

requireFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…