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) { ${moduleCode} })(require, module, module.exports)`;
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 = './src/index.js'; // Update this with your entry file
const bundledCode = bundle(entryFile);
fs.writeFileSync('./bundle.js', bundledCode, 'utf8');