const path = require('path'); const fs = require('fs'); const DIR_BUILT_SCRIPTS = path.join('build', 'scripts'); const DIR_OUT = path.join('build', 'assets', 'scripts'); let doneFiles = []; let full = ` var exports = {}; `; const outputFix = (output, dir) => { let out = output.replace(/exports\.\_\_esModule\ =\ true\;/gm, ''); // Replace requires with exports. const reg = /var\ (.*?)\ \= require\("(.*?)"\)\;/gm; const matches = out.matchAll(reg); let match; while(!(match = matches.next()).done) { const exp = new RegExp(`${match.value[1]}\.`); exp.multiline = true; scanFile(path.join(dir, match.value[2]), dir); out = out.replace(match.value[0], '').replace(exp, 'exports.'); } // Remove exports.whatever = void 0; out = out.replace(/exports\.(.*?) \= void 0\;/gm, ''); return out; } const scanFile = (file, dir) => { if(!file.endsWith('.js')) file += '.js'; if(doneFiles.indexOf(file) !== -1) return; doneFiles.push(file); const contents = fs.readFileSync(file, 'utf-8'); const f = outputFix(contents, dir); full = full + `\n//${file}\n` + f; } const scanDir = dir => fs.readdirSync(dir).forEach(file => { if(!file.endsWith('.js')) return; const joint = path.join(DIR_BUILT_SCRIPTS, file); const stat = fs.statSync(joint); if(stat.isDirectory()) { scanDir(joint); } else { scanFile(joint, dir); } }); scanDir(DIR_BUILT_SCRIPTS); if(!fs.existsSync(DIR_OUT)) fs.mkdirSync(DIR_OUT); fs.writeFileSync(path.join(DIR_OUT, 'main.js'), full);