Class: LoaderStrategy<T>
Defined in: projects/pieces/src/lib/strategies/LoaderStrategy.ts:26
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
Constructor
new LoaderStrategy<
T>():LoaderStrategy<T>
Defined in: projects/pieces/src/lib/strategies/LoaderStrategy.ts:31
Returns
LoaderStrategy<T>
Properties
clientUsesESModules
clientUsesESModules:
boolean
Defined in: projects/pieces/src/lib/strategies/LoaderStrategy.ts:27
supportedExtensions
supportedExtensions:
string[]
Defined in: projects/pieces/src/lib/strategies/LoaderStrategy.ts:28
Methods
filter()
filter(
path:string):FilterResult
Defined in: projects/pieces/src/lib/strategies/LoaderStrategy.ts:38
Retrieves the name and the extension of the specified file path.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The path of the file to be processed. |
Returns
A ModuleData on success, otherwise null to stop the store from processing the path.
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 };
}
}
Implementation of
load()
load(
store:Store<T>,file:HydratedModuleData):ILoaderResult<T>
Defined in: projects/pieces/src/lib/strategies/LoaderStrategy.ts:73
The load hook, use this to override the loader.
Parameters
| Parameter | Type |
|---|---|
store | Store<T> |
file | HydratedModuleData |
Returns
Example
class MyStrategy extends LoaderStrategy {
load(store, file) {
// ...
}
}
Implementation of
onError()
onError(
error:Error,path:string):void
Defined in: projects/pieces/src/lib/strategies/LoaderStrategy.ts:116
Parameters
| Parameter | Type | Description |
|---|---|---|
error | Error | The error that was thrown. |
path | string | The path of the file that caused the error to be thrown. |
Returns
void
Implementation of
onLoad()
onLoad(
store:Store<T>,piece:T):unknown
Defined in: projects/pieces/src/lib/strategies/LoaderStrategy.ts:96
Called after a piece has been loaded, but before Piece.onLoad and Map.set.
Parameters
| Parameter | Type | Description |
|---|---|---|
store | Store<T> | The store that holds the piece. |
piece | T | The piece that was loaded. |
Returns
unknown
Implementation of
onLoadAll()
onLoadAll(
store:Store<T>):unknown
Defined in: projects/pieces/src/lib/strategies/LoaderStrategy.ts:101
Called after all pieces have been loaded.
Parameters
| Parameter | Type | Description |
|---|---|---|
store | Store<T> | The store that loaded all pieces. |
Returns
unknown
Implementation of
onUnload()
onUnload(
store:Store<T>,piece:T):unknown
Defined in: projects/pieces/src/lib/strategies/LoaderStrategy.ts:106
Called after a piece has been unloaded or overwritten by a newly loaded piece.
Parameters
| Parameter | Type | Description |
|---|---|---|
store | Store<T> | The store that held the piece. |
piece | T | The piece that was unloaded. |
Returns
unknown
Implementation of
onUnloadAll()
onUnloadAll(
store:Store<T>):unknown
Defined in: projects/pieces/src/lib/strategies/LoaderStrategy.ts:111
Called after all pieces have been unloaded.
Parameters
| Parameter | Type | Description |
|---|---|---|
store | Store<T> | The store that unloaded all pieces. |
Returns
unknown
Implementation of
preload()
preload(
file:ModuleData):AsyncPreloadResult<T>
Defined in: projects/pieces/src/lib/strategies/LoaderStrategy.ts:53
The pre-load hook, use this to override the loader.
Parameters
| Parameter | Type |
|---|---|
file | ModuleData |
Returns
Examples
// CommonJS support:
class MyStrategy extends LoaderStrategy {
preload(path) {
return require(path);
}
}
// ESM support:
class MyStrategy extends LoaderStrategy {
preload(file) {
return import(file.path);
}
}
Implementation of
walk()
walk(
store:Store<T>,path:string,logger?:null|StoreLogger):AsyncIterableIterator<string>
Defined in: projects/pieces/src/lib/strategies/LoaderStrategy.ts:120
Walks the specified path and returns an async iterator of all the files' paths.
Parameters
| Parameter | Type | Description |
|---|---|---|
store | Store<T> | The store that is walking the path. |
path | string | The path to recursively walk. |
logger? | null | StoreLogger | The logger to use when walking the path, if any. |
Returns
AsyncIterableIterator<string>