@mlut/core 2.1.1 → 2.1.2

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.
@@ -1,5 +1,7 @@
1
1
  export declare class JitEngine {
2
2
  private utils;
3
+ private breakpoints;
4
+ private breakpointsMap;
3
5
  private inputFileDir;
4
6
  private sassModuleName;
5
7
  private inputFileCache;
@@ -13,6 +15,7 @@ export declare class JitEngine {
13
15
  updateSassConfig(content: string): Promise<void>;
14
16
  generateCss(): Promise<string>;
15
17
  private extractUtils;
18
+ private compareUtilsWithAtRule;
16
19
  private filterAndProcessClassStr;
17
20
  private extractUserSassConfig;
18
21
  private loadUtils;
@@ -9,6 +9,8 @@ const sass = await import('sass-embedded')
9
9
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
10
10
  export class JitEngine {
11
11
  utils = new Set();
12
+ breakpoints = [];
13
+ breakpointsMap = {};
12
14
  inputFileDir = __dirname;
13
15
  sassModuleName = 'tools';
14
16
  inputFileCache = '@use "../sass/tools";';
@@ -83,8 +85,10 @@ export class JitEngine {
83
85
  .replace(this.utilsRegexps.singleQuotedContent, '')
84
86
  .match(this.utilsRegexps.backtickQuotedContent)
85
87
  ?.reduce(this.filterAndProcessClassStr, allClassNames);
86
- return [...allClassNames.join(' ').split(' ').reduce((acc, cssClass) => {
87
- const utility = cssClass.split('_').find((str) => (this.utilsRegexps.utilName.test(str)));
88
+ const utilsWithAtRule = new Set();
89
+ const mainUtils = [...allClassNames.join(' ').split(' ').reduce((acc, cssClass) => {
90
+ const utilComponents = cssClass.split('_');
91
+ const utility = utilComponents.find((str) => (this.utilsRegexps.utilName.test(str)));
88
92
  if (utility) {
89
93
  const separator = utility.replace(this.utilsRegexps.utilName, '')[0];
90
94
  if (separator && !this.utilsRegexps.valueSeparator.test(separator)) {
@@ -93,12 +97,42 @@ export class JitEngine {
93
97
  const utilName = utility.match(this.utilsRegexps.utilName)?.[0];
94
98
  if (this.utils.has(utilName) ||
95
99
  (utilName[0] === '-' && !this.utilsRegexps.contextUtil.test(utilName))) {
96
- acc.add(cssClass);
100
+ const [atRule] = utilComponents;
101
+ if (utilComponents.length > 1 && !atRule.startsWith(utilName) && (atRule[0] === '@' ||
102
+ atRule[0] === '<' ||
103
+ (atRule in this.breakpointsMap) ||
104
+ this.breakpoints.find((item) => atRule.startsWith(item)))) {
105
+ utilsWithAtRule.add(cssClass);
106
+ }
107
+ else {
108
+ acc.add(cssClass);
109
+ }
97
110
  }
98
111
  }
99
112
  return acc;
100
113
  }, new Set())];
114
+ return mainUtils.concat([...utilsWithAtRule].sort(this.compareUtilsWithAtRule));
101
115
  }
116
+ compareUtilsWithAtRule = (a, b) => {
117
+ if (a[0] === '@' && b[0] === '@') {
118
+ return 0;
119
+ }
120
+ if (a[0] === '@') {
121
+ return 1;
122
+ }
123
+ if (b[0] === '@') {
124
+ return -1;
125
+ }
126
+ const bpA = a.split('_')[0];
127
+ const bpB = b.split('_')[0];
128
+ if (!(bpA in this.breakpointsMap)) {
129
+ return 1;
130
+ }
131
+ if (!(bpB in this.breakpointsMap)) {
132
+ return -1;
133
+ }
134
+ return this.breakpointsMap[bpA] - this.breakpointsMap[bpB];
135
+ };
102
136
  filterAndProcessClassStr = (acc, str) => {
103
137
  if (this.utilsRegexps.uppercaseLetter.test(str)) {
104
138
  acc.push(str.replace(this.utilsRegexps.tooMoreSpaces, ' ').slice(1, -1));
@@ -120,12 +154,25 @@ export class JitEngine {
120
154
  }
121
155
  }
122
156
  async loadUtils(userConfig = this.defaultSassConfig) {
123
- const { css } = (await sass.compileStringAsync(userConfig + '\n a{ all: map.keys(map.get(ml.$utils-db, "utils", "registry")); }', {
157
+ const placeholderRules = `
158
+ \n a{ all: map.keys(map.get(ml.$utils-db, "utils", "registry")); }
159
+ \n b{ all: map.keys(map.get(ml.$at-rules-cfg, "breakpoints")); }
160
+ `;
161
+ const { css } = (await sass.compileStringAsync(userConfig + placeholderRules, {
124
162
  style: 'compressed',
125
163
  loadPaths: [__dirname, 'node_modules'],
126
164
  }));
127
- const strEnd = css.lastIndexOf('"') - css.length + 1;
128
- this.utils = new Set(JSON.parse('[' + css.split('all:')[1].slice(0, strEnd) + ']'));
165
+ const [, rawUtils, rawBreakpoints] = css.split('all:');
166
+ const rawUtilsStrEnd = rawUtils.lastIndexOf('"') - rawUtils.length + 1;
167
+ const rawBpsStrEnd = rawBreakpoints.lastIndexOf('"') - rawBreakpoints.length + 1;
168
+ this.utils = new Set(JSON.parse('[' + rawUtils.slice(0, rawUtilsStrEnd) + ']'));
169
+ this.breakpoints =
170
+ JSON.parse('[' + rawBreakpoints.slice(0, rawBpsStrEnd) + ']');
171
+ this.breakpointsMap = this.breakpoints.reduce((acc, item, i) => {
172
+ acc['<' + item] = i - 0.1;
173
+ acc[item] = i;
174
+ return acc;
175
+ }, {});
129
176
  }
130
177
  }
131
178
  export const jitEngine = new JitEngine();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mlut/core",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "Atomic CSS toolkit with Sass and ergonomics for creating styles of any complexity",
5
5
  "author": "mr150",
6
6
  "type": "module",