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

simple-asset-loader

v1.1.0

Published

simple asset loader

Downloads

2

Readme

Simple-asset-loader

用于简化异步加载编写,通过

require('simple-asset-loader?async=true![.js|.json]')(cb,errCb);

可以自动生成如下代码

module.exports=function(cb,errCb){
  	require.ensure([],function(require){
      	var m=require('path-to-js');
        cb(m);
    },function(err){
      errCb(err);
    });
}

当没有给simple-asset-loader传入任何参数时,等价于直接require一个模块,require返回的并不是module.exports=function(cb,errCb){}这样的函数,而是直接返回被引用模块导出的对象。

Params

  1. chunkName 会让当前资源变为异步加载并会在require.ensure 最后一个参数带上chunkName使这个异步加载的chunk变成一个命名chunk。

  2. async=true会让当前资源加载变为异步加载。

  3. rule=[rule]自定义规则,这个参数需要配合配置一起使用:

    {
      ...some webpack config
      loader:{
        simpleAssetConfig{
          'your-rule':function(context,options){
            	// context 是代表当前loader的this对象
            	// options 表示传到loader上的url参数
            	// 当前规则需要用字符串拼接上 cb 和 errCb(可选) 这个用于加载完成的回调和失败回调
            	 return `require.ensure([],function(require){
    				var m = require(${JSON.stringify(context.resource)})
    				typeof cb === ${JSON.stringify('function')} && cb(m);
    			  },function(err){
    				typeof errCb === ${JSON.stringify('function')} && errCb(err);
    			  },${JSON.stringify(query.chunkName)});`
          }
        }
      }
    }
    • 调用loader示例

      require('simple-asset-loader?rule=your-rule')(mod=>{
        //
      },err=>{
        //加载失败
      });

    4.assetMap=true 此时需要引用的资源需要是一个json文件,格式为:

    {
        "asyncA":"../async/asyncA.js",
        "asyncB":{
            "name":"../async/asyncB.js",
            "rule":"[rule defined in simpleAssetConfig]"
        },
        "asyncC":{
            "name":"../async/asyncC.js",
            "async":false
        }
    }

    调用var assetRoute=require('simple-asset-loader?assetMap=true!assetMap.json');回返回一个路由函数,通过调用assetRoute(chunkName,cb,errCb)可以在cb回调内拿到对应的加载到的chunk 返回的对象,与异步加载类似。默认情况下assetMap内的模块都是使用异步加载的,如果map的值是一个对象,对像内容如下:

    • name 模块的路径
    • [async] 可选,表示是否对当前模块加载使用异步加载方式,当async=false意味着使用普通的require加载模块,require 返回的对象依然是通过cb回调返回。
    • [rule] 可选,表示使用simpleAssetConfig内的模块加载规则,如果rule的值为空或者其他可以被认为是false的值,那么在加载这个模块时不会使用任何规则。