serve-spm
Version:
SPM middleware for debug.
95 lines (80 loc) • 2.35 kB
JavaScript
;
var debug = require('debug')('serve-spm:rules');
var join = require('path').join;
var relative = require('path').relative;
var util = require('./util');
/*
Rules list
every rule should return an object containing `path` if need matching
- url: request url Object parsed by `url`
- pkg: a father package
*/
module.exports = [
// /$ -> index.html
function(url, pkg) {
if (/\/$/.test(url.pathname)) {
debug('matching %s -> %s', url.pathname, 'index.html');
return {
path: join(pkg.dest, url.pathname, 'index.html')
};
}
},
// /$ -> index.htm
function(url, pkg) {
if (/\/$/.test(url.pathname)) {
debug('matching %s -> %s', url.pathname, 'index.htm');
return {
path: join(pkg.dest, url.pathname, 'index.htm')
};
}
},
// ^/a.js -> a.js
function(url, pkg) {
return {
path: join(pkg.dest, url.pathname)
};
},
// ^/dist/{name}/{version}/a.js -> /a.js
function(url, pkg) {
var pathname = url.pathname;
var prefix = '/dist/' + pkg.name + '/' + pkg.version;
if (pathname.indexOf(prefix) === 0) {
pathname = pathname.replace(prefix, '');
debug('matching %s -> %s', url.pathname, pathname);
return {
path: join(pkg.dest, pathname)
};
}
},
// ^/{name}/{version}/a.js -> /dist/{name}/{version}/a.js
function(url, pkg, rootPkg) {
var pathname = join('dist', url.pathname);
debug('matching %s -> %s', url.pathname, pathname);
return {
wrap: false,
path: join(rootPkg.dest, pathname)
};
},
// ^/{name}/{version}/a.js -> /a.js
function(url, pkg) {
var matched = util.matchNameVersion(url.pathname);
if (matched && matched.name === pkg.name && matched.version === pkg.version) {
debug('matching %s -> %s', url.pathname, matched.file);
return {
path: join(pkg.dest, matched.file)
};
}
},
// ^/{name}/{version}/a.js -> /spm_modules/{name}/{version}/a.js
function(url, pkg) {
var matched = util.matchNameVersion(url.pathname);
if (!matched) return;
var sub = pkg.getPackage(matched.name + '@' + matched.version);
if (sub) {
debug('matching %s -> %s', join(relative(pkg.dest ,sub.dest), matched.file));
return {
path: join(sub.dest, matched.file)
};
}
}
];