From e4fe93fe215a661df68c5880df4e852637f49c90 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 8 Dec 2022 03:07:43 +0000 Subject: [PATCH] automatically patch devicescript with missing imports --- src/workers/devicescript/devicescript-worker.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/workers/devicescript/devicescript-worker.ts b/src/workers/devicescript/devicescript-worker.ts index ed37e2ca15df..de5349e7f326 100644 --- a/src/workers/devicescript/devicescript-worker.ts +++ b/src/workers/devicescript/devicescript-worker.ts @@ -74,14 +74,18 @@ class WorkerHost { let serviceSpecs: jdspec.ServiceSpec[] +const DEVICESCRIPT_PREFIX = 'import * as ds from "@devicescript/core"' const handlers: { [index: string]: (props: any) => object | Promise } = { compile: async (props: DeviceScriptCompileRequest) => { - const { source } = props + let { source = "" } = props if (!serviceSpecs) throw new Error("specs missing") + + if (source.indexOf(DEVICESCRIPT_PREFIX) < 0) { + source = DEVICESCRIPT_PREFIX + "\n\n" + source + } const host = new WorkerHost(serviceSpecs) const res = compile(source, { host }) - return >{ ...res, files: host.files,