diff --git a/builder.js b/builder.js new file mode 100644 index 0000000..f0fc4f0 --- /dev/null +++ b/builder.js @@ -0,0 +1,82 @@ +class Builder { + constructor(files) { + this.files = files + } + + buildModule(file) { + const script = document.createElement('script') + script.setAttribute('type', 'module') + let initAppend = "" + let append = "" + const data = file.data.replaceAll( + /^\s*export\s+(?:class|function|async\s+function|const)\s+(\S+)/gms, + (match, p1) => { + const path = JSON.stringify(file.name) + const mref = `Macchiato.modules[${path}]` + const pref = `[${JSON.stringify(p1)}]` + initAppend = `\n\n${mref} = {}` + const s = `${mref}${pref} = ${p1}` + append += "\n" + s + return `// append: ${s}\n${match}` + } + ).replaceAll( + /^\s*import\s+(\{[^}]+\})\s+from\s+("[^"]+"|')/gms, + (match, p1, p2) => { + const vars = p1.replaceAll(' as ', ': ') + const importPath = p2.slice(1, -1) + const path = JSON.stringify( + importPath.slice( + importPath.indexOf('/') + 1 + ) + ) + const ref = `Macchiato.modules[${path}]` + return `const ${vars} = ${ref}` + } + ) + script.textContent = ( + "\n" + data + initAppend + append + "\n" + ) + return script.outerHTML + } + + build() { + const modules = this.files.map(file => { + return this.buildModule(file) + }) + return ` + + +
+