1 | "use strict";
|
2 |
|
3 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
4 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
5 | };
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | const promises_1 = __importDefault(require("node:fs/promises"));
|
8 | const node_path_1 = __importDefault(require("node:path"));
|
9 | const css_modules_loader_core_1 = __importDefault(require("./css-modules-loader-core"));
|
10 | class FileSystemLoader {
|
11 | constructor(root, plugins) {
|
12 | this.root = root;
|
13 | this.sources = {};
|
14 | this.importNr = 0;
|
15 | this.core = new css_modules_loader_core_1.default(plugins);
|
16 | this.tokensByFile = {};
|
17 | }
|
18 | async fetch(_newPath, relativeTo, _trace, initialContents) {
|
19 | const newPath = _newPath.replace(/^["']|["']$/g, '');
|
20 | const trace = _trace || String.fromCharCode(this.importNr++);
|
21 | const relativeDir = node_path_1.default.dirname(relativeTo);
|
22 | const rootRelativePath = node_path_1.default.resolve(relativeDir, newPath);
|
23 | let fileRelativePath = node_path_1.default.resolve(node_path_1.default.join(this.root, relativeDir), newPath);
|
24 | const isNodeModule = (fileName) => fileName[0] !== '.' && fileName[0] !== '/';
|
25 |
|
26 | if (isNodeModule(newPath)) {
|
27 | try {
|
28 | fileRelativePath = require.resolve(newPath);
|
29 | }
|
30 | catch (e) { }
|
31 | }
|
32 | let source;
|
33 | if (!initialContents) {
|
34 | const tokens = this.tokensByFile[fileRelativePath];
|
35 | if (tokens) {
|
36 | return tokens;
|
37 | }
|
38 | try {
|
39 | source = await promises_1.default.readFile(fileRelativePath, 'utf-8');
|
40 | }
|
41 | catch (error) {
|
42 | if (relativeTo && relativeTo !== '/') {
|
43 | return {};
|
44 | }
|
45 | throw error;
|
46 | }
|
47 | }
|
48 | else {
|
49 | source = initialContents;
|
50 | }
|
51 | const { injectableSource, exportTokens } = await this.core.load(source, rootRelativePath, trace, this.fetch.bind(this));
|
52 | const re = new RegExp(/@import\s'(\D+?)';/, 'gm');
|
53 | let importTokens = {};
|
54 | let result;
|
55 | while ((result = re.exec(injectableSource))) {
|
56 | const importFile = result === null || result === void 0 ? void 0 : result[1];
|
57 | if (importFile) {
|
58 | let importFilePath = isNodeModule(importFile)
|
59 | ? importFile
|
60 | : node_path_1.default.resolve(node_path_1.default.dirname(fileRelativePath), importFile);
|
61 | const localTokens = await this.fetch(importFilePath, relativeTo, undefined, initialContents);
|
62 | Object.assign(importTokens, localTokens);
|
63 | }
|
64 | }
|
65 | const tokens = { ...exportTokens, ...importTokens };
|
66 | this.sources[trace] = injectableSource;
|
67 | this.tokensByFile[fileRelativePath] = tokens;
|
68 | return tokens;
|
69 | }
|
70 | }
|
71 | exports.default = FileSystemLoader;
|
72 |
|
\ | No newline at end of file |