(targetPath, sheBang, loaderFilePath)
| 556 | }; |
| 557 | |
| 558 | const 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 | |
| 607 | global.bytenode = { |
| 608 | compileCode, |
nothing calls this directly
no test coverage detected
searching dependent graphs…