Last active
February 3, 2022 02:01
-
-
Save sirlancelot/d2131f1a0516538cda13924f8ffc6263 to your computer and use it in GitHub Desktop.
Use rollup within Bitburner to bundle scripts before copying them to other servers. https://danielyxie.github.io/bitburner/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { rollup, VERSION } from "https://cdn.jsdelivr.net/npm/rollup/+esm" | |
const basePath = (path) => { | |
const index = path.lastIndexOf("/") | |
return index != -1 ? path.slice(0, index) : "" | |
} | |
/** @param {NS} ns */ | |
const bitburnerResolve = (ns) => ({ | |
name: "BitburnerResolve", | |
resolveId(source, importer) { | |
const base = importer ? basePath(importer) : "" | |
let url = new URL(source, `file:${base}`).pathname | |
// Bitburner: top-level files don't start with slash in `fileExists` | |
// lookup table. | |
if (url.lastIndexOf("/") === 0) url = url.slice(1) | |
return ns.fileExists(url) ? url : null | |
}, | |
load: (id) => ns.read(id), | |
}) | |
/** | |
* @param {NS} ns | |
* @param {string} input | |
*/ | |
export async function bundle(ns, input) { | |
let bundle, output | |
try { | |
bundle = await rollup({ | |
input, | |
plugins: [ | |
bitburnerResolve(ns), | |
], | |
}) | |
output = await bundle.generate({ | |
compact: true, | |
}).then((result) => result.output) | |
} finally { | |
if (bundle) bundle.close() | |
} | |
if (output.length > 1) | |
throw new Error("Don't use async chunks") | |
return output[0].code | |
} | |
/** @param {NS} ns */ | |
export async function main(ns) { | |
ns.tprint([ | |
``, | |
`Rollup v${VERSION} for Bitburner`, | |
``, | |
`Add the following line to your scripts:`, | |
` import { bundle } from "${ns.getScriptName()}"`, | |
``, | |
`Use it like so:`, | |
` const code = await bundle(ns, "file.js")`, | |
` await ns.write("file.min.js", code, "w")`, | |
``, | |
].join("\n")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment