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

keep-loader

v0.7.1

Published

Generate different code based on the development and production environment, just like the macro features of other compiled programming languages

Downloads

44

Readme

keep-loader for webpack

描述

keep-loader用于在不同的打包环境下需要生成不同的代码的场景,就像C/C++中的宏特性一样。

English document:https://github.com/wendux/keep-loader/blob/master/readme-en.md

Chinese document:https://github.com/wendux/keep-loader/blob/master/readme.md

使用

  1. 安装 keep-loader

    npm install keep-loader --save-dev
  2. 修改webpack配置文件

    module: {
     ...
        rules: [
        ...
          {
            test: /\.js$/,
            use: [
              {
                loader: 'keep-loader',
                options:{
                  keep:process.env.NODE_ENV === 'production'?"prod":"dev"
                }
              },
              {loader: 'babel-loader'}
            ],
            include: [resolve('src'), resolve('test')]
          },
          ...
          ]
        ...  
     }

现在,你在代码里可以直接使用 KEEP 函数 , 例如:

var env=""
KEEP("dev",e=>{
  console.log("我将会在dev构建过程中保留,其它环境构建过程中被移除")
  env="production"
})
KEEP("prod",e=>{
  console.log("我将会在prod构建过程中输出,其它环境构建过程中被移除")
  env="production"
})
console.log(env)
//在dev环境中输出 "development" , 在prod环境中输出"production" .

KEEP(env,callback)

此函数在keep-loader中动态定义,你不必手动定义。

  • env : 要保留代码的环境,值必须和webpack配置中keep-loader的options的keep值相匹配。另外,env必须是一个字符串直接量,而不能是变量!因为,keep-loader实在构建过程中处理js源码,而不是在执行过程中。
  • callback: 要保留的源码回调; 保留的回调会就地立即执行。

示例

1.比如我们在开发环境时静态资源用我们本地的,而线上环境是直接引用cdn,那么我们可以写一个获取基地址的函数:

function getAssetBaseUrl(){
  var baseUrl="http://localhost/static"
  KEEP("prod",()=>{
    baseUrl="http://cdn.xxx.com/static"
  })
  return baseUrl;
}
  1. 比如我们要在测试环境开启日志,而在生产环境关闭日志,那么我们可以写一个log函数:
function log(){
   var arg=arguments;
   KEEP("dev",()=>{
    console.log.apply(console,arg)
  })
}

最后

欢迎Star