Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/cli/src/loader/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const preferredLoader = process.env.CHECKLY_PREFERRED_LOADER

export function isPreferredLoader (name: string): boolean {
return preferredLoader === name
}

export function preferenceDelta (name: string): number {
return isPreferredLoader(name) ? 200 : 0
}
4 changes: 4 additions & 0 deletions packages/cli/src/loader/jiti.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FileLoader, FileLoaderOptions, UnsupportedFileLoaderError } from './loader'
import { FileMatch } from './match'
import { preferenceDelta } from './config'

interface JitiExports {
createJiti (id: string, userOptions?: any): Jiti
Expand Down Expand Up @@ -65,11 +66,14 @@ export class InitializedJitiFileLoaderState extends FileLoader {
export type JitiFileLoaderOptions = FileLoaderOptions

export class JitiFileLoader extends FileLoader {
static DEFAULT_PRIORITY = 500 + preferenceDelta('jiti')

static state: FileLoader = new UninitializedJitiFileLoaderState()

constructor (options?: JitiFileLoaderOptions) {
super({
match: FileMatch.standardFiles().complement(),
priority: JitiFileLoader.DEFAULT_PRIORITY,
...options,
})
}
Expand Down
19 changes: 19 additions & 0 deletions packages/cli/src/loader/loader.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import { FileMatch } from './match'

export interface FileLoaderOptions {
/**
* The priority of the loader, used to resolve load order if multiple
* loaders are available.
*
* The higher the priority the earlier the load order.
*/
priority?: number
match?: FileMatch
}

export abstract class FileLoader {
static DEFAULT_PRIORITY = 1000

protected fileMatcher: FileMatch
#priority: number

constructor (options?: FileLoaderOptions) {
this.fileMatcher = options?.match ?? FileMatch.any()
this.#priority = options?.priority ?? FileLoader.DEFAULT_PRIORITY
}

/**
* The priority of the loader, used to resolve load order if multiple
* loaders are available.
*/
get priority () {
return this.#priority
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/loader/mixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class MixedFileLoader extends FileLoader {

constructor (...loaders: FileLoader[]) {
super()
this.loaders = new Set(loaders)
this.loaders = new Set([...loaders].sort((a, b) => b.priority - a.priority))
}

isAuthoritativeFor (filePath: string): boolean {
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/loader/ts-node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { preferenceDelta } from './config'
import { FileLoader, FileLoaderOptions, UnsupportedFileLoaderError } from './loader'
import { FileMatch } from './match'

Expand Down Expand Up @@ -85,11 +86,14 @@ export class InitializedTSNodeFileLoaderState extends FileLoader {
export type TSNodeFileLoaderOptions = FileLoaderOptions

export class TSNodeFileLoader extends FileLoader {
static DEFAULT_PRIORITY = 500 + preferenceDelta('ts-node')

static state: FileLoader = new UninitializedTSNodeFileLoaderState()

constructor (options?: TSNodeFileLoaderOptions) {
super({
match: FileMatch.standardFiles().complement(),
priority: TSNodeFileLoader.DEFAULT_PRIORITY,
...options,
})
}
Expand Down