@hyperfrontend/workspace/hookshooks
The resolution hook that loads a workspace's TypeScript from source: the path aliases of tsconfig.base.json, and extensionless relative imports completed the way the compiler does.
Importing the entry installs the hook for the workspace above the working directory, so one flag is all a script needs:
node --import @hyperfrontend/workspace/hooks tools/scripts/report.ts
The command line installs the hook itself for the root it finds, which is how an executor or generator a workspace writes in TypeScript loads without any flag. installAliases adds another workspace root at run time, and installFrom finds the root above a directory first.
The hook imports nothing but Node's own modules, so it works before the aliases it installs exist.
API Reference§
ƒ Functions
Parameters
| Name | Type | Description |
|---|---|---|
§sources | WorkspaceSources | The workspace whose aliases are read |
§specifier | string | An import specifier |
Returns
string.ts or .tsx appended, or the directory's index.ts.Parameters
Returns
stringExample
The three completions, and the cases the hook leaves to Node
const parent = '/repo/libs/features/src/index.ts'
completeRelative('./host/open', parent) // => '/repo/libs/features/src/host/open.ts'
completeRelative('./host', parent) // => '/repo/libs/features/src/host/index.ts', when host/ is a directory
completeRelative('./host/open.ts', parent) // => null, the file exists as written
completeRelative('@hyperfrontend/nexus', parent) // => null, not relativeReturns
stringmodule-typescript for workspace sources, null otherwiseParameters
| Name | Type | Description |
|---|---|---|
§directory | string | The directory of the module, absolute |
Returns
stringParameters
| Name | Type | Description |
|---|---|---|
§root | string | The workspace root, absolute |
Examples
Running a workspace's TypeScript from source
node --import @hyperfrontend/workspace/hooks tools/scripts/report.ts
```
Importing the module installs the hooks for the workspace above the working directory; call
`installAliases` only for a second workspace, which is asked after the first.Lending a script the aliases of another workspace
import { installAliases } from '@hyperfrontend/workspace/hooks'
installAliases('/repos/other-workspace')
installAliases('/repos/other-workspace') // a second call for the same root changes nothing
const { buildGraph } = await import('@other/graph') // an alias of the other workspace's tsconfig.base.jsonParameters
| Name | Type | Description |
|---|---|---|
§directory | string | Where to look from, absolute |
Example
Installing for the workspace a file belongs to
installFrom(dirname(fileURLToPath(import.meta.url))) // finds the nearest hf.json above this file
installFrom('/tmp') // does nothing: no workspace aboveReturns
boolean.ts and .tsx files inside the workspace and outside node_modulesParameters
| Name | Type | Description |
|---|---|---|
§root | string | The workspace root, absolute |
Returns
WorkspaceSourcesParameters
| Name | Type | Description |
|---|---|---|
§sources | WorkspaceSources | The workspace whose sources are resolved |
§specifier | string | The import specifier |
§parentURL | string | The importing module's URL, when there is one |
Returns
stringExample
An alias resolves from anywhere, a relative import only from workspace TypeScript
const sources = readSources('/repo')
resolveWorkspace(sources, '@hyperfrontend/nexus', undefined) // => 'file:///repo/libs/nexus/src/index.ts'
resolveWorkspace(sources, './host', 'file:///repo/libs/features/src/index.ts') // => 'file:///repo/libs/features/src/host/index.ts'
resolveWorkspace(sources, './host', 'file:///repo/node_modules/pkg/index.js') // => null
resolveWorkspace(sources, 'typescript', 'file:///repo/libs/features/src/index.ts') // => null, Node resolves itParameters
| Name | Type | Description |
|---|---|---|
§directory | string | Where to start, absolute |
Returns
string