介绍
主要用于 NodeJS 环境,ES 模块的适配,例如: __dirname 通过适配的 getImportDirName 获取
项目源码
- 自建 gitlab 地址: https://gitlab.qiushaocloud.top/qiushaocloud/npmjs-dts-types-projects
- github 地址: https://github.com/qiushaocloud/npmjs-dts-types-projects
npm 包
- 安装 npm 包:
npm install --save-dev @qiushaocloud/qesm-nodejs-adapter
插件 TS 定义
declare global {
const loadImportJSON: <T=any>(filepath: string, importMetaUrl: string) => Record<string, T>;
const getImportFileName: (importMetaUrl: string) => string;
const getImportDirName: (importMetaUrl: string) => string;
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace NodeJS {
interface Global {
loadImportJSON: <T=any>(filepath: string, importMetaUrl: string) => Record<string, T>;
getImportFileName: (importMetaUrl: string) => string;
getImportDirName: (importMetaUrl: string) => string;
}
}
}
declare module '@qiushaocloud/qesm-nodejs-adapter' {
export function loadImportJSON <T=any>(filepath: string, importMetaUrl: string): Record<string, T>;
export function getImportFileName (importMetaUrl: string): string;
export function getImportDirName (importMetaUrl: string): string;
}
export {};
插件使用示例
// 使用方式
import '@qiushaocloud/qesm-nodejs-adapter'; // 项目入口处导入即可,然后其它模块就能直接使用
const __dirname = getImportDirName(import.meta.url);
const __filename = getImportFileName(import.meta.url);
const jsonFileContent = loadImportJSON('./config.json', import.meta.url);
console.log('__dirname:', __dirname);
console.log('__filename:', __filename);
console.log('jsonFileContent:', jsonFileContent);