UNPKG

2.96 kBJavaScriptView Raw
1'use strict';
2
3var extname = require('path').extname;
4var readFile = require('fs').readFileSync;
5var exists = require('fs').existsSync;
6var urlparse = require('url').parse;
7var File = require('vinyl');
8var debug = require('debug')('serve-spm:parse');
9var rules = require('./rules');
10var stat = require('fs').statSync;
11
12/*
13 Match local file from request url
14
15 url: request url
16 opt
17 - pkg: a father package
18 - rules: custom rules
19*/
20
21module.exports = function parse(url, opt) {
22 debug('find file of url %s in %s', url, opt.rootPkg.dest);
23
24 var myRules = Array.isArray(opt.rules) ? opt.rules.concat(rules) : rules;
25 url = urlparse(url);
26
27 /*
28 return a gulp File(vinyl) with some custom properties
29
30 - wrap: whether code should be wrapped by define
31 - url: request url Object parsed by `url`
32 - pkg: a father package
33 */
34 var file = findFile(url, opt.pkg, opt.rootPkg, myRules);
35 if (!file) return;
36
37 // wrap define by default, except for
38 // 1. has ?nowrap in url
39 // 2. specified in rules
40 if (hasNoWrap(url)) {
41 file.wrap = false;
42 } else if (typeof file.wrap !== 'boolean') {
43 file.wrap = true;
44 }
45
46 file.url = url;
47 file.pkg = opt.pkg;
48 return file;
49};
50
51function hasNoWrap(url) {
52 var search = url.search;
53 return search && search.indexOf('nowrap') > -1;
54}
55
56function findFile(url, pkg, rootPkg, rules) {
57 for (var i in rules) {
58 debug('find file from rule[%s]', i);
59 var rule = rules[i];
60 var ret = rule(url, pkg, rootPkg) || {};
61 debug('rule result path: %s, wrap: %s', ret.path, ret.wrap);
62
63 // ignore when no result or result is invalid
64 if (!ret.path) {
65 continue;
66 }
67
68 // check filepath exists
69 var filepath = testFile(ret.path);
70
71 // check directory
72 if (filepath && stat(filepath).isDirectory()) {
73 return;
74 }
75
76 if (filepath) {
77 debug('matched %s -> %s on rule[%s]', url.pathname, filepath, i);
78 var file = new File({
79 base: pkg.dest,
80 path: filepath,
81 contents: readFile(filepath)
82 });
83 file.wrap = ret.wrap;
84 return file;
85 }
86 }
87}
88
89function testFile(filepath) {
90 var file;
91
92 // a.css.js$ -> a.css
93 // a.tpl.js$ -> a.tpl
94 // ...
95 if (/\.[a-z]+\.js$/.test(filepath)) {
96 file = filepath.slice(0, -3);
97 if (exists(file)) return file;
98 }
99
100 // a.js$ -> a.coffee
101 if (extname(filepath) === '.js') {
102 file = filepath.replace(/\.js$/, '.coffee');
103 if (exists(file)) return file;
104 }
105
106 // a.css$ -> a.less
107 // a.css$ -> a.scss, a.sass
108 // a.css$ -> a.styl
109 if (extname(filepath) === '.css') {
110 file = filepath.replace(/\.css$/, '.less');
111 if (exists(file)) return file;
112
113 // file = filepath.replace(/\.css$/, '.scss');
114 // if (exists(file)) return file;
115
116 // file = filepath.replace(/\.css$/, '.sass');
117 // if (exists(file)) return file;
118
119 // file = filepath.replace(/\.css$/, '.styl');
120 // if (exists(file)) return file;
121 }
122
123 if (exists(filepath)) return filepath;
124}
125