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

index-render

v1.0.9

Published

Automatic index generation

Downloads

11

Readme

index-render


根据指定的形式自动生成目录的索引

index-render 的目的

假设有目录结构如下,自动生成 componets 的索引文件 index.js

  - componets
   + button
     - index.js
   + foo-bar
     - index.js
   + nest
     + child-nest
       - index.js
       + grandson-nest
         - index.js

自动生成 components/index.js

export { default as Button, IButtonProps } from "./button";
export { default as FooBar, IFooBarProps } from "./foo-bar";
export { default as Nest, INestProps } from "./nest";
export { default as ChildNest, IChildNestProps } from "./nest/child-nest";
export { default as GrandsonNest, IGrandsonNestProps } from "./nest/child-nest/grandson-nest";

安装

$ npm install index-render

code example

const indexCreater = require("../index");

indexCreater([
    {
        root: 'components', // 绝对路径或者现对路径,如果是相对路径会通过path.resolve转为绝对路径
        match: '**/!(*.*)', // 此处参数为glob类型
        separator: /(-|_)/g,
        exportPattern: 'export { default as [name] , I[name]Props } from \'[path]\'',
        ignore: 'button', // glob pattern 或者 glob pattern Array
        suffix: '.ts',
        callback(template, items) {
            return template + '\n// test'
        }
    },
    {
        root: 'otherComponents',
        match: '**/!(*.*)', // 此处参数为glob类型
        separator: /(-|_)/g,
        exportPattern: 'export { default as [name] } from \'[path]\'',
        suffix: '.jsx'
    },
    // exportPattern 没有填将自动解析 glob 匹配文件的 export
    {
        root: 'autoComponents',
        match: '*/index.*',
        separator: /(-|_)/g,
        suffix: '.js'
    }
])

自动解析文件export 功能

exportPattern 如果没配置自动解析 glob 匹配文件的 export

假设匹配的文件内容如下

export default class foo {}
export const fooVar = 0

那么生成的index.js将自动解析出export

export { default as Foo, fooVar } from './foo/index.js'

详见example/autoComponents/

目前不支持解析export * from "mod"的玩法

感谢iZhen添加的自动解析文件export功能

API

  • @prop {String} root 要生成索引的根目录,绝对路径或者现对路径,如果是相对路径会通过path.resolve转为绝对路径

  • @prop {String} match 子模块的匹配规则 glob pattern

  • @prop {Regex} separator 子模块目录(文件)命名分割符默认为 /(-|_)/g

  • @prop {String} exportPattern 子模块导出模板,如果不填index-creater将尝试自动解析glob匹配的文件export

  • @prop {String} suffix 生成index文件的后缀默认为 '.js'

  • @prop {String|Array} ignore glob pattern or glob pattern array

  • @prop {(template,items)=>String} callback 自定义回调函数,template 为引进生成的文件模板,items 匹配的文件信息,返回值为新的模板