-
Notifications
You must be signed in to change notification settings - Fork 47
/
file-system-loader.js
138 lines (113 loc) · 4.86 KB
/
file-system-loader.js
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
'use strict';
var DepGraph = require('dependency-graph').DepGraph;
var nodeResolve = require('resolve');
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _indexJs = require('css-modules-loader-core/lib/index.js');
var _indexJs2 = _interopRequireDefault(_indexJs);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
// Sorts dependencies in the following way:
// AAA comes before AA and A
// AB comes after AA and before A
// All Bs come after all As
// This ensures that the files are always returned in the following order:
// - In the order they were required, except
// - After all their dependencies
var traceKeySorter = function traceKeySorter(a, b) {
if (a.length < b.length) {
return a < b.substring(0, a.length) ? -1 : 1;
} else if (a.length > b.length) {
return a.substring(0, b.length) <= b ? -1 : 1;
} else {
return a < b ? -1 : 1;
}
};
var FileSystemLoader = (function () {
function FileSystemLoader(root, plugins) {
_classCallCheck(this, FileSystemLoader);
this.root = root;
this.sources = {};
this.traces = {};
this.importNr = 0;
this.core = new _indexJs2['default'](plugins);
this.tokensByFile = {};
this.deps = new DepGraph();
}
_createClass(FileSystemLoader, [{
key: 'fetch',
value: function fetch(_newPath, relativeTo, _trace) {
var _this = this;
var newPath = _newPath.replace(/^["']|["']$/g, ''),
trace = _trace || String.fromCharCode(this.importNr++);
return new Promise(function (resolve, reject) {
var relativeDir = _path2['default'].dirname(relativeTo),
rootRelativePath = _path2['default'].resolve(relativeDir, newPath),
rootRelativeDir = _path2['default'].join(_this.root, relativeDir),
fileRelativePath = _path2['default'].resolve(rootRelativeDir, newPath);
// if the path is not relative or absolute, try to resolve it in node_modules
if (newPath[0] !== '.' && newPath[0] !== '/') {
try {
fileRelativePath = nodeResolve.sync(newPath, { basedir: rootRelativeDir });
} catch (e) {}
}
// first time? add a node
if (_trace === undefined) {
if (!_this.deps.hasNode(fileRelativePath)) {
_this.deps.addNode(fileRelativePath);
}
}
// otherwise add a dependency
else {
var parentFilePath = _path2['default'].join(_this.root, relativeTo);
if (!_this.deps.hasNode(parentFilePath)) {
console.error('NO NODE', parentFilePath, fileRelativePath)
}
if (!_this.deps.hasNode(fileRelativePath)) {
_this.deps.addNode(fileRelativePath);
}
_this.deps.addDependency(parentFilePath, fileRelativePath);
}
var tokens = _this.tokensByFile[fileRelativePath];
if (tokens) {
return resolve(tokens);
}
_fs2['default'].readFile(fileRelativePath, 'utf-8', function (err, source) {
if (err) reject(err);
_this.core.load(source, rootRelativePath, trace, _this.fetch.bind(_this)).then(function (_ref) {
var injectableSource = _ref.injectableSource;
var exportTokens = _ref.exportTokens;
_this.sources[fileRelativePath] = injectableSource;
_this.traces[trace] = fileRelativePath;
_this.tokensByFile[fileRelativePath] = exportTokens;
resolve(exportTokens);
}, reject);
});
});
}
}, {
key: 'finalSource',
get: function () {
var traces = this.traces;
var sources = this.sources;
var written = {};
return Object.keys(traces).sort(traceKeySorter).map(function (key) {
var filename = traces[key];
if (written[filename] === true) {
return null;
}
written[filename] = true;
return sources[filename];
}).join('');
}
}]);
return FileSystemLoader;
})();
exports['default'] = FileSystemLoader;
module.exports = exports['default'];