This commit is contained in:
Morten Olsen
2023-03-26 22:15:07 +02:00
commit 9b1a067d56
80 changed files with 7889 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
import { Observable } from "../../observable";
import { InputPluginOption, ModuleFormat, watch } from "rollup";
type ScriptOptions = {
path: string;
format: ModuleFormat;
plugins?: InputPluginOption;
};
const build = (options: ScriptOptions, update: (code: string) => void) =>
new Promise<string>((resolve, reject) => {
let compiled = false;
const watcher = watch({
input: options.path,
plugins: options.plugins,
onwarn: () => { },
output: {
format: options.format,
},
watch: {
skipWrite: true,
},
});
watcher.on("event", async (event) => {
if (event.code === "BUNDLE_END") {
const { output } = await event.result.generate({
format: options.format,
});
const { code } = output[0];
if (!compiled) {
resolve(code);
compiled = true;
} else {
update(code);
}
}
if (event.code === "ERROR") {
reject(event.error);
}
});
});
const createScript = (options: ScriptOptions) => {
const script: Observable<string> = new Observable(() =>
build(options, (code) => script.set(() => Promise.resolve(code)))
);
return script;
};
export { createScript };