From bdbb99ecfd40fefd340325e29d25bf3e76c62191 Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Mon, 18 Mar 2024 13:00:25 -0700 Subject: [PATCH] Fix type issues with loadStyles options and compiler types --- src/App.ts | 2 +- src/AppForServer.ts | 15 +++++++++------ src/files.ts | 11 ++++++++--- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/App.ts b/src/App.ts index 30636be5..7add9e7f 100644 --- a/src/App.ts +++ b/src/App.ts @@ -98,7 +98,7 @@ export abstract class AppBase extends EventEmitter { } abstract _init(options?: AppOptions); - loadViews(_viewFilename, _viewName) { } + loadViews(_viewFilename, _viewName?) { } loadStyles(_filename, _options) { } component(constructor: ComponentConstructor | SingletonComponentConstructor): this; diff --git a/src/AppForServer.ts b/src/AppForServer.ts index a50a75e6..b56e81ab 100644 --- a/src/AppForServer.ts +++ b/src/AppForServer.ts @@ -8,12 +8,15 @@ import * as racer from 'racer'; -const util = racer.util; import { AppBase } from './App'; +import { type DerbyBase } from './Derby'; +import { type StyleCompilerOptions } from './files'; import { PageForServer } from './PageForServer'; import parsing = require('./parsing'); import * as derbyTemplates from './templates'; +const util = racer.util; + interface Agent { send(message: Record): void; } @@ -42,7 +45,7 @@ function htmlCompiler(file) { return file; } -type CompilerFunciton = (file: string, filename?: string, options?: unknown) => unknown; +type CompilerFunciton = (file: string, filename?: string, options?: unknown) => string; function watchOnce(filenames, callback) { const watcher = chokidar.watch(filenames); @@ -76,7 +79,7 @@ export class AppForServer extends AppBase { watchFiles: boolean; router: any; - constructor(derby, name: string, filename: string, options) { + constructor(derby: DerbyBase, name: string, filename: string, options) { super(derby, name, filename, options); this._init(options); } @@ -208,7 +211,7 @@ export class AppForServer extends AppBase { this.scriptMapUrl = (this.scriptMapBaseUrl || serialized.scriptMapBaseUrl) + serialized.scriptMapUrl; } - loadViews(filename, namespace) { + loadViews(filename: string, namespace?: string) { const data = files.loadViewsSync(this, filename, namespace); parsing.registerParsedViews(this, data.views); if (this.watchFiles) this._watchViews(data.files, filename, namespace); @@ -216,7 +219,7 @@ export class AppForServer extends AppBase { return this; } - loadStyles(filename, options) { + loadStyles(filename: string, options?: StyleCompilerOptions) { this._loadStyles(filename, options); const stylesView = this.views.find('Styles'); stylesView.source += ''; @@ -224,7 +227,7 @@ export class AppForServer extends AppBase { return this; } - private _loadStyles(filename, options) { + private _loadStyles(filename: string, options?: StyleCompilerOptions) { const styles = files.loadStylesSync(this, filename, options); let filepath = ''; diff --git a/src/files.ts b/src/files.ts index 481cc821..d812df69 100644 --- a/src/files.ts +++ b/src/files.ts @@ -9,9 +9,10 @@ import * as path from 'node:path'; import * as racer from 'racer'; import * as resolve from 'resolve'; +import { type AppForServer } from './AppForServer'; import * as parsing from './parsing'; -export function loadViewsSync(app, sourceFilename, namespace) { +export function loadViewsSync(app: AppForServer, sourceFilename: string, namespace: string) { let views = []; let files = []; const filename = resolve.sync(sourceFilename, { @@ -43,7 +44,7 @@ export function loadViewsSync(app, sourceFilename, namespace) { files = files.concat(imported.files); } - const htmlFile = compiler(file, filename); + const htmlFile = compiler(file, filename) as string; const parsedViews = parsing.parseViews(htmlFile, namespace, filename, onImport); return { views: views.concat(parsedViews), @@ -51,7 +52,11 @@ export function loadViewsSync(app, sourceFilename, namespace) { }; } -export function loadStylesSync(app, sourceFilename, options) { +export interface StyleCompilerOptions extends Record { + compress?: boolean; +} + +export function loadStylesSync(app: AppForServer, sourceFilename: string, options?: StyleCompilerOptions) { if (options == null) { options = { compress: racer.util.isProduction }; }