@jesscss/style-resolver 2.0.0-alpha.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Matthew Dean
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # @jesscss/style-resolver
2
+
3
+ **Stylesheet import resolution across CSS, Less, SCSS, and Jess — include paths,
4
+ load paths, and extension/index resolution.**
5
+
6
+ `@jesscss/style-resolver` is a building block for
7
+ [Jess](https://github.com/jesscss/jess). Given a stylesheet's source and an import statement, it
8
+ figures out which file that import actually points to, applying each language's
9
+ lookup rules (Less and SCSS candidate expansion, partials, index files, search
10
+ paths).
11
+
12
+ It handles the mechanics of finding imports — extracting import statements from
13
+ source, expanding a bare import path into the ordered list of candidate files a
14
+ given language would try, and resolving that against a filesystem-like interface.
15
+
16
+ ## Where it fits
17
+
18
+ Import resolution is a shared concern the compiler and tooling both need. It is
19
+ also one of the building blocks toward the broader module/scoping story on the
20
+ Jess roadmap (whose headline is the post-`.jess` minimal browser build) — but this
21
+ package is just the resolver, not that feature.
22
+
23
+ ## Who uses it
24
+
25
+ This is an internal package. Most people should install
26
+ [`jess`](https://www.npmjs.com/package/jess) and use the `jess` CLI.
27
+ The JavaScript/TypeScript API is **not yet stabilized** and is intentionally
28
+ undocumented for now.
29
+
30
+ ## Status
31
+
32
+ Alpha. Published to npm under both the `latest` and `alpha` dist-tags. Please
33
+ [report bugs](https://github.com/jesscss/jess/issues).
34
+
35
+ ## Links
36
+
37
+ - Repository: <https://github.com/jesscss/jess>
38
+ - Documentation: <https://jesscss.github.io/> (currently pre-alpha content)
39
+
40
+ ## License
41
+
42
+ [MIT](https://github.com/jesscss/jess/blob/dev/LICENSE)
package/lib/index.cjs ADDED
@@ -0,0 +1,122 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+ //#endregion
24
+ let node_path = require("node:path");
25
+ node_path = __toESM(node_path);
26
+ //#region src/index.ts
27
+ function uniq(items) {
28
+ const out = [];
29
+ const seen = /* @__PURE__ */ new Set();
30
+ for (const it of items) {
31
+ if (seen.has(it)) continue;
32
+ seen.add(it);
33
+ out.push(it);
34
+ }
35
+ return out;
36
+ }
37
+ function stripQueryHash(input) {
38
+ const m = input.match(/^([^?#]+)([?#].*)?$/);
39
+ return {
40
+ filePart: m?.[1] ?? input,
41
+ suffix: m?.[2] ?? ""
42
+ };
43
+ }
44
+ function extractImports(sourceText, lang) {
45
+ const out = [];
46
+ const re = /@(import|use)\s*(?:\(([^)]*)\)\s*)*(?:url\(\s*)?(?:'([^']+)'|"([^"]+)")/g;
47
+ for (let m; m = re.exec(sourceText);) {
48
+ const optionsRaw = m[2] ?? "";
49
+ const options = optionsRaw ? optionsRaw.split(",").map((s) => s.trim()).filter(Boolean) : void 0;
50
+ const spec = m[3] ?? m[4] ?? "";
51
+ if (!spec) continue;
52
+ const startInMatch = m[3] != null ? m[0].indexOf(m[3]) : m[0].indexOf(m[4] ?? "");
53
+ const startOffset = m.index + startInMatch;
54
+ const endOffset = startOffset + spec.length;
55
+ out.push({
56
+ lang,
57
+ specifier: spec,
58
+ options,
59
+ specifierRange: {
60
+ startOffset,
61
+ endOffset
62
+ }
63
+ });
64
+ }
65
+ return out;
66
+ }
67
+ function expandLessImportCandidates(importPath) {
68
+ if (node_path.extname(importPath) !== ".less") return [`${importPath}.less`, `${importPath}`];
69
+ return [importPath];
70
+ }
71
+ function expandScssImportCandidates(importPath) {
72
+ const ext = node_path.extname(importPath);
73
+ const base = ext ? importPath.slice(0, -ext.length) : importPath;
74
+ const candidates = [];
75
+ const pushUnique = (p) => {
76
+ if (!candidates.includes(p)) candidates.push(p);
77
+ };
78
+ const withExt = (p) => p.endsWith(".scss") ? p : `${p}.scss`;
79
+ if (ext) {
80
+ pushUnique(importPath);
81
+ const dir = node_path.dirname(importPath);
82
+ const file = node_path.basename(importPath);
83
+ if (!file.startsWith("_")) pushUnique(node_path.join(dir, `_${file}`));
84
+ return candidates;
85
+ }
86
+ pushUnique(withExt(base));
87
+ pushUnique(withExt(node_path.join(node_path.dirname(base), `_${node_path.basename(base)}`)));
88
+ pushUnique(withExt(node_path.join(base, "index")));
89
+ pushUnique(withExt(node_path.join(base, "_index")));
90
+ return candidates;
91
+ }
92
+ function resolveImport(fs, opts) {
93
+ const specifier = opts.specifier.trim();
94
+ if (!specifier) return null;
95
+ if (/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(specifier) || specifier.startsWith("data:")) return null;
96
+ const { filePart, suffix } = stripQueryHash(specifier);
97
+ const fromDir = node_path.dirname(opts.fromFilePath);
98
+ const cfg = opts.config ?? {};
99
+ const searchPaths = [fromDir];
100
+ if (opts.lang === "less" && Array.isArray(cfg.includePaths)) searchPaths.push(...cfg.includePaths);
101
+ if (opts.lang === "scss" && Array.isArray(cfg.loadPaths)) searchPaths.push(...cfg.loadPaths);
102
+ if (cfg.rootDir) searchPaths.push(cfg.rootDir);
103
+ const candidatesRel = opts.lang === "less" ? expandLessImportCandidates(filePart) : opts.lang === "scss" ? expandScssImportCandidates(filePart) : [filePart.endsWith(".css") ? filePart : `${filePart}.css`, filePart];
104
+ const candidatesAbs = [];
105
+ for (const baseDir of uniq(searchPaths)) for (const rel of candidatesRel) {
106
+ const abs = node_path.resolve(baseDir, rel);
107
+ candidatesAbs.push({
108
+ p: abs,
109
+ resolvedBy: baseDir === fromDir ? "exact" : "loadPath"
110
+ });
111
+ }
112
+ for (const c of candidatesAbs) if (fs.exists(c.p)) return {
113
+ filePath: c.p + suffix,
114
+ resolvedBy: c.resolvedBy
115
+ };
116
+ return null;
117
+ }
118
+ //#endregion
119
+ exports.expandLessImportCandidates = expandLessImportCandidates;
120
+ exports.expandScssImportCandidates = expandScssImportCandidates;
121
+ exports.extractImports = extractImports;
122
+ exports.resolveImport = resolveImport;
package/lib/index.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ export type StyleLang = 'css' | 'less' | 'scss' | 'jess';
2
+ export type FsLike = {
3
+ exists(filePath: string): boolean;
4
+ };
5
+ export type StylesConfig = {
6
+ /** Project root; used as an optional search base. */
7
+ rootDir?: string;
8
+ /** Less-style include paths. */
9
+ includePaths?: string[];
10
+ /** SCSS-style load paths. */
11
+ loadPaths?: string[];
12
+ };
13
+ export type ImportStatement = {
14
+ lang: StyleLang;
15
+ specifier: string;
16
+ options?: string[];
17
+ specifierRange: {
18
+ startOffset: number;
19
+ endOffset: number;
20
+ };
21
+ };
22
+ export type ResolveResult = {
23
+ filePath: string;
24
+ resolvedBy: 'exact' | 'extension' | 'partial' | 'index' | 'loadPath';
25
+ };
26
+ export type ResolveImportOptions = {
27
+ lang: StyleLang;
28
+ fromFilePath: string;
29
+ specifier: string;
30
+ config?: StylesConfig;
31
+ };
32
+ export declare function extractImports(sourceText: string, lang: StyleLang): ImportStatement[];
33
+ export declare function expandLessImportCandidates(importPath: string): string[];
34
+ export declare function expandScssImportCandidates(importPath: string): string[];
35
+ export declare function resolveImport(fs: FsLike, opts: ResolveImportOptions): ResolveResult | null;
package/lib/index.js ADDED
@@ -0,0 +1,95 @@
1
+ import * as path from "node:path";
2
+ //#region src/index.ts
3
+ function uniq(items) {
4
+ const out = [];
5
+ const seen = /* @__PURE__ */ new Set();
6
+ for (const it of items) {
7
+ if (seen.has(it)) continue;
8
+ seen.add(it);
9
+ out.push(it);
10
+ }
11
+ return out;
12
+ }
13
+ function stripQueryHash(input) {
14
+ const m = input.match(/^([^?#]+)([?#].*)?$/);
15
+ return {
16
+ filePart: m?.[1] ?? input,
17
+ suffix: m?.[2] ?? ""
18
+ };
19
+ }
20
+ function extractImports(sourceText, lang) {
21
+ const out = [];
22
+ const re = /@(import|use)\s*(?:\(([^)]*)\)\s*)*(?:url\(\s*)?(?:'([^']+)'|"([^"]+)")/g;
23
+ for (let m; m = re.exec(sourceText);) {
24
+ const optionsRaw = m[2] ?? "";
25
+ const options = optionsRaw ? optionsRaw.split(",").map((s) => s.trim()).filter(Boolean) : void 0;
26
+ const spec = m[3] ?? m[4] ?? "";
27
+ if (!spec) continue;
28
+ const startInMatch = m[3] != null ? m[0].indexOf(m[3]) : m[0].indexOf(m[4] ?? "");
29
+ const startOffset = m.index + startInMatch;
30
+ const endOffset = startOffset + spec.length;
31
+ out.push({
32
+ lang,
33
+ specifier: spec,
34
+ options,
35
+ specifierRange: {
36
+ startOffset,
37
+ endOffset
38
+ }
39
+ });
40
+ }
41
+ return out;
42
+ }
43
+ function expandLessImportCandidates(importPath) {
44
+ if (path.extname(importPath) !== ".less") return [`${importPath}.less`, `${importPath}`];
45
+ return [importPath];
46
+ }
47
+ function expandScssImportCandidates(importPath) {
48
+ const ext = path.extname(importPath);
49
+ const base = ext ? importPath.slice(0, -ext.length) : importPath;
50
+ const candidates = [];
51
+ const pushUnique = (p) => {
52
+ if (!candidates.includes(p)) candidates.push(p);
53
+ };
54
+ const withExt = (p) => p.endsWith(".scss") ? p : `${p}.scss`;
55
+ if (ext) {
56
+ pushUnique(importPath);
57
+ const dir = path.dirname(importPath);
58
+ const file = path.basename(importPath);
59
+ if (!file.startsWith("_")) pushUnique(path.join(dir, `_${file}`));
60
+ return candidates;
61
+ }
62
+ pushUnique(withExt(base));
63
+ pushUnique(withExt(path.join(path.dirname(base), `_${path.basename(base)}`)));
64
+ pushUnique(withExt(path.join(base, "index")));
65
+ pushUnique(withExt(path.join(base, "_index")));
66
+ return candidates;
67
+ }
68
+ function resolveImport(fs, opts) {
69
+ const specifier = opts.specifier.trim();
70
+ if (!specifier) return null;
71
+ if (/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(specifier) || specifier.startsWith("data:")) return null;
72
+ const { filePart, suffix } = stripQueryHash(specifier);
73
+ const fromDir = path.dirname(opts.fromFilePath);
74
+ const cfg = opts.config ?? {};
75
+ const searchPaths = [fromDir];
76
+ if (opts.lang === "less" && Array.isArray(cfg.includePaths)) searchPaths.push(...cfg.includePaths);
77
+ if (opts.lang === "scss" && Array.isArray(cfg.loadPaths)) searchPaths.push(...cfg.loadPaths);
78
+ if (cfg.rootDir) searchPaths.push(cfg.rootDir);
79
+ const candidatesRel = opts.lang === "less" ? expandLessImportCandidates(filePart) : opts.lang === "scss" ? expandScssImportCandidates(filePart) : [filePart.endsWith(".css") ? filePart : `${filePart}.css`, filePart];
80
+ const candidatesAbs = [];
81
+ for (const baseDir of uniq(searchPaths)) for (const rel of candidatesRel) {
82
+ const abs = path.resolve(baseDir, rel);
83
+ candidatesAbs.push({
84
+ p: abs,
85
+ resolvedBy: baseDir === fromDir ? "exact" : "loadPath"
86
+ });
87
+ }
88
+ for (const c of candidatesAbs) if (fs.exists(c.p)) return {
89
+ filePath: c.p + suffix,
90
+ resolvedBy: c.resolvedBy
91
+ };
92
+ return null;
93
+ }
94
+ //#endregion
95
+ export { expandLessImportCandidates, expandScssImportCandidates, extractImports, resolveImport };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@jesscss/style-resolver",
3
+ "version": "2.0.0-alpha.10",
4
+ "engines": {
5
+ "node": "^20.19.0 || >=22.12.0"
6
+ },
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "type": "module",
11
+ "main": "lib/index.cjs",
12
+ "types": "lib/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./lib/index.d.ts",
16
+ "import": "./lib/index.js",
17
+ "require": "./lib/index.cjs"
18
+ }
19
+ },
20
+ "files": [
21
+ "lib"
22
+ ],
23
+ "devDependencies": {
24
+ "typescript": "~5.8.2",
25
+ "vitest": "^4.1.0"
26
+ },
27
+ "module": "lib/index.js",
28
+ "scripts": {
29
+ "build": "pnpm compile",
30
+ "compile": "tsdown --tsconfig tsconfig.build.json --no-dts && tsc -p tsconfig.build.json --emitDeclarationOnly --noCheck",
31
+ "test": "vitest --watch=false",
32
+ "lint": "eslint '**/*.{js,ts}'"
33
+ }
34
+ }