npm i @putout/bundle
import {bundle} from '@putout/bundler';
console.log(bundle(CWD, entry, filesystem));
To Simplify things up all files converted to CommonJS first. Let's suppose none of them use top-level await to get things simpler.
Traverse all files starting from entry
and get all filenames.
Traverse filesystem and create object that contains filename and file content:
const __filesystem = {
'/entry.js': () => {
const client = require('/client.js');
console.log(client);
},
'/client.js': (exports, require, module) => {
module.exports = 'hello';
},
};
Most likely we need IIFE so couple bundles can be loaded on page simultaneously.
const __modules = {};
const __filesystem = {
'/entry.js': () => {
const client = require('/client.js');
console.log(client);
},
'/client.js': (exports, require, module) => {
module.exports = 'hello';
},
};
const require = (name) => {
const exports = {};
const module = {
exports,
};
if (__modules[name])
return __modules[name];
__filesystem[name](exports, require, module);
__modules[name] = module.exports;
return module.exports;
};
require('/entry.js');
MIT