mage-obsidian 1.0.9 → 1.0.10

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mage-obsidian",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "Mage Obsidian Ultis",
5
5
  "main": "./src/scripts/buildThemes.js",
6
6
  "keywords": [
@@ -1,63 +1,192 @@
1
- import themeResolver from './themeResolverSync.js';
2
- import path from 'node:path';
3
- import { MODULE_WEB_PATH, THEME_CSS_FOLDER, THEME_MODULE_WEB_PATH } from '../config/default.js';
4
- import { resolveFileByTheme } from './moduleResolver.js';
5
- import configResolver from './configResolver.js';
6
- import fs from 'node:fs/promises';
1
+ import path from "path";
2
+ import configResolver from "./configResolver.js";
3
+ import {
4
+ ALL_JS_VUE_FILES_WITH_INHERITANCE_FILE_NAME,
5
+ MODULE_WEB_PATH,
6
+ PRECOMPILED_FOLDER,
7
+ THEME_MODULE_WEB_PATH
8
+ } from "../config/default.js";
9
+ import getFilesFromFolders from '../utils/findComponents.js'
10
+ import fs from "fs";
11
+ import deepmerge from "deepmerge";
7
12
 
8
- const { getMagentoConfig, getModuleDefinition, getThemeDefinition, getModulesConfigArray } = configResolver;
13
+ const defaultFoldersToMap = [
14
+ {
15
+ src: configResolver.getMagentoConfig().VUE_COMPONENTS_PATH,
16
+ ext: [
17
+ 'vue',
18
+ 'js'
19
+ ]
20
+ },
21
+ {
22
+ src: configResolver.getMagentoConfig().JS_PATH,
23
+ ext: [
24
+ 'js'
25
+ ]
26
+ }
27
+ ];
28
+
29
+ const cachedModulesByTheme = {};
9
30
 
10
- const MODULE_CSS_EXTEND_FILE = getMagentoConfig().MODULE_CSS_EXTEND_FILE;
11
- const THEME_CSS_SOURCE_FILE = getMagentoConfig().THEME_CSS_SOURCE_FILE;
31
+ async function getAllJsVueFilesFromActiveModules() {
32
+ let result = {};
33
+ for (const entry of configResolver.getModulesConfigArray()) {
34
+ const [moduleName, moduleConfig] = entry;
35
+ const moduleResult = await getFilesFromFolders(
36
+ moduleName,
37
+ path.resolve(moduleConfig.src, MODULE_WEB_PATH),
38
+ defaultFoldersToMap
39
+ );
40
+ Object.assign(result, moduleResult);
41
+ }
42
+ return result;
43
+ }
12
44
 
13
- async function getThemeImports(themeName, themeConfig) {
14
- if (!themeConfig) {
15
- themeConfig = themeResolver.getThemeConfig(themeName);
45
+ async function getAllJsVueFilesFromTheme(themeName) {
46
+ let result = {};
47
+ const theme = configResolver.getMagentoConfig().themes[themeName];
48
+ if (!theme) {
49
+ return {}
16
50
  }
17
- const themeDefinition = getThemeDefinition(themeName);
18
- const themePath = themeDefinition.src;
19
51
 
20
- let imports = '';
21
- if (themeConfig.includeCssSourceFromParentThemes && themeDefinition.parent) {
22
- imports += await getThemeImports(themeDefinition.parent, themeConfig);
52
+ if (theme.parent) {
53
+ result = await getAllJsVueFilesFromTheme(theme.parent)
23
54
  }
24
55
 
25
- imports += `@import "${path.join(themePath, THEME_MODULE_WEB_PATH, THEME_CSS_FOLDER, THEME_CSS_SOURCE_FILE)}";\n`;
26
- return imports;
56
+ for (const moduleName of configResolver.getAllMagentoModulesEnabled()) {
57
+ const modulePath = path.resolve(theme.src, moduleName, THEME_MODULE_WEB_PATH);
58
+ const moduleResult = await getFilesFromFolders(
59
+ moduleName,
60
+ modulePath,
61
+ defaultFoldersToMap
62
+ );
63
+ if (moduleResult) {
64
+ result = deepmerge(result, moduleResult);
65
+ }
66
+ }
67
+ const moduleResult = await getFilesFromFolders(
68
+ 'Theme',
69
+ path.resolve(theme.src, THEME_MODULE_WEB_PATH),
70
+ defaultFoldersToMap
71
+ );
72
+ result = deepmerge(result, moduleResult);
73
+ return result;
27
74
  }
28
75
 
29
- function resolveModuleCssSourcePath(moduleName, themeName) {
30
- let moduleConfigSourcePath = resolveFileByTheme(themeName, moduleName, 'css', MODULE_CSS_EXTEND_FILE);
31
- if (!moduleConfigSourcePath) {
32
- const moduleDefinition = getModuleDefinition(moduleName);
33
- moduleConfigSourcePath = path.join(moduleDefinition.src, MODULE_WEB_PATH, 'css', MODULE_CSS_EXTEND_FILE);
76
+
77
+ function getAllJsVueFilesWithInheritanceCached(themeName) {
78
+ themeName = themeName ?? process.env.CURRENT_THEME;
79
+ if (cachedModulesByTheme[themeName]) {
80
+ return cachedModulesByTheme[themeName];
34
81
  }
35
- return moduleConfigSourcePath;
82
+ //check if exits
83
+
84
+ const filePath = path.join(PRECOMPILED_FOLDER, themeName, ALL_JS_VUE_FILES_WITH_INHERITANCE_FILE_NAME);
85
+ const fileContent = fs.readFileSync(filePath, "utf-8");
86
+ const data = JSON.parse(fileContent);
87
+ cachedModulesByTheme[themeName] = data;
88
+ return data;
89
+ }
90
+
91
+ async function getAllJsVueFilesWithInheritance(themeName) {
92
+ themeName = themeName ?? process.env.CURRENT_THEME;
93
+ if (cachedModulesByTheme[themeName]) {
94
+ return cachedModulesByTheme[themeName];
95
+ }
96
+ const allJsVueFilesFromModules = await getAllJsVueFilesFromActiveModules();
97
+ const allJsVueFilesFromThemes = await getAllJsVueFilesFromTheme(themeName);
98
+ cachedModulesByTheme[themeName] = deepmerge(allJsVueFilesFromModules, allJsVueFilesFromThemes);
99
+ return cachedModulesByTheme[themeName];
36
100
  }
37
101
 
38
- async function getCssImports(themeName) {
39
- const themeConfig = themeResolver.getThemeConfig(themeName);
40
- const excludedModules = new Set(themeConfig.ignoredCssFromModules || []);
41
- const modulesConfig = getModulesConfigArray();
102
+ const moduleConfigByThemeCache = new Map();
42
103
 
43
- let cssImports = '';
104
+ const MODULE_CONFIG_FILE = configResolver.getMagentoConfig().MODULE_CONFIG_FILE;
44
105
 
45
- if (themeConfig.ignoredCssFromModules !== 'all') {
46
- for (const [moduleName, moduleConfig] of modulesConfig) {
47
- if (excludedModules.has(moduleName)) continue;
106
+ /**
107
+ * Gets the theme configuration file path.
108
+ * @param {string} moduleSrc
109
+ * @returns {string} Full path to the configuration file.
110
+ */
111
+ function getModuleConfigPath(moduleSrc) {
112
+ return path.join(moduleSrc, 'view/frontend/web/', MODULE_CONFIG_FILE);
113
+ }
114
+
115
+ function resolveFileByTheme(themeName, moduleName, filePath, includeTailwindConfigFromParentThemes) {
116
+ const theme = configResolver.getMagentoConfig().themes[themeName];
117
+ const fullFilePath = path.join(theme.src, moduleName, 'web', filePath);
118
+ if (fs.existsSync(fullFilePath)) {
119
+ return fullFilePath;
120
+ }
48
121
 
49
- const filePath = resolveModuleCssSourcePath(moduleName, themeName);
50
- try {
51
- await fs.access(filePath);
52
- cssImports += `@import "${filePath}";\n`;
53
- } catch {
54
- // ignore if file doesn't exist
55
- }
122
+ if (includeTailwindConfigFromParentThemes && theme.parent) {
123
+ return resolveFileByTheme(theme.parent, moduleName, filePath, includeTailwindConfigFromParentThemes);
124
+ }
125
+ return null;
126
+ }
127
+
128
+ async function resolveModuleConfig(moduleName, themeName, includeTailwindConfigFromParentThemes) {
129
+ const module = configResolver.getMagentoConfig().modules[moduleName];
130
+ let moduleConfigSourcePath = resolveFileByTheme(themeName, moduleName, MODULE_CONFIG_FILE, includeTailwindConfigFromParentThemes);
131
+
132
+ if (!moduleConfigSourcePath && !module) {
133
+ return null;
134
+ }
135
+ if (!moduleConfigSourcePath) {
136
+ moduleConfigSourcePath = getModuleConfigPath(module.src);
137
+ }
138
+ if (!fs.existsSync(moduleConfigSourcePath)) {
139
+ return null;
140
+ }
141
+
142
+ // Importación dinámica común en ESModules
143
+ const moduleConfig = await import(moduleConfigSourcePath);
144
+ return moduleConfig.default ?? moduleConfig;
145
+ }
146
+
147
+ /**
148
+ * Retrieves the Tailwind configuration for a theme synchronously.
149
+ * Uses a cache system to optimize performance.
150
+ * @param {string} themeName - Name of the theme.
151
+ * @param {object} themeConfig
152
+ * @returns {object|null} Tailwind configuration object.
153
+ */
154
+ async function getModuleConfigByThemeConfig(themeName, themeConfig) {
155
+ if (moduleConfigByThemeCache.has(themeName)) {
156
+ return moduleConfigByThemeCache.get(themeName);
157
+ }
158
+ if (!themeConfig) return null;
159
+ const modules = configResolver.getAllMagentoModulesEnabled();
160
+ let modulesConfig = {};
161
+
162
+ for (const moduleName of modules) {
163
+ let moduleConfig = await resolveModuleConfig(moduleName, themeName, themeConfig.includeTailwindConfigFromParentThemes);
164
+ if (!moduleConfig) continue;
165
+ if (
166
+ themeConfig.ignoredTailwindConfigFromModules === 'all' ||
167
+ themeConfig.ignoredTailwindConfigFromModules.includes(moduleName)
168
+ ) {
169
+ moduleConfig.tailwind = {};
170
+ } else if (moduleConfig.tailwind?.content) {
171
+ const moduleSrc = configResolver.getMagentoConfig().modules[moduleName].src;
172
+ moduleConfig.tailwind.content = moduleConfig.tailwind.content.map((content) =>
173
+ path.join(moduleSrc, 'view/frontend/web', content)
174
+ );
56
175
  }
176
+
177
+ modulesConfig = deepmerge(modulesConfig, moduleConfig);
57
178
  }
58
179
 
59
- cssImports += await getThemeImports(themeName);
60
- return cssImports;
180
+ moduleConfigByThemeCache.set(themeName, modulesConfig);
181
+ return modulesConfig;
61
182
  }
62
183
 
63
- export default getCssImports;
184
+ export default {
185
+ getAllJsVueFilesWithInheritance,
186
+ getAllJsVueFilesWithInheritanceCached,
187
+ getModuleConfigByThemeConfig,
188
+ resolveFileByTheme
189
+ };
190
+
191
+ export { getModuleConfigByThemeConfig, resolveFileByTheme, getAllJsVueFilesWithInheritance, getAllJsVueFilesWithInheritanceCached };
192
+
@@ -10,7 +10,7 @@ export default async (themeName) => {
10
10
  execSync('rm -rf .precompiled/*', { stdio: 'inherit' });
11
11
  execSync(`mkdir -p .precompiled/${themeName}`, { stdio: 'inherit' });
12
12
  console.log('Generating precompiled files');
13
- await precompileCss(themeName);
14
13
  await precompileJs(themeName);
14
+ await precompileCss(themeName);
15
15
  isPrecompiled = true;
16
16
  }