| 92 | } |
| 93 | |
| 94 | function createBundle(modules, entryId) { |
| 95 | const moduleEntries = []; |
| 96 | for (const [id, info] of modules.entries()) { |
| 97 | const deps = JSON.stringify(info.dependencyMap || {}); |
| 98 | moduleEntries.push( |
| 99 | `'${id}': { factory: function(module, exports, require) {\n${info.code}\n}, map: ${deps} }`, |
| 100 | ); |
| 101 | } |
| 102 | return `(function(){\n const modules = {\n${moduleEntries.join(',\n')}\n };\n const cache = {};\n function load(id) {\n if (cache[id]) {\n return cache[id];\n }\n const entry = modules[id];\n if (!entry) {\n throw new Error('Unknown module ' + id);\n }\n const module = { exports: {} };\n cache[id] = module.exports;\n entry.factory(module, module.exports, createRequire(id));\n cache[id] = module.exports;\n return cache[id];\n }\n function createRequire(parentId) {\n return function(request) {\n const parent = modules[parentId];\n if (!parent) {\n throw new Error('Unknown parent module ' + parentId);\n }\n const resolved = parent.map && parent.map[request];\n if (!resolved) {\n throw new Error('Cannot resolve module ' + request + ' from ' + parentId);\n }\n return load(resolved);\n };\n }\n load('${entryId}');\n})();`; |
| 103 | } |
| 104 | |
| 105 | async function inlineStyles(html, baseDir) { |
| 106 | const linkRegex = /<link\s+rel=["']stylesheet["']\s+href=["'](.+?)["']\s*\/?>(?:\s*<\/link>)?/gi; |