@qiushaocloud/qesm-nodejs-adapter
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

介绍

主要用于 NodeJS 环境,ES 模块的适配,例如: __dirname 通过适配的 getImportDirName 获取

项目源码

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);

开源不易,如果对您有帮助,请您动一动您的小手,给作者点 Star,也请您多多关注分享者「邱少羽梦

Package Sidebar

Install

npm i @qiushaocloud/qesm-nodejs-adapter

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

5.15 kB

Total Files

4

Last publish

Collaborators

  • qiushaocloud