UNPKG

5.19 kBJavaScriptView Raw
1'use strict';
2
3var DepGraph = require('dependency-graph').DepGraph;
4var nodeResolve = require('resolve');
5
6Object.defineProperty(exports, '__esModule', {
7 value: true
8});
9
10var _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; }; })();
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
13
14function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
15
16var _indexJs = require('css-modules-loader-core/lib/index.js');
17
18var _indexJs2 = _interopRequireDefault(_indexJs);
19
20var _fs = require('fs');
21
22var _fs2 = _interopRequireDefault(_fs);
23
24var _path = require('path');
25
26var _path2 = _interopRequireDefault(_path);
27
28// Sorts dependencies in the following way:
29// AAA comes before AA and A
30// AB comes after AA and before A
31// All Bs come after all As
32// This ensures that the files are always returned in the following order:
33// - In the order they were required, except
34// - After all their dependencies
35var traceKeySorter = function traceKeySorter(a, b) {
36 if (a.length < b.length) {
37 return a < b.substring(0, a.length) ? -1 : 1;
38 } else if (a.length > b.length) {
39 return a.substring(0, b.length) <= b ? -1 : 1;
40 } else {
41 return a < b ? -1 : 1;
42 }
43};
44
45var FileSystemLoader = (function () {
46 function FileSystemLoader(root, plugins) {
47 _classCallCheck(this, FileSystemLoader);
48
49 this.root = root;
50 this.sources = {};
51 this.importNr = 0;
52 this.core = new _indexJs2['default'](plugins);
53 this.tokensByFile = {};
54 this.deps = new DepGraph();
55 }
56
57 _createClass(FileSystemLoader, [{
58 key: 'fetch',
59 value: function fetch(_newPath, relativeTo, _trace) {
60 var _this = this;
61
62 var newPath = _newPath.replace(/^["']|["']$/g, ''),
63 trace = _trace || String.fromCharCode(this.importNr++);
64 return new Promise(function (resolve, reject) {
65 var relativeDir = _path2['default'].dirname(relativeTo),
66 rootRelativePath = _path2['default'].resolve(relativeDir, newPath),
67 rootRelativeDir = _path2['default'].join(_this.root, relativeDir),
68 fileRelativePath = _path2['default'].resolve(rootRelativeDir, newPath);
69
70 // if the path is not relative or absolute, try to resolve it in node_modules
71 if (newPath[0] !== '.' && newPath[0] !== '/') {
72 var paths;
73 if (process.env.NODE_PATH) {
74 paths = process.env.NODE_PATH.split(_path2['default'].delimiter);
75 }
76 try {
77 fileRelativePath = nodeResolve.sync(newPath, {
78 basedir: rootRelativeDir,
79 paths: paths
80 });
81 // in this case we need to actualize rootRelativePath too
82 rootRelativePath = _path2['default'].relative(_this.root, fileRelativePath);
83 } catch (e) {}
84 }
85
86 // first time? add a node
87 if (_trace === undefined) {
88 if (!_this.deps.hasNode(fileRelativePath)) {
89 _this.deps.addNode(fileRelativePath);
90 }
91 }
92 // otherwise add a dependency
93 else {
94 var parentFilePath = _path2['default'].join(_this.root, relativeTo);
95 if (!_this.deps.hasNode(parentFilePath)) {
96 console.error('NO NODE', parentFilePath, fileRelativePath)
97 }
98 if (!_this.deps.hasNode(fileRelativePath)) {
99 _this.deps.addNode(fileRelativePath);
100 }
101 _this.deps.addDependency(parentFilePath, fileRelativePath);
102 }
103
104 var tokens = _this.tokensByFile[fileRelativePath];
105 if (tokens) {
106 return resolve(tokens);
107 }
108
109 _fs2['default'].readFile(fileRelativePath, 'utf-8', function (err, source) {
110 if (err) reject(err);
111 _this.core.load(source, rootRelativePath, trace, _this.fetch.bind(_this)).then(function (_ref) {
112 var injectableSource = _ref.injectableSource;
113 var exportTokens = _ref.exportTokens;
114
115 _this.sources[fileRelativePath] = injectableSource;
116 _this.tokensByFile[fileRelativePath] = exportTokens;
117 resolve(exportTokens);
118 }, reject);
119 });
120 });
121 }
122 }, {
123 key: 'finalSource',
124 get: function () {
125 var sources = this.sources;
126 var written = {};
127
128 return this.deps.overallOrder().map(function (filename) {
129 if (written[filename] === true) {
130 return null;
131 }
132 written[filename] = true;
133
134 return sources[filename];
135 }).join('');
136 }
137 }]);
138
139 return FileSystemLoader;
140})();
141
142exports['default'] = FileSystemLoader;
143module.exports = exports['default'];