@rws-framework/client 2.9.13 → 2.9.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,187 @@
1
+ const { rwsPath } = require('@rws-framework/console');
2
+ const _tools = require('../../_tools');
3
+ const _scss_fs_builder = require('./_fs');
4
+ let _scss_fs = null;
5
+ const fs = require('fs');
6
+ const path = require('path');
7
+ const CSS_IMPORT_REGEX = /^(?!.*\/\/)(?!.*\/\*).*@import\s+['"]((?![^'"]*:[^'"]*).+?)['"];?/gm;
8
+ const SCSS_USE_REGEX = /^(?!.*\/\/)(?!.*\/\*).*@use\s+['"]?([^'"\s]+)['"]?;?/gm;
9
+
10
+ function processImportPath(importPath, fileRootDir = null, noext = false) {
11
+ _scss_fs = _scss_fs_builder(this);
12
+
13
+ if (importPath.split('')[0] === '~') {
14
+ return fillSCSSExt(replaceWithNodeModules(importPath, null, true), noext);
15
+ }
16
+
17
+ if (importPath.indexOf('@rws-mixins') === 0) {
18
+ return path.resolve(rwsPath.findPackageDir(__dirname), 'src', 'styles', 'includes.scss');
19
+ }
20
+
21
+ if (importPath.indexOf('@cwd') === 0) {
22
+ return fillSCSSExt(process.cwd() + '/' + importPath.slice(4), noext);
23
+ }
24
+
25
+ if (importPath.split('')[0] === '/') {
26
+
27
+ return fillSCSSExt(importPath, noext);
28
+ }
29
+
30
+ if (fileRootDir) {
31
+ const relativized = path.resolve(fileRootDir) + '/' + importPath;
32
+
33
+ if (importPath.split('')[0] === '.') {
34
+ return fillSCSSExt(relativized, noext);
35
+ }
36
+
37
+ if (!fs.existsSync(relativized)) {
38
+ const partSplit = relativized.split('/');
39
+ partSplit[partSplit.length - 1] = '_' + partSplit[partSplit.length - 1] + '.scss';
40
+
41
+ const newPath = underscorePath(relativized);
42
+
43
+ if (fs.existsSync(newPath)) {
44
+ return newPath;
45
+ }
46
+ }
47
+ return fillSCSSExt(relativized, noext);
48
+ }
49
+
50
+ return importPath;
51
+ }
52
+
53
+ function underscorePath(path, noext = false) {
54
+ const partSplit = path.split('/');
55
+ partSplit[partSplit.length - 1] = '_' + partSplit[partSplit.length - 1] + (path.indexOf('.scss') > - 1 || noext ? '' : '.scss');
56
+ return partSplit.join('/');
57
+ }
58
+
59
+ function fillSCSSExt(scssPath, noext = false) {
60
+ const underscoredPath = underscorePath(scssPath, noext);
61
+ if (!fs.existsSync(scssPath) && fs.existsSync(underscoredPath)) {
62
+ return underscoredPath;
63
+ }
64
+
65
+ if (noext) {
66
+ return scssPath;
67
+ }
68
+
69
+ if ((!fs.existsSync(scssPath) || (fs.existsSync(scssPath) && fs.statSync(scssPath).isDirectory())) && fs.existsSync(`${scssPath}.scss`)) {
70
+ return `${scssPath}.scss`;
71
+ }
72
+
73
+ if (fs.existsSync(`_${scssPath}.scss`)) {
74
+ return `${scssPath}.scss`;
75
+ }
76
+
77
+ return scssPath;
78
+ }
79
+
80
+ function extractScssImports(fileContent, importRootPath) {
81
+ _scss_fs = _scss_fs_builder(this);
82
+ let match;
83
+ const imports = [];
84
+
85
+ while ((match = CSS_IMPORT_REGEX.exec(fileContent)) !== null) {
86
+ const importPath = match[1];
87
+ const importLine = match[0];
88
+
89
+ if (fs.statSync(importRootPath).isFile()) {
90
+ importRootPath = path.dirname(importRootPath);
91
+ }
92
+
93
+ const processedImportPath = processImportPath(importPath, importRootPath);
94
+
95
+ imports.push([processedImportPath, importLine, path.resolve(processedImportPath)]);
96
+ }
97
+
98
+ return [imports, fileContent];
99
+ }
100
+
101
+ function extractScssUses(fileContent) {
102
+ _scss_fs = _scss_fs_builder(this);
103
+ let match;
104
+ const uses = [];
105
+
106
+ while ((match = SCSS_USE_REGEX.exec(fileContent)) !== null) {
107
+ const usesPath = match[1];
108
+ const usesLine = match[0];
109
+
110
+ if (!uses.find((item) => {
111
+ return item[0] == usesPath
112
+ }) && !usesPath !== 'sass:math') {
113
+ uses.push([usesPath, usesLine]);
114
+ }
115
+ }
116
+
117
+ // console.log(uses);
118
+
119
+ return [uses];
120
+ }
121
+
122
+ function detectImports(code) {
123
+ return CSS_IMPORT_REGEX.test(code);
124
+ }
125
+
126
+ function replaceWithNodeModules(input, fileDir = null, absolute = false, token = '~') {
127
+ _scss_fs = _scss_fs_builder(this);
128
+ return input.replace(token, absolute ? `${path.resolve(_tools.findRootWorkspacePath(process.cwd()), 'node_modules')}/` : this.node_modules_dir(fileDir ? fileDir : process.cwd()));
129
+ }
130
+
131
+ function processImports(imports, fileRootDir, importStorage = {}, sub = false) {
132
+ _scss_fs = _scss_fs_builder(this);
133
+ const importResults = [];
134
+
135
+ const getStorage = (sourceComponentPath, importedFileContent) => {
136
+ const sourceComponentPathFormatted = sourceComponentPath.replace('/', '_');
137
+
138
+ if (!(sourceComponentPathFormatted in importStorage)) {
139
+ importStorage[sourceComponentPathFormatted] = importedFileContent;
140
+
141
+ return importedFileContent;
142
+ }
143
+
144
+ return '';
145
+ }
146
+
147
+ imports.forEach(importData => {
148
+ const originalImportPath = importData[0];
149
+ let importPath = processImportPath(originalImportPath, fileRootDir);
150
+ _scss_fs = _scss_fs_builder(this);
151
+ let replacedScssContent = getStorage(importPath, _scss_fs.getCodeFromFile(importPath).replace(/\/\*[\s\S]*?\*\//g, ''));
152
+
153
+ const recursiveImports = extractScssImports(replacedScssContent, importPath)[0];
154
+
155
+ if (recursiveImports.length) {
156
+
157
+ replacedScssContent = replaceImports(processImports(recursiveImports, path.dirname(importPath), importStorage, true), replacedScssContent);
158
+ }
159
+
160
+ importResults.push({
161
+ line: importData[1],
162
+ code: replacedScssContent
163
+ });
164
+ });
165
+
166
+ return importResults;
167
+ }
168
+
169
+ function replaceImports(processedImports, code) {
170
+ processedImports.forEach(importObj => {
171
+ code = code.replace(importObj.line, importObj.code);
172
+ });
173
+
174
+ return code;
175
+ }
176
+
177
+ module.exports = (element) => ({
178
+ processImportPath: processImportPath.bind(element),
179
+ fillSCSSExt: fillSCSSExt.bind(element),
180
+ underscorePath: underscorePath.bind(element),
181
+ detectImports: detectImports.bind(element),
182
+ extractScssUses: extractScssUses.bind(element),
183
+ extractScssImports: extractScssImports.bind(element),
184
+ replaceImports: replaceImports.bind(element),
185
+ processImports: processImports.bind(element),
186
+ replaceWithNodeModules: replaceWithNodeModules.bind(element)
187
+ });