|
1 | | -import { readdir as readDir, writeFile, unlink, mkdir as mkDir, readFile, copyFile } from "fs/promises" |
| 1 | +import { readdir as readDir, writeFile, mkdir as mkDir, readFile, copyFile } from "fs/promises" |
2 | 2 | import { watch as watchDir } from "chokidar" |
3 | 3 | import { minify } from "terser" |
4 | 4 | import { resolve as resolvePath, basename, extname } from "path" |
@@ -219,29 +219,33 @@ export async function pull(srcPath: string, hackmudPath: string, script: string) |
219 | 219 | * @param script JavaScript or TypeScript code |
220 | 220 | */ |
221 | 221 | export async function minifyScript(script: string) { |
222 | | - const uid = Date.now().toString(36) |
| 222 | + // const uid = Date.now().toString(36) |
223 | 223 | const autocompleteMatch = script.match(/^(?:\/\/ @autocomplete (.+)|function(?: \w+| )?\([^\)]*\)\s*{\s*\/\/(.+))\n/) |
224 | | - const scriptLines = script.split("\n") |
| 224 | + // const scriptLines = script.split("\n") |
225 | 225 |
|
226 | | - for (let i = 0; i < scriptLines.length; i++) { |
227 | | - const line = scriptLines[i] |
| 226 | + // for (let i = 0; i < scriptLines.length; i++) { |
| 227 | + // const line = scriptLines[i] |
228 | 228 |
|
229 | | - if (/\s*function\s*\(/.exec(line)?.index == 0) |
230 | | - break |
| 229 | + // if (/\s*function\s*\(/.exec(line)?.index == 0) |
| 230 | + // break |
231 | 231 |
|
232 | | - if (!(!line || /[^\s]/.exec(line) == null || /\s*\/\//.exec(line)?.index == 0)) { |
233 | | - scriptLines.splice(i, 0, "function (context, args) {") |
234 | | - scriptLines.push("}") |
235 | | - break |
236 | | - } |
237 | | - } |
| 232 | + // if (!(!line || /[^\s]/.exec(line) == null || /\s*\/\//.exec(line)?.index == 0)) { |
| 233 | + // scriptLines.splice(i, 0, "function (context, args) {") |
| 234 | + // scriptLines.push("}") |
| 235 | + // break |
| 236 | + // } |
| 237 | + // } |
| 238 | + |
| 239 | + // script = scriptLines.join("\n") |
238 | 240 |
|
239 | | - script = scriptLines.join("\n") |
| 241 | + // // preprocessing |
| 242 | + // script = script |
| 243 | + // .replace(/function(?: \w+| )?\(/, `function script_${uid}(`) |
| 244 | + // .replace(/#[\w.]+\(/g, a => a.replace("#", `_hash_${uid}_`).replace(/\./g, `_dot_${uid}_`)) |
240 | 245 |
|
241 | | - // preprocessing |
242 | 246 | script = script |
243 | | - .replace(/function(?: \w+| )?\(/, `function script_${uid}(`) |
244 | | - .replace(/#[\w.]+\(/g, a => a.replace("#", `_hash_${uid}_`).replace(/\./g, `_dot_${uid}_`)) |
| 247 | + .replace("export ", "") |
| 248 | + .replace(/\$([\w.]+\()/g, a => "$" + a.slice(1).replace(/\./g, "$")) |
245 | 249 |
|
246 | 250 | // compilation |
247 | 251 | script = transpileModule(script, { |
@@ -276,11 +280,15 @@ export async function minifyScript(script: string) { |
276 | 280 | printWidth: Infinity |
277 | 281 | }) |
278 | 282 |
|
279 | | - // postprocessing |
| 283 | + // // postprocessing |
| 284 | + // script = script |
| 285 | + // .replace(`script_${uid}`, "") |
| 286 | + // .replace(new RegExp(`_hash_${uid}_`, "g"), "#") |
| 287 | + // .replace(new RegExp(`_dot_${uid}_`, "g"), ".") |
| 288 | + |
280 | 289 | script = script |
281 | | - .replace(`script_${uid}`, "") |
282 | | - .replace(new RegExp(`_hash_${uid}_`, "g"), "#") |
283 | | - .replace(new RegExp(`_dot_${uid}_`, "g"), ".") |
| 290 | + .replace(/\$[\w\$]+\(/g, a => a.replace("$", "#").replace(/\$/g, ".")) |
| 291 | + .replace(/function ?\w+\(/, "function (") |
284 | 292 |
|
285 | 293 | if (autocompleteMatch) |
286 | 294 | return script.replace(/function \(.*\) \{/, `$& // ${(autocompleteMatch[1] || autocompleteMatch[2]).trim()}`) |
|
0 commit comments