npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

api-auto-import

v0.0.43

Published

> api 自动导入

Downloads

4

Readme

api-auto-import

api 自动导入

Project setup

npm install api-auto-import

使用方法

// 默认配置
apiAuto({
	outFile: 'index.ts',//输出文件名称
	resolveAliasName: 'src/api',//需要导入的目录
	apiName: '$apis',//全局的模块名称
	hotUpdate: true,//是否采用文件热更新
	suffix: false,//导入是否携带后缀名
	constApiData: '$apiData',//变量名称
	isDefault: false,//导出的对象中是否有install方法
	is_ts: true,//是否生成ts类型文件
	isWindow: true,//是否挂载在Window对象
	is_export: false,//是否导出每个模块
	files: [
		/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
	],//需要导出文件类型
});
vite
// vite.config.ts
import { defineConfig } from 'vite';
import apiAuto from 'api-auto-import/vite';
import { resolve } from 'path';
export default defineConfig({
	resolve: {
		alias: {
			'@': resolve(__dirname, 'src'),
		},
	},
	plugins: [
		apiAuto({
			resolveAliasName: '@/api',
			outFile: 'index.ts',
		}),
	],
});

webpack

// vue.config.js
const { defineConfig } = require('@vue/cli-service');
const apiAuto = require('api-auto-import/webpack');
module.exports = defineConfig({
	transpileDependencies: true,
	configureWebpack: {
		plugins: [
			new apiAuto({
				resolveAliasName: '@/api',
				outFile: 'index.ts',
			}),
		],
	},
});
// webpack.config.js
// Generated using webpack-cli https://github.com/webpack/webpack-cli
const path = require('path');
const apiAuto = require('api-auto-import/webpack');
module.exports = {
	plugins: [
		new apiAuto({
			resolveAliasName: '@/api',
			outFile: 'index.ts',
		}),
	],
	resolve: {
		alias: {
			'@': path.resolve(__dirname, 'src'),
		},
	},
};

注意

在编辑器中使用 ES6 import 语法时,应该能够正常显示鼠标悬停提示和类型信息 使用 tsconfig.json 配置好 paths

// tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "@/*": ["./src/*"]
    },
  }
}

webpack.config.js 或 vue.config.js 中热更新可能无法生效或出现无限循环,请使用 hotUpdate 入参关闭热更新

new apiAuto({
	resolveAliasName: '@/api',
	outFile: 'index.ts',
	hotUpdate: false,
});
生成的默认样例
/*eslint-disable*/

// @ts-ignore 
import {App} from "vue";

// @ts-ignore
import v1_index from '@/api/v1/index'
// @ts-ignore
import v1 from '@/api/v1'
// @ts-ignore
import v2_a from '@/api/v2/a'
// @ts-ignore
import v2 from '@/api/v2'
// @ts-ignore
import v3_w from '@/api/v3/w'
// @ts-ignore
import v3 from '@/api/v3'


export const $apiData = {
    v1: {
        index: v1_index,
        ...v1
    },
    v2: {
        a: v2_a,
        ...v2
    },
    v3: {
        w: v3_w,
        ...v3
    }
};


declare global {
    const $apis:typeof $apiData;
    
    interface Window {
        $apis : typeof $apiData;
    }
    
}



export default {
    install(app:App<Element>){
        app.config.globalProperties.$apis = $apiData;
        
        window.$apis = $apiData;
        
    },
}