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

babel-plugin-code-inject

v1.4.0

Published

A Babel plugin that implements automatic injection of functions/code segments based on comments

Downloads

19

Readme

babel-plugin-code-inject

这是一个 babel 插件,根据注释自动插入相应的函数调用, 并通过注释控制所插入的具体位置,同时会根据配置自动引入相关的依赖包。此外还支持代码片段的插入。

Install

npm i babel-plugin-code-inject -D

Usage

configuration

示例:

module.exports = {
  plugins: [
    ['code-inject', {
      log: {
        // named | default | namespaced
        kind: 'named',
        require: 'log4js'
      },
      codeTemplate: "alert('test')"
    }]
  ]
}

methods

@inject:xxx

以注释形式声明在函数上方, xxx取自插件参数配置的 key, 表示改函数需要插入 xxx, 默认是函数调用, 如果配置提供的是字符串则认为是代码片段

@code:xxx

以注释形式声明在函数内部,需和@inject:xxx 配套使用, 就像 async await,表示 xxx 插入位置为此处, 若未手动声明则为默认插入至函数内部第一行。

// @inject:log
function foo() {
  const c = 1
  //@code:log
  return 1
}

example

处理前的代码

// @inject:noRequire
const fn = () => true

// @inject:log
function foo() {
  const c = 1
  //@code:log
  return 1
}

// @inject:codeTemplate
function foo2() {
  const c = 2
  //@code:codeTemplate
  return 1
}

处理后的代码

import { log } from 'log4js'

const fn = () => {
  log()
  return true
}

function foo() {
  const c = 1
  log()
  return 1
}

function foo() {
  const c = 2
  alert('test')
  return 1
}

options

key

用于识 @inject:xxx 声明, 以及非 代码片段 情况下的函数名

String

当值的类型为 string 时, 如下方的 anyKey, 表示使用的是代码段模式

module.exports = {
  plugins: [
    ['code-inject', {
      anyKey: "console.log('code segment')"
    }]
  ]
}

Object

当值的类型是对象, 则表示是函数调用

kind

kind 表示导入形式。 有三种导入方式 named 、 default、 namespaced, 此设计参考 babel-helper-module-imports

  • named 对应 import { a } from "b" 形式
  • default 对应 import a from "b" 形式
  • namespaced 对应 import * as a from "b" 形式

require

可选

require 为依赖的包名, 若不填写则表示全局方法, 则不会再进行 import