https://github.com/truemogician/multilang-module-loader
A utility library that helps loading modules dynamically in multiple languages, including JSON, YAML, JavaScript and TypeScript.
https://github.com/truemogician/multilang-module-loader
Last synced: 3 months ago
JSON representation
A utility library that helps loading modules dynamically in multiple languages, including JSON, YAML, JavaScript and TypeScript.
- Host: GitHub
- URL: https://github.com/truemogician/multilang-module-loader
- Owner: truemogician
- License: mit
- Created: 2023-09-07T18:07:01.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-07T18:34:27.000Z (over 1 year ago)
- Last Synced: 2025-02-23T07:46:42.903Z (3 months ago)
- Language: TypeScript
- Homepage:
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Multilang Module Loader
### Description
This is a utility library that helps to dynamically load modules in Node.js and works fine with TypeScript.
This library is primarily designed for configuration files, as it's a common practice to write configuration files in JavaScript or TypeScript.### Supported Formats
| Format | Extension |
| :----------: | :---------------------------: |
| JSON | `.json` |
| JSON5 | `.json5`, `.jsonc` |
| YAML | `.yaml`, `.yml` |
| JavaScript | `.js`, `.cjs`, `.mjs`, `.jsx` |
| CoffeeScript | `.coffee`, `.litcoffee` |
| TypeScript | `.ts`, `.cts`, `.mts`, `.tsx` |For more detailed information, please refer to [interpret](https://github.com/gulpjs/interpret). This library uses `interpret` and `rechoir` to load script modules.
### Example
```ts
import loadModule from "multilang-module-loader";// Construct the path to the module dynamically, e.g. by reading config files, scanning directories, making network requests, etc.
const modulePath = ...;
// Define the module type or import from somewhere. If not specified, the module will be loaded as `any`.
type ModuleType = ...;// Load the module.
loadModule(modulePath).then(module => {
// Use the module.
...
});
```### Notes
If you're running a TypeScript source file using this library with `ts-node`, remember to apply appropriate `compilerOptions` to `ts-node`, e.g. as a common practice, it's likely that you import this library using `import` syntax. It's a ESM feature, so you need to specify this file as a ESM module or pass a TSConfig file with `esModuleInterop` enabled to `ts-node`.