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

koa-assets

v0.2.6

Published

最好用的assets代理工具,请与koa-join和koa-charset等其他中间件配合使用。

Downloads

20

Readme

koa-assets

介绍

基于koa的前端开发环境中间件,最好用的前端本地调试映射工具,请与koa-joinkoa-charset等其他中间件配合使用。

安装

koa 依赖支持 generator 的 Node 环境,准确来说,是 node >= 0.11.9 的环境。

$ npm install koa-join
$ npm install koa-assets

请务必使用node --harmony模式运行程序,例如:

$ node --harmony index.js

为了方便,可以将在你的.bash_profile中将node设置为默认启动harmony模式的别名:

alias node='node --harmony'

Example

index.js

var koa = require('koa');
var join = require('koa-join');
var assets = require('koa-assets');

var app = koa();

app.use(join());    //koa-join 用来将combo请求分解成多个url
app.use(assets({
  urls: [{
    rule: /tm\/([\S]+?)\/[\d\.]+/,
    dest: '~/dev/tm/$1'
  }]
}));

app.listen(80);

hosts绑定

在使用koa-assets进行本地调试时,请将g.tbcdn.cng.assets.daily.taobao.net绑定到本地ip 127.0.0.1上。

127.0.0.1  g.tbcdn.cn
127.0.0.1. g.assets.daily.taobao.net

运行

$ node --harmony index.js #mac下监听80端口可能需要sudo权限

使用说明

文件查找

koa-assets将assets url请求捕获,并通过可配置的规则,映射到用户本地目录,方便开发和调试。以上面的Example为例,当我们访问:

http://g.tbcdn.cn/tm/detail/1.5.53/app.css

koa-assets的目录查找顺序依次是:

=> ~/dev/tm/detail/src/app.css
 => ~/dev/tm/detail/build/app.css
  => ~/dev/tm/detail/app.css
   => http://10.235.136.37/tm/detail/1.5.53/app.css

当然,只要用其中的一种方式找到了文件,就不会用其他方式继续查找。

配置

urls:匹配和映射规则

  • rule:匹配的url规则,支持正则和字符串
  • dest:映射的目标目录
使用案例
案例1:正则
[{
  rule: /tm\/detail\/[\d\.]+/,
  dest: '~/dev/tm/detail'
}]
案例2:正则
[{
  rule: /tm\/detail\/[\d\.]+/,
  dest: function($){
    console.log($);
    return '~/dev/tm/detail';
  }
}]
案例3:字符串
[{
  rule: 'tm/detail/1.5.53',
  dest: '~/dev/tm/detail'
}]

hosts:线上服务器ip

本地不存在的文件,会到通过请求线上服务器获取,例如koa-assets的默认配置:

{
  'g.tbcdn.cn': '10.235.136.37',
  'g.assets.daily.taobao.net': '10.235.136.37'
}

可以将g.tbcdn.cn指向线上服务器ip地址,例如:

{
  'g.tbcdn.cn': '115.238.23.250',
}

编码设定

输入编码

读取映射目录文件的文件编码,默认以utf-8方式读入。当目录文件编码不为utf-8时,可以通过设置inputCharset字段来让koa-assets进行文件读取时的自动转码,例如:

[{
  rule: 'tm/detail/1.5.53',
  dest: '~/dev/tm/detail',
  inputCharset: 'gbk'
}]
输出编码

也叫响应编码。koa-join和koa-assets响应的默认编码是utf-8,如需定制具体某些请求的响应编码可通过koa-charset中间件进行编码设定和字符编码转换,例如:

//用在koa-join和koa-assets之前
app.use(function *(next){
  yield next;
  //对指定url的请求设置成gbk编码
  if(this.url.indexOf('tm/buy') !== -1){
  	this.charset = 'gbk';
  }
});
app.use(join());
app.use(assets());
//...

License

MIT