UNPKG

2.35 kBJavaScriptView Raw
1'use strict';
2
3var debug = require('debug')('serve-spm:rules');
4var join = require('path').join;
5var relative = require('path').relative;
6var util = require('./util');
7
8/*
9 Rules list
10
11 every rule should return an object containing `path` if need matching
12
13 - url: request url Object parsed by `url`
14 - pkg: a father package
15*/
16
17module.exports = [
18
19 // /$ -> index.html
20 function(url, pkg) {
21 if (/\/$/.test(url.pathname)) {
22 debug('matching %s -> %s', url.pathname, 'index.html');
23 return {
24 path: join(pkg.dest, url.pathname, 'index.html')
25 };
26 }
27 },
28
29 // /$ -> index.htm
30 function(url, pkg) {
31 if (/\/$/.test(url.pathname)) {
32 debug('matching %s -> %s', url.pathname, 'index.htm');
33 return {
34 path: join(pkg.dest, url.pathname, 'index.htm')
35 };
36 }
37 },
38
39
40 // ^/a.js -> a.js
41 function(url, pkg) {
42 return {
43 path: join(pkg.dest, url.pathname)
44 };
45 },
46
47 // ^/dist/{name}/{version}/a.js -> /a.js
48 function(url, pkg) {
49 var pathname = url.pathname;
50 var prefix = '/dist/' + pkg.name + '/' + pkg.version;
51 if (pathname.indexOf(prefix) === 0) {
52 pathname = pathname.replace(prefix, '');
53 debug('matching %s -> %s', url.pathname, pathname);
54 return {
55 path: join(pkg.dest, pathname)
56 };
57 }
58 },
59
60 // ^/{name}/{version}/a.js -> /dist/{name}/{version}/a.js
61 function(url, pkg, rootPkg) {
62 var pathname = join('dist', url.pathname);
63 debug('matching %s -> %s', url.pathname, pathname);
64 return {
65 wrap: false,
66 path: join(rootPkg.dest, pathname)
67 };
68 },
69
70 // ^/{name}/{version}/a.js -> /a.js
71 function(url, pkg) {
72 var matched = util.matchNameVersion(url.pathname);
73 if (matched && matched.name === pkg.name && matched.version === pkg.version) {
74 debug('matching %s -> %s', url.pathname, matched.file);
75 return {
76 path: join(pkg.dest, matched.file)
77 };
78 }
79 },
80
81 // ^/{name}/{version}/a.js -> /spm_modules/{name}/{version}/a.js
82 function(url, pkg) {
83 var matched = util.matchNameVersion(url.pathname);
84 if (!matched) return;
85
86 var sub = pkg.getPackage(matched.name + '@' + matched.version);
87 if (sub) {
88 debug('matching %s -> %s', join(relative(pkg.dest ,sub.dest), matched.file));
89 return {
90 path: join(sub.dest, matched.file)
91 };
92 }
93 }
94];