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

loader-core

v0.1.0

Published

A minimum loader for JS in browsers

Downloads

9

Readme

loader-core

最简JS加载器。

特点:

  • 以插入script标签的形式加载外链JS。
  • 可扩展。

相关

浏览器使用方法

将dist目录下的loader.jsloader.min.js嵌入页面后, 可以页面中使用window.Loader

Loader.load('https://s0.meituan.net/bs/jsm?f=require(@mtfe/zepto):dist/[email protected]').then(function () {
  // `window.Zepto` is available now
})

功能扩展

指定resolve方法

将多个请求合并成一个。

var loader = new Loader({
  resolve: function (requests) {
    var prefix = 'https://s0.meituan.net/bs/jsm?f='
    var sources = {
      fastclick: 'require(fastclick):lib/[email protected]',
      zepto: 'require(@mtfe/zepto):dist/[email protected]',
    }
    return prefix + [].concat(requests).map(function (request) {
      return sources[request]
    }).join(';')
  }
})

loader.load('fastclick').then(function () {
  // `window.FastClick` and `window.Zepto` are both ready
})

loader.load('zepto').then(function () {
  // `window.FastClick` and `window.Zepto` are both ready
})

指定loader

加载CSS。

function loadCSS(src) {
  return new Promise(function (resolve, reject) {
    var node = document.createElement('link')
    node.setAttribute('type', 'text/css')
    node.setAttribute('rel', 'stylesheet')
    node.setAttribute('href', src)
    node.onload = function () {
      node.onerror = node.onload = null
      resolve()
    }
    node.onerror = function (err) {
      node.onerror = node.onload = null
      reject(err)
    }
    var ref = document.getElementsByTagName('script')[0]
    ref.parentNode.insertBefore(node, ref)
  })
}

var loader = new Loader({ loader: loadCSS })

loader.load('https://s0.meituan.net/bs/cssm/?f=fewww:/www/css/common.css,/www/css/base.css').then(function () {
  // styles are ready now
})

API

Loader.load(requests, type)

使用给定类型的默认加载器(loader)加载指定资源。

requests

Type: String, Array

type

Type: String

Default: 'js'

Loader.register(type, constructor)

注册给定的加载器,其后便可以通过Loader.load(reqs, type)来使用该加载器加载资源。

type

Type: String

constructor

Type: Function

下面注册一个CSS加载器:

Loader.register('css', function (opts) {
  var loader = new Loader(opts)
  loader.loader = loadCSS
  return loader
})

function loadCSS(src) {
  return new Promise(function (resolve, reject) {
    var node = document.createElement('link')
    node.setAttribute('type', 'text/css')
    node.setAttribute('rel', 'stylesheet')
    node.setAttribute('href', src)
    node.onload = function () {
      node.onerror = node.onload = null
      resolve()
    }
    node.onerror = function (err) {
      node.onerror = node.onload = null
      reject(err)
    }
    var ref = document.getElementsByTagName('script')[0]
    ref.parentNode.insertBefore(node, ref)
  })
}

于是可以在浏览器中使用:

Loader.load('https://s0.meituan.net/bs/cssm/?f=fewww:/www/css/common.css,/www/css/base.css', 'css')

Loader.create(type, opts)

创建一个新的给定类型的加载器。

var loader = Loader.create('css')

loader.resolve = function (requests) {
  var prefix = 'https://s0.meituan.net/bs/cssm?f=fewww:'
  var sources = {
    common: '/www/css/common.css',
    base: '/www/css/base.css',
  }
  return prefix + [].concat(requests).map(function (request) {
    return sources[request]
  }).join(';')
}

loader.load('common').then(function () {
  // common.css and base.css are downloaded together
})

loader.load('base').then(function () {
  // common.css and base.css are downloaded together
})

// OR
/*
loader.load(['common', 'base']).then(function () {
  // common.css and base.css are downloaded together
})
*/

如何开发新的加载器

index.js:

var Loader = require('loader-core')

Loader.register('awesomeLoader', awesomeLoaderConstructor)

module.exports = Loader

使用browserify将其打包成浏览器端可用的包,并用uglify进行代码压缩。

一个加载器需要resolveloader两个函数。 resolve负责将资源代号解析成绝对路径, loader使用该绝对路径加载资源。

所以,尽量将一些通用的resolveloader函数发成独立的包, 这样在开发加载器时便可以共用这些包。 或者,在开发加载器时,将这两部分置于独立模块中,可单独require。 如此,在用browserify打包时,便可以减少无用的代码。

譬如前面实现combo功能的resolve方法, 以及加载CSS的loader方法都是可以独立出来的。

TODO

  • 创建开发新加载器的脚手架