Introduction
File loading module. Provides multiple file loading and execution methods, serving as the foundational dependency for controller loading, config loading, and task execution. When JS modules/files maintained by developers need to be executed or called in business layers such as controller, service, jobs, preload, etc., there may be path or encryption issues. Using the loader can avoid these problems.
Note: The loadFile function from the loader module is exported as loaderLoadFile in the main ee-core entry point to avoid naming conflict with the loadFile function from the core module. When importing from ee-core/loader, the original name loadFile is used; when importing from ee-core (the root package), use loaderLoadFile.
Import
Both CJS and ESM:
// ESM
import { loadFile, requireFile, execFile, resolveModule, getFullpath } from 'ee-core/loader';
// CJS
const { loadFile, requireFile, execFile, resolveModule, getFullpath } = require('ee-core/loader');From the main entry (aliased):
// ESM
import { loaderLoadFile, requireFile, execFile, resolveModule, getFullpath } from 'ee-core';
// CJS
const { loaderLoadFile, requireFile, execFile, resolveModule, getFullpath } = require('ee-core');API
loadFile(filepath, ...inject)
Description: Load a file and automatically execute function exports. If the file exports a plain function (not a class or bytecode class), automatically calls that function with the injected arguments and returns the execution result. Suitable for config files that need to dynamically return content based on appInfo. Relative paths are resolved based on the electron directory (getElectronDir()).
Parameters: | Parameter | Type | Required | Description |
| filepath | string | Yes | File path (relative paths are based on electronDir) |
| inject | unknown[] | No | Arguments passed to the function export (spread into the call) |
Returns: unknown — File export content or function execution result
Example:
// Load config file with appInfo injection (relative path)
const config = loadFile('config/config.default', appInfo);
// Load with absolute path
const data = loadFile('/absolute/path/to/module.js');See also: loaderLoadFile (alias in main entry), core.loadFile (different module)
loaderLoadFile(filepath, ...inject)
Description: Alias of loadFile from the loader module, exported from the main ee-core entry point to avoid conflict with core.loadFile. Same behavior as loadFile.
Parameters: | Parameter | Type | Required | Description |
| filepath | string | Yes | File path (relative paths are based on electronDir) |
| inject | unknown[] | No | Arguments passed to the function export |
Returns: unknown — File export content or function execution result
Example:
// Import from main entry using the alias
const { loaderLoadFile } = require('ee-core');
const config = loaderLoadFile('config/config.default', appInfo);See also: loader.loadFile
requireFile(filepath)
Description: Load a file without auto-execution. Returns the module export content directly, without any processing. Suitable for scenarios where the original module reference is needed (e.g. child process task loading).
Parameters: | Parameter | Type | Required | Description |
| filepath | string | Yes | Absolute file path |
Returns: unknown — Module export content
Example:
// Load a job module without executing it
const jobModule = requireFile('/path/to/jobs/example/timer.js');execFile(filepath, ...inject)
Description: Load and run a file. Determines execution method based on export type: class/bytecode class is instantiated with new, plain function is called directly, other types are returned as-is. Injected arguments are passed to the constructor or function call.
Parameters: | Parameter | Type | Required | Description |
| filepath | string | Yes | Absolute file path |
| inject | unknown[] | No | Arguments passed to the constructor or function (spread into the call) |
Returns: unknown — Instantiated class object or function execution result
Example:
// Instantiate a class from a file
const instance = execFile('/path/to/service.js', appInfo);
// Execute a function from a file
const result = execFile('/path/to/handler.js', eventData);resolveModule(filepath)
Description: Resolve the absolute path of a module. First tries require.resolve(), then falls back to suffix-based rules: .default / .prod tries .jsc (bytecode version), .js tries .jsc, no suffix tries .js and .jsc.
Parameters: | Parameter | Type | Required | Description |
| filepath | string | Yes | Module path |
Returns: string | undefined — Absolute path of the module, or undefined if it cannot be resolved
Example:
// Resolve a config file
const configPath = resolveModule('config/config.default');
// Returns: '/project/electron/config/config.default.js' or '.jsc'
// Resolve fails gracefully
const unknown = resolveModule('nonexistent/module');
// Returns: undefinedSee also: getFullpath
getFullpath(filepath)
Description: Get the absolute path of a file under the electron directory. Relative paths are resolved based on getElectronDir(), and resolveModule is also attempted to find the actual file. Throws an error if the file does not exist.
Parameters: | Parameter | Type | Required | Description |
| filepath | string | Yes | File path (relative or absolute) |
Returns: string — Absolute path of the file
Example:
// Get absolute path of a relative file
const fullPath = getFullpath('controller/example.js');
// Returns: '/project/electron/controller/example.js'
// Get absolute path of an absolute file
const fullPath = getFullpath('/absolute/path/to/file.js');See also: resolveModule, ps.getElectronDir
