1 | 'use strict';
|
2 |
|
3 | var DepGraph = require('dependency-graph').DepGraph;
|
4 |
|
5 | Object.defineProperty(exports, '__esModule', {
|
6 | value: true
|
7 | });
|
8 |
|
9 | 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; }; })();
|
10 |
|
11 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
12 |
|
13 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
14 |
|
15 | var _indexJs = require('css-modules-loader-core/lib/index.js');
|
16 |
|
17 | var _indexJs2 = _interopRequireDefault(_indexJs);
|
18 |
|
19 | var _fs = require('fs');
|
20 |
|
21 | var _fs2 = _interopRequireDefault(_fs);
|
22 |
|
23 | var _path = require('path');
|
24 |
|
25 | var _path2 = _interopRequireDefault(_path);
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | var traceKeySorter = function traceKeySorter(a, b) {
|
35 | if (a.length < b.length) {
|
36 | return a < b.substring(0, a.length) ? -1 : 1;
|
37 | } else if (a.length > b.length) {
|
38 | return a.substring(0, b.length) <= b ? -1 : 1;
|
39 | } else {
|
40 | return a < b ? -1 : 1;
|
41 | }
|
42 | };
|
43 |
|
44 | var FileSystemLoader = (function () {
|
45 | function FileSystemLoader(root, plugins) {
|
46 | _classCallCheck(this, FileSystemLoader);
|
47 |
|
48 | this.root = root;
|
49 | this.sources = {};
|
50 | this.traces = {};
|
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 | fileRelativePath = _path2['default'].resolve(_path2['default'].join(_this.root, relativeDir), newPath);
|
68 |
|
69 |
|
70 | if (newPath[0] !== '.' && newPath[0] !== '/') {
|
71 | try {
|
72 | fileRelativePath = require.resolve(newPath);
|
73 | } catch (e) {}
|
74 | }
|
75 |
|
76 |
|
77 | if (_trace === undefined) {
|
78 | if (!_this.deps.hasNode(fileRelativePath)) {
|
79 | _this.deps.addNode(fileRelativePath);
|
80 | }
|
81 | }
|
82 |
|
83 | else {
|
84 | var parentFilePath = _path2['default'].join(_this.root, relativeTo);
|
85 | if (!_this.deps.hasNode(parentFilePath)) {
|
86 | console.error('NO NODE', parentFilePath, fileRelativePath)
|
87 | }
|
88 | if (!_this.deps.hasNode(fileRelativePath)) {
|
89 | _this.deps.addNode(fileRelativePath);
|
90 | }
|
91 | _this.deps.addDependency(parentFilePath, fileRelativePath);
|
92 | }
|
93 |
|
94 | var tokens = _this.tokensByFile[fileRelativePath];
|
95 | if (tokens) {
|
96 | return resolve(tokens);
|
97 | }
|
98 |
|
99 | _fs2['default'].readFile(fileRelativePath, 'utf-8', function (err, source) {
|
100 | if (err) reject(err);
|
101 | _this.core.load(source, rootRelativePath, trace, _this.fetch.bind(_this)).then(function (_ref) {
|
102 | var injectableSource = _ref.injectableSource;
|
103 | var exportTokens = _ref.exportTokens;
|
104 |
|
105 | _this.sources[fileRelativePath] = injectableSource;
|
106 | _this.traces[trace] = fileRelativePath;
|
107 | _this.tokensByFile[fileRelativePath] = exportTokens;
|
108 | resolve(exportTokens);
|
109 | }, reject);
|
110 | });
|
111 | });
|
112 | }
|
113 | }, {
|
114 | key: 'finalSource',
|
115 | get: function () {
|
116 | var traces = this.traces;
|
117 | var sources = this.sources;
|
118 | var written = {};
|
119 |
|
120 | return Object.keys(traces).sort(traceKeySorter).map(function (key) {
|
121 | var filename = traces[key];
|
122 | if (written[filename] === true) {
|
123 | return null;
|
124 | }
|
125 | written[filename] = true;
|
126 |
|
127 | return sources[filename];
|
128 | }).join('');
|
129 | }
|
130 | }]);
|
131 |
|
132 | return FileSystemLoader;
|
133 | })();
|
134 |
|
135 | exports['default'] = FileSystemLoader;
|
136 | module.exports = exports['default'];
|