const fs = require('fs'); const path = require('path'); function bundle(entryFile) { const entryContent = fs.readFileSync(entryFile, 'utf8'); const modules = {}; function require(modulePath) { const fullPath = path.resolve(__dirname, modulePath); if (modules[fullPath]) { return modules[fullPath].exports; } const module = { exports: {} }; modules[fullPath] = module; // Execute the module code with `require`, `module`, and `exports` // available as local variables. const moduleCode = fs.readFileSync(fullPath, 'utf8'); const wrappedCode = `(function (require, module, exports, __dirname, __filename) { ${moduleCode} })(require, module, module.exports, "${path.dirname(fullPath)}", "${fullPath}")`; eval(wrappedCode); return module.exports; } require(entryFile); // Create the bundle const bundledCode = Object.keys(modules) .map(modulePath => { const moduleName = JSON.stringify(path.relative(__dirname, modulePath)); const moduleContent = modules[modulePath].exports.toString(); return `// ${moduleName}\n${moduleContent}`; }) .join('\n\n'); return bundledCode; } const entryFile = path.resolve(__dirname,'src/index.js'); // Update this with your entry file const bundledCode = bundle(entryFile); fs.writeFileSync('./bundle.js', bundledCode, 'utf8');