-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
178 lines (148 loc) · 3.56 KB
/
index.js
File metadata and controls
178 lines (148 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Module Dependencies
*/
var debug = require('debug')('duo-css-compat');
var Package = require('duo-package');
var fmt = require('util').format;
var join = require('path').join;
var main = require('duo-main');
var fs = require('co-fs');
var keys = Object.keys;
/**
* Export `plugin`
*/
module.exports = plugin;
/**
* Add "styles" compatibility
*
* @param {Object} opts
* @return {Function}
* @api public
*/
function plugin() {
var visited = {};
return function *compatibility(file, entry) {
if (file.type !== 'css' || entry.type !== 'css') return;
var duo = file.duo;
var token = duo.token();
var cache = yield duo.getCache();
var manifest = join(file.root, 'component.json');
// only visit a manifest once
if (visited[manifest]) return;
visited[manifest] = true;
var obj = json(manifest);
var styles = obj.styles;
var deps = obj.dependencies || {};
// only trigger for "old" components
// that have a "styles" attribute in
// their component.json
if (!styles || !styles.length) return;
// This will get picked up already in duo
if (styles.length === 1 && !keys(deps)) return;
// styles: [ ... ]
var entrypoint = main(obj, 'css');
var i = styles.indexOf(entrypoint);
if (~i) styles.splice(i, 1);
// Add the additional styles in, if there are any
styles.forEach(function (style) {
debug('%s: adding "%s" style', file.id, style);
file.src = fmt('%s\n@import "/%s";', file.src, style);
});
// No need to continue if we don't have
// any additional dependencies
if (!keys(deps)) return;
var dir = join(entry.root, 'components');
var pkgs = [];
// dependencies: { ... }
// create packages for each dependency
for (var pkg in deps) {
pkgs[pkgs.length] = new Package(pkg, deps[pkg])
.cache(cache)
.token(token)
.directory(dir);
}
// fetch the packages
yield fetch(file.id, pkgs);
// filter out non-CSS packages
var paths = yield filter(pkgs);
// Add imports
paths.forEach(function (path) {
debug('%s: adding "%s" dependency', file.id, path);
file.src = fmt('@import "%s";\n%s', path, file.src);
});
};
}
/**
* Fetch the package
*
* @param {String} rel
* @param {Array} pkgs
* @return {Array}
* @api private
*/
function fetch(rel, pkgs) {
return pkgs.map(function(pkg) {
debug('%s: fetching "%s"', rel, pkg.slug());
return pkg.fetch();
});
}
/**
* Filter out non-css packages
*
* @param {Package} pkg
* @return {String}
* @api private
*/
function *filter(pkgs) {
var slugs = {};
// get the css path
var paths = pkgs
.map(function(pkg) {
var obj = json(pkg.path('component.json'));
var entry = main(obj, 'css') || 'index.css';
var path = pkg.path(main(obj, 'css') || 'index.css');
slugs[path] = fmt('%s:%s', pkg.slug(), entry);
return stat(path);
});
// check existence in parallel
paths = yield paths;
// filter out non-css packages
// and remap to slug:entry
return paths
.filter(function(path) {
return path;
})
.map(function(path) {
path = slugs[path];
return path;
});
}
/**
* Load JSON
*
* @param {String} path
* @return {Object}
* @api private
*/
function json(path) {
try {
return JSON.parse(JSON.stringify(require(path)));
} catch (e) {
return {};
}
}
/**
* Stat
*
* @param {String} path
* @return {Boolean}
* @api private
*/
function *stat(path) {
try {
yield fs.stat(path);
return path;
} catch (e) {
return false;
}
}