Skip to main content

Class: LoaderStrategy<T>

A multi-purpose feature-complete loader strategy supporting multi-piece modules as well as supporting both ECMAScript Modules and CommonJS with reloading support.

Type parameters

Type parameter
T extends Piece

Implements

Constructors

new LoaderStrategy()

new LoaderStrategy<T>(): LoaderStrategy<T>

Returns

LoaderStrategy<T>

Source

projects/pieces/src/lib/strategies/LoaderStrategy.ts:30

Properties

clientUsesESModules

clientUsesESModules: boolean

Source

projects/pieces/src/lib/strategies/LoaderStrategy.ts:26


filterDtsFiles

private readonly filterDtsFiles: boolean = false

Source

projects/pieces/src/lib/strategies/LoaderStrategy.ts:28


supportedExtensions

supportedExtensions: string[]

Source

projects/pieces/src/lib/strategies/LoaderStrategy.ts:27

Methods

filter()

filter(path: string): FilterResult

Retrieves the name and the extension of the specified file path.

Parameters

ParameterTypeDescription
pathstringThe path of the file to be processed.

Returns

FilterResult

A ModuleData on success, otherwise null to stop the store from processing the path.

Implementation of

ILoaderStrategy . filter

Example

// ts-node support
class MyStrategy extends LoaderStrategy {
filter(path) {
const extension = extname(path);
if (!['.js', '.ts'].includes(extension)) return null;
const name = basename(path, extension);
return { extension, name };
}
}

Source

projects/pieces/src/lib/strategies/LoaderStrategy.ts:55


load()

load(store: Store<T, never>, file: HydratedModuleData): ILoaderResult<T>

The load hook, use this to override the loader.

Parameters

ParameterType
storeStore<T, never>
fileHydratedModuleData

Returns

ILoaderResult<T>

Implementation of

ILoaderStrategy . load

Example

class MyStrategy extends LoaderStrategy {
load(store, file) {
// ...
}
}

Source

projects/pieces/src/lib/strategies/LoaderStrategy.ts:86


onError()

onError(error: Error, path: string): void

Parameters

ParameterTypeDescription
errorErrorThe error that was thrown.
pathstringThe path of the file that caused the error to be thrown.

Returns

void

Implementation of

ILoaderStrategy . onError

Source

projects/pieces/src/lib/strategies/LoaderStrategy.ts:129


onLoad()

onLoad(store: Store<T, never>, piece: T): unknown

Called after a piece has been loaded, but before Piece.onLoad and Store.set.

Parameters

ParameterTypeDescription
storeStore<T, never>The store that holds the piece.
pieceTThe piece that was loaded.

Returns

unknown

Implementation of

ILoaderStrategy . onLoad

Source

projects/pieces/src/lib/strategies/LoaderStrategy.ts:109


onLoadAll()

onLoadAll(store: Store<T, never>): unknown

Called after all pieces have been loaded.

Parameters

ParameterTypeDescription
storeStore<T, never>The store that loaded all pieces.

Returns

unknown

Implementation of

ILoaderStrategy . onLoadAll

Source

projects/pieces/src/lib/strategies/LoaderStrategy.ts:114


onUnload()

onUnload(store: Store<T, never>, piece: T): unknown

Called after a piece has been unloaded or overwritten by a newly loaded piece.

Parameters

ParameterTypeDescription
storeStore<T, never>The store that held the piece.
pieceTThe piece that was unloaded.

Returns

unknown

Implementation of

ILoaderStrategy . onUnload

Source

projects/pieces/src/lib/strategies/LoaderStrategy.ts:119


onUnloadAll()

onUnloadAll(store: Store<T, never>): unknown

Called after all pieces have been unloaded.

Parameters

ParameterTypeDescription
storeStore<T, never>The store that unloaded all pieces.

Returns

unknown

Implementation of

ILoaderStrategy . onUnloadAll

Source

projects/pieces/src/lib/strategies/LoaderStrategy.ts:124


preload()

preload(file: ModuleData): AsyncPreloadResult<T>

The pre-load hook, use this to override the loader.

Parameters

ParameterType
fileModuleData

Returns

AsyncPreloadResult<T>

Implementation of

ILoaderStrategy . preload

Examples

// CommonJS support:
class MyStrategy extends LoaderStrategy {
preload(path) {
return require(path);
}
}
// ESM support:
class MyStrategy extends LoaderStrategy {
preload(file) {
return import(file.path);
}
}

Source

projects/pieces/src/lib/strategies/LoaderStrategy.ts:70


walk()

walk(store: Store<T, never>, path: string, logger?: null | StoreLogger): AsyncIterableIterator<string>

Walks the specified path and returns an async iterator of all the files' paths.

Parameters

ParameterTypeDescription
storeStore<T, never>The store that is walking the path.
pathstringThe path to recursively walk.
logger?null | StoreLoggerThe logger to use when walking the path, if any.

Returns

AsyncIterableIterator<string>

Implementation of

ILoaderStrategy . walk

Source

projects/pieces/src/lib/strategies/LoaderStrategy.ts:133