From 9ea033b4ee057d63591d0f87560f788e8b6f605d Mon Sep 17 00:00:00 2001 From: bat Date: Fri, 7 Apr 2023 06:59:40 +0000 Subject: [PATCH] Add builder --- builder.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 builder.js 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 ` + + + + Editor + + + +${'<'}script type="module"> +window.Macchiato = {modules: {}} +${' +${modules.join("\n")} +${'<'}script type="module"> + +${' + + + `.trim() + } +} \ No newline at end of file