mirror of
https://github.com/morten-olsen/morten-olsen.github.io.git
synced 2026-02-08 01:46:28 +01:00
init
This commit is contained in:
52
bin/resources/script/index.ts
Normal file
52
bin/resources/script/index.ts
Normal 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 };
|
||||
Reference in New Issue
Block a user