@mlut/core 2.1.0 → 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;
@@ -1,13 +1,16 @@
1
1
  import path from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
2
3
  import { logger } from '../utils/index.js';
3
4
  const sass = await import('sass-embedded')
4
5
  .catch(() => import('sass'))
5
6
  .catch(() => {
6
7
  throw new Error('The Sass package is not installed. You can do this with `npm i -D sass-embedded`');
7
8
  });
8
- const __dirname = new URL('.', import.meta.url).pathname;
9
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
9
10
  export class JitEngine {
10
11
  utils = new Set();
12
+ breakpoints = [];
13
+ breakpointsMap = {};
11
14
  inputFileDir = __dirname;
12
15
  sassModuleName = 'tools';
13
16
  inputFileCache = '@use "../sass/tools";';
@@ -22,6 +25,7 @@ export class JitEngine {
22
25
  utilName: /^-?[A-Z]{1}[a-zA-Z]*/,
23
26
  uppercaseLetter: /[A-Z]/,
24
27
  contextUtil: /-Ctx([\d\-#]|$)/,
28
+ valueSeparator: /[0-9-#=]/,
25
29
  };
26
30
  configRegexps = {
27
31
  userSettings: /@use ['"][^'"]*(tools|mlut|core)['"](\s*as\s+[\w]+)?\s+with\s*\(([^;]+)\);/s,
@@ -81,18 +85,54 @@ export class JitEngine {
81
85
  .replace(this.utilsRegexps.singleQuotedContent, '')
82
86
  .match(this.utilsRegexps.backtickQuotedContent)
83
87
  ?.reduce(this.filterAndProcessClassStr, allClassNames);
84
- return [...allClassNames.join(' ').split(' ').reduce((acc, cssClass) => {
85
- 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)));
86
92
  if (utility) {
93
+ const separator = utility.replace(this.utilsRegexps.utilName, '')[0];
94
+ if (separator && !this.utilsRegexps.valueSeparator.test(separator)) {
95
+ return acc;
96
+ }
87
97
  const utilName = utility.match(this.utilsRegexps.utilName)?.[0];
88
98
  if (this.utils.has(utilName) ||
89
99
  (utilName[0] === '-' && !this.utilsRegexps.contextUtil.test(utilName))) {
90
- 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
+ }
91
110
  }
92
111
  }
93
112
  return acc;
94
113
  }, new Set())];
114
+ return mainUtils.concat([...utilsWithAtRule].sort(this.compareUtilsWithAtRule));
95
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
+ };
96
136
  filterAndProcessClassStr = (acc, str) => {
97
137
  if (this.utilsRegexps.uppercaseLetter.test(str)) {
98
138
  acc.push(str.replace(this.utilsRegexps.tooMoreSpaces, ' ').slice(1, -1));
@@ -114,12 +154,25 @@ export class JitEngine {
114
154
  }
115
155
  }
116
156
  async loadUtils(userConfig = this.defaultSassConfig) {
117
- 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, {
118
162
  style: 'compressed',
119
163
  loadPaths: [__dirname, 'node_modules'],
120
164
  }));
121
- const strEnd = css.lastIndexOf('"') - css.length + 1;
122
- 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
+ }, {});
123
176
  }
124
177
  }
125
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.0",
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",