@mlut/core 2.2.0 → 2.3.0

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,3 +1,4 @@
1
+ export declare const fileURLToPath: typeof import("url").fileURLToPath;
1
2
  export declare class JitEngine {
2
3
  private utils;
3
4
  private breakpoints;
@@ -1,12 +1,23 @@
1
- import path from 'node:path';
2
- import { fileURLToPath } from 'node:url';
1
+ import { path } from '../utils/path.js';
3
2
  import { logger } from '../utils/index.js';
3
+ const isNode = globalThis.process?.env != undefined;
4
+ const isTestEnv = globalThis.process?.env?.NODE_ENV === 'test';
4
5
  const sass = await import('sass-embedded')
5
6
  .catch(() => import('sass'))
7
+ .catch(() => import('https://jspm.dev/sass'))
6
8
  .catch(() => {
7
9
  throw new Error('The Sass package is not installed. You can do this with `npm i -D sass-embedded`');
8
10
  });
9
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
11
+ const sassImporters = !isTestEnv && isNode ? [] :
12
+ await import('./importerFromMemory.js')
13
+ .then((r) => [r.importer])
14
+ .catch(() => []);
15
+ export const fileURLToPath = isNode ?
16
+ await import('node:url').then((r) => r.fileURLToPath) :
17
+ ((str) => str.toString());
18
+ const __dirname = isNode ?
19
+ path.dirname(fileURLToPath(import.meta.url)) :
20
+ globalThis.location?.origin ?? 'http://localhost';
10
21
  export class JitEngine {
11
22
  utils = new Set();
12
23
  breakpoints = [];
@@ -37,7 +48,9 @@ export class JitEngine {
37
48
  }
38
49
  let sassConfig = this.defaultSassConfig;
39
50
  if (inputPath && inputContent) {
40
- this.inputFileDir = path.dirname(path.resolve(process.cwd(), inputPath));
51
+ if (isNode) {
52
+ this.inputFileDir = path.dirname(path.resolve(process.cwd(), inputPath));
53
+ }
41
54
  this.inputFileCache = inputContent;
42
55
  sassConfig = this.extractUserSassConfig(inputContent);
43
56
  }
@@ -77,7 +90,11 @@ export class JitEngine {
77
90
  const sortedAtRuleUtils = atRuleUtils.flat().sort(this.compareUtilsWithAtRule);
78
91
  const allUniqueUtils = [...new Set(mainUtils.flat().concat(sortedAtRuleUtils))];
79
92
  const applyStr = `\n@include ${this.sassModuleName}.apply(${JSON.stringify(allUniqueUtils)},(),true);`;
80
- return sass.compileStringAsync(this.inputFileCache + applyStr, { loadPaths: [this.inputFileDir, 'node_modules'] }).then(({ css }) => css, (e) => (logger.error('Sass compilation error.', e), ''));
93
+ return sass.compileStringAsync(this.inputFileCache + applyStr, {
94
+ loadPaths: [this.inputFileDir, 'node_modules'],
95
+ importers: sassImporters,
96
+ silenceDeprecations: ['if-function',]
97
+ }).then(({ css }) => css, (e) => (logger.error('Sass compilation error.', e), ''));
81
98
  }
82
99
  extractUtils(content) {
83
100
  let fixedContent = content.replace(this.utilsRegexps.escapedQuotes, '');
@@ -168,6 +185,8 @@ export class JitEngine {
168
185
  const { css } = (await sass.compileStringAsync(userConfig + placeholderRules, {
169
186
  style: 'compressed',
170
187
  loadPaths: [__dirname, 'node_modules'],
188
+ importers: sassImporters,
189
+ silenceDeprecations: ['if-function',]
171
190
  }));
172
191
  const [, rawUtils, rawBreakpoints] = css.split('all:');
173
192
  const rawUtilsStrEnd = rawUtils.lastIndexOf('"') - rawUtils.length + 1;
@@ -0,0 +1,17 @@
1
+ declare global {
2
+ var mlut: {
3
+ githubToken?: string;
4
+ };
5
+ }
6
+ export declare class SassSourcesLoader {
7
+ private readonly kit;
8
+ private readonly owner;
9
+ private readonly repo;
10
+ private readonly unusedPath;
11
+ private readonly dirs;
12
+ readonly initPath = "packages/core/src/sass";
13
+ loadDir(dirPath: string): Promise<string[]>;
14
+ isDir(dirPath: string): boolean;
15
+ loadFile(filePath: string): Promise<string>;
16
+ }
17
+ export declare const sassSourcesLoader: SassSourcesLoader;
@@ -0,0 +1,58 @@
1
+ const retryPromise = import('https://esm.sh/@octokit/plugin-retry')
2
+ .catch(() => import('@octokit/plugin-retry'))
3
+ .then((r) => r.retry);
4
+ const octokit = await import('https://esm.sh/@octokit/rest')
5
+ .catch(() => import('@octokit/rest'))
6
+ .then(async (r) => {
7
+ const retry = await retryPromise;
8
+ const ctr = r.Octokit.plugin(retry);
9
+ return new ctr({
10
+ auth: globalThis.mlut?.githubToken ?? process.env.GITHUB_TOKEN,
11
+ });
12
+ });
13
+ export class SassSourcesLoader {
14
+ kit = octokit;
15
+ owner = 'mlutcss';
16
+ repo = 'mlut';
17
+ unusedPath = 'packages/core/src/sass/css/utils';
18
+ dirs = new Set();
19
+ initPath = 'packages/core/src/sass';
20
+ async loadDir(dirPath) {
21
+ if (dirPath.includes(this.unusedPath)) {
22
+ return [];
23
+ }
24
+ return this.kit.repos.getContent({
25
+ owner: this.owner,
26
+ repo: this.repo,
27
+ path: dirPath,
28
+ }).then((r) => {
29
+ if (!Array.isArray(r.data)) {
30
+ return [];
31
+ }
32
+ const result = [];
33
+ for (const item of r.data) {
34
+ if (item.type === 'dir') {
35
+ this.dirs.add(item.path);
36
+ }
37
+ result.push(item.name);
38
+ }
39
+ return result;
40
+ });
41
+ }
42
+ isDir(dirPath) {
43
+ return this.dirs.has(dirPath);
44
+ }
45
+ async loadFile(filePath) {
46
+ return this.kit.repos.getContent({
47
+ owner: this.owner,
48
+ repo: this.repo,
49
+ path: filePath,
50
+ }).then((r) => {
51
+ if (Array.isArray(r.data) || r.data.type !== 'file') {
52
+ return '';
53
+ }
54
+ return globalThis.atob(r.data.content);
55
+ });
56
+ }
57
+ }
58
+ export const sassSourcesLoader = new SassSourcesLoader();
@@ -0,0 +1,8 @@
1
+ import type { Importer, ImporterResult } from 'sass';
2
+ export declare class ModuleImporter implements Importer<'async'> {
3
+ private modules;
4
+ private aliases;
5
+ canonicalize: (initUrl: string) => URL | null;
6
+ load: (canonicalUrl: URL) => ImporterResult;
7
+ }
8
+ export declare const importer: ModuleImporter;
@@ -0,0 +1,66 @@
1
+ import { path } from '../utils/path.js';
2
+ import { sassSourcesLoader } from './SassSourcesLoader.js';
3
+ const locationOrigin = globalThis.location?.origin ?? 'http://localhost';
4
+ const sassIndexFileName = '_index.scss';
5
+ async function loadSourcesRecursive(loader) {
6
+ const result = new Map();
7
+ const pathPrefix = locationOrigin + '/';
8
+ const doLoadRecursive = async (entry, map) => {
9
+ const files = await loader.loadDir(entry);
10
+ await Promise.all(files.map(async (item) => {
11
+ const itemPath = path.join(entry, item);
12
+ const isDir = loader.isDir(itemPath);
13
+ if (isDir) {
14
+ await doLoadRecursive(itemPath, map);
15
+ }
16
+ else {
17
+ const pathObj = path.parse(itemPath);
18
+ if (pathObj.base !== sassIndexFileName && pathObj.base.startsWith('_')) {
19
+ pathObj.base = pathObj.base.slice(1);
20
+ }
21
+ const finalPath = pathPrefix + path.format(pathObj);
22
+ map.set(finalPath, (await loader.loadFile(itemPath)));
23
+ }
24
+ }));
25
+ return map;
26
+ };
27
+ return doLoadRecursive(sassSourcesLoader.initPath, result);
28
+ }
29
+ const modulesMap = await loadSourcesRecursive(sassSourcesLoader);
30
+ export class ModuleImporter {
31
+ modules = modulesMap;
32
+ aliases = {
33
+ '@mlut/core': '../sass/index',
34
+ '@mlut/core/tools': '../sass/tools',
35
+ };
36
+ canonicalize = (initUrl) => {
37
+ let url = initUrl;
38
+ if (!url.includes('/sass/')) {
39
+ if (url in this.aliases) {
40
+ url = this.aliases[url];
41
+ }
42
+ else if (!url.startsWith('@mlut/core')) {
43
+ return null;
44
+ }
45
+ }
46
+ const isEntryUrl = !url.startsWith('http');
47
+ let newUrl = url + '.scss';
48
+ if (isEntryUrl) {
49
+ newUrl = new URL(path.join(locationOrigin, sassSourcesLoader.initPath, url.split('sass')[1]) + '.scss').href;
50
+ }
51
+ if (!this.modules.has(newUrl)) {
52
+ newUrl = path.join(newUrl.replace('.scss', ''), sassIndexFileName);
53
+ }
54
+ return new URL(newUrl);
55
+ };
56
+ load = (canonicalUrl) => {
57
+ if (!this.modules.has(canonicalUrl.href)) {
58
+ throw new Error('Unknown module');
59
+ }
60
+ return {
61
+ contents: this.modules.get(canonicalUrl.href),
62
+ syntax: 'scss'
63
+ };
64
+ };
65
+ }
66
+ export const importer = new ModuleImporter();
@@ -747,6 +747,7 @@ $utils-db: (
747
747
  ),
748
748
  'Bgr': (
749
749
  'properties': background-repeat,
750
+ 'multi-list-separator': ml.$tULs1,
750
751
  'keywords': (
751
752
  '': no-repeat,
752
753
  'nr': no-repeat,
@@ -759,10 +760,12 @@ $utils-db: (
759
760
  ),
760
761
  'Bgp': (
761
762
  'properties': background-position,
763
+ 'multi-list-separator': ml.$tULs1,
762
764
  'keywords': 'position'
763
765
  ),
764
766
  'Bgs': (
765
767
  'properties': background-size,
768
+ 'multi-list-separator': ml.$tULs1,
766
769
  'keywords': (
767
770
  'ct': contain,
768
771
  'cv': cover,
@@ -770,6 +773,7 @@ $utils-db: (
770
773
  ),
771
774
  'Bga': (
772
775
  'properties': background-attachment,
776
+ 'multi-list-separator': ml.$tULs1,
773
777
  'keywords': (
774
778
  'f': fixed,
775
779
  'l': local,
@@ -783,13 +787,16 @@ $utils-db: (
783
787
  ),
784
788
  'Bgcl': (
785
789
  'properties': background-clip,
790
+ 'multi-list-separator': ml.$tULs1,
786
791
  'keywords': 'box-ext',
787
792
  ),
788
793
  'Bgo': (
789
794
  'properties': background-origin,
795
+ 'multi-list-separator': ml.$tULs1,
790
796
  'keywords': 'box-ext',
791
797
  ),
792
798
  'Bgi': (
799
+ 'multi-list-separator': ml.$tULs1,
793
800
  'properties': background-image,
794
801
  ),
795
802
  '-Gdl': (
@@ -0,0 +1,2 @@
1
+ import type { PlatformPath } from 'node:path';
2
+ export declare const path: PlatformPath;
@@ -0,0 +1,3 @@
1
+ export const path = await import('https://esm.sh/path-browserify-esm')
2
+ .catch(() => import('node:path'))
3
+ .then((r) => r.default);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mlut/core",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "Atomic CSS toolkit with Sass and ergonomics for creating styles of any complexity",
5
5
  "author": "mr150",
6
6
  "type": "module",
@@ -40,9 +40,12 @@
40
40
  "dist"
41
41
  ],
42
42
  "devDependencies": {
43
+ "@octokit/plugin-retry": "^8.0.1",
44
+ "@octokit/rest": "^22.0.0",
43
45
  "@types/node": "^20.10.5",
46
+ "path-browserify-esm": "^1.0.6",
44
47
  "sass": "^1.93.0",
45
48
  "sass-embedded": "^1.93.0",
46
- "typescript": "^4.8.0"
49
+ "typescript": "^5.8.0"
47
50
  }
48
51
  }