-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathFileSystemLoader.js
More file actions
141 lines (115 loc) · 3.68 KB
/
Copy pathFileSystemLoader.js
File metadata and controls
141 lines (115 loc) · 3.68 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
// Initially copied from https://github.com/css-modules/css-modules-loader-core
import postcss from "postcss";
import path from "path";
import Parser from "./Parser";
import { getFileSystem } from "./fs";
class Core {
constructor(plugins) {
this.plugins = plugins || Core.defaultPlugins;
}
async load(sourceString, sourcePath, trace, pathFetcher) {
const parser = new Parser(pathFetcher, trace);
const plugins = this.plugins.concat([parser.plugin()]);
const result = await postcss(plugins).process(sourceString, {
from: sourcePath,
});
return {
injectableSource: result.css,
exportTokens: parser.exportTokens,
};
}
}
// 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
const traceKeySorter = (a, b) => {
if (a.length < b.length) {
return a < b.substring(0, a.length) ? -1 : 1;
}
if (a.length > b.length) {
return a.substring(0, b.length) <= b ? -1 : 1;
}
return a < b ? -1 : 1;
};
export default class FileSystemLoader {
constructor(root, plugins, fileResolve) {
if (root === "/" && process.platform === "win32") {
const cwdDrive = process.cwd().slice(0, 3);
if (!/^[A-Za-z]:\\$/.test(cwdDrive)) {
throw new Error(`Failed to obtain root from "${process.cwd()}".`);
}
root = cwdDrive;
}
this.root = root;
this.fileResolve = fileResolve;
this.sources = {};
this.traces = {};
this.importNr = 0;
this.core = new Core(plugins);
this.tokensByFile = {};
this.fs = getFileSystem();
}
async fetch(_newPath, relativeTo, _trace) {
const newPath = _newPath.replace(/^["']|["']$/g, "");
const trace = _trace || String.fromCharCode(this.importNr++);
const useFileResolve = typeof this.fileResolve === "function";
const fileResolvedPath = useFileResolve
? await this.fileResolve(newPath, relativeTo)
: await Promise.resolve();
if (fileResolvedPath && !path.isAbsolute(fileResolvedPath)) {
throw new Error('The returned path from the "fileResolve" option must be absolute.');
}
const relativeDir = path.dirname(relativeTo);
const rootRelativePath = fileResolvedPath || path.resolve(relativeDir, newPath);
let fileRelativePath =
fileResolvedPath || path.resolve(path.resolve(this.root, relativeDir), newPath);
// if the path is not relative or absolute, try to resolve it in node_modules
if (!useFileResolve && newPath[0] !== "." && !path.isAbsolute(newPath)) {
try {
fileRelativePath = require.resolve(newPath);
} catch {
// noop
}
}
const tokens = this.tokensByFile[fileRelativePath];
if (tokens) return tokens;
return new Promise((resolve, reject) => {
this.fs.readFile(fileRelativePath, "utf-8", async (err, source) => {
if (err) reject(err);
const { injectableSource, exportTokens } = await this.core.load(
source,
rootRelativePath,
trace,
this.fetch.bind(this),
);
this.sources[fileRelativePath] = injectableSource;
this.traces[trace] = fileRelativePath;
this.tokensByFile[fileRelativePath] = exportTokens;
resolve(exportTokens);
});
});
}
get finalSources() {
const traces = this.traces;
const sources = this.sources;
const written = new Set();
return Object.keys(traces)
.sort(traceKeySorter)
.map((key) => {
const filename = traces[key];
if (written.has(filename)) {
return null;
}
written.add(filename);
return { file: filename, source: sources[filename] };
})
.filter(Boolean);
}
get finalSource() {
return this.finalSources.map((entry) => entry.source).join("");
}
}