mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-14 16:47:55 +00:00
Docs: Update benchmark for test 4
This commit is contained in:
@@ -1,76 +1,9 @@
|
||||
async function bundleApp(entryFile, files) {
|
||||
const { transform } = await import('https://unpkg.com/@babel/standalone/babel.min.js');
|
||||
async function renderTemplate(templateString, data) {
|
||||
const { default: Handlebars } = await import(
|
||||
'https://cdn.jsdelivr.net/npm/handlebars@4.7.8/dist/handlebars.min.js'
|
||||
);
|
||||
|
||||
const modules = {};
|
||||
const visited = new Set();
|
||||
|
||||
const normalizePath = (path) => {
|
||||
return path.replace(/^\.\//, '').replace(/\.js$/, '') + '.js';
|
||||
};
|
||||
|
||||
const extractRequires = (code) => {
|
||||
const requires = [];
|
||||
const regex = /require\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
|
||||
let match;
|
||||
while ((match = regex.exec(code)) !== null) {
|
||||
requires.push(match[1]);
|
||||
}
|
||||
return requires;
|
||||
};
|
||||
|
||||
const traverse = (fileName) => {
|
||||
const normalizedName = normalizePath(fileName);
|
||||
|
||||
if (visited.has(normalizedName)) return;
|
||||
if (!files[normalizedName]) {
|
||||
throw new Error(`Missing file: ${normalizedName}`);
|
||||
}
|
||||
|
||||
visited.add(normalizedName);
|
||||
|
||||
const transformed = transform(files[normalizedName], {
|
||||
presets: ['env']
|
||||
}).code;
|
||||
|
||||
modules[normalizedName] = transformed;
|
||||
|
||||
const deps = extractRequires(transformed);
|
||||
deps.forEach(dep => traverse(dep));
|
||||
};
|
||||
|
||||
traverse(entryFile);
|
||||
|
||||
const moduleEntries = Object.entries(modules).map(([name, code]) =>
|
||||
`${JSON.stringify(name)}: function(require, module, exports) {\n${code}\n}`
|
||||
).join(',\n');
|
||||
|
||||
return `(function() {
|
||||
const modules = {
|
||||
${moduleEntries}
|
||||
};
|
||||
|
||||
const cache = {};
|
||||
|
||||
function require(name) {
|
||||
const normalized = name.replace(/^\.\//, '').replace(/\.js$/, '') + '.js';
|
||||
|
||||
if (cache[normalized]) {
|
||||
return cache[normalized].exports;
|
||||
}
|
||||
|
||||
if (!modules[normalized]) {
|
||||
throw new Error('Module not found: ' + normalized);
|
||||
}
|
||||
|
||||
const module = { exports: {} };
|
||||
cache[normalized] = module;
|
||||
|
||||
modules[normalized](require, module, module.exports);
|
||||
|
||||
return module.exports;
|
||||
}
|
||||
|
||||
return require(${JSON.stringify(normalizePath(entryFile))});
|
||||
})();`;
|
||||
const template = Handlebars.compile(templateString);
|
||||
return template(data);
|
||||
}
|
||||
export default bundleApp;
|
||||
export default renderTemplate;
|
||||
Reference in New Issue
Block a user