@jesscss/style-resolver 2.0.0-alpha.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.
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/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,136 @@
1
+ import * as path from 'node:path';
2
+ function uniq(items) {
3
+ const out = [];
4
+ const seen = new Set();
5
+ for (const it of items) {
6
+ if (seen.has(it)) {
7
+ continue;
8
+ }
9
+ seen.add(it);
10
+ out.push(it);
11
+ }
12
+ return out;
13
+ }
14
+ function stripQueryHash(input) {
15
+ const m = input.match(/^([^?#]+)([?#].*)?$/);
16
+ return { filePart: m?.[1] ?? input, suffix: m?.[2] ?? '' };
17
+ }
18
+ // Intentionally tolerant extraction for editor features (not a full parser).
19
+ // Supports:
20
+ // - @import "x";
21
+ // - @import (multiple, reference) "x";
22
+ // - @use "x";
23
+ // - @import url("x");
24
+ export function extractImports(sourceText, lang) {
25
+ const out = [];
26
+ // options: (a, b) blocks (Less)
27
+ // optional url(
28
+ // then a quoted string
29
+ const re = /@(import|use)\s*(?:\(([^)]*)\)\s*)*(?:url\(\s*)?(?:'([^']+)'|"([^"]+)")/g;
30
+ for (let m; (m = re.exec(sourceText));) {
31
+ const optionsRaw = m[2] ?? '';
32
+ const options = optionsRaw
33
+ ? optionsRaw.split(',').map(s => s.trim()).filter(Boolean)
34
+ : undefined;
35
+ const spec = m[3] ?? m[4] ?? '';
36
+ if (!spec) {
37
+ continue;
38
+ }
39
+ const startInMatch = m[3] != null ? m[0].indexOf(m[3]) : m[0].indexOf(m[4] ?? '');
40
+ const startOffset = m.index + startInMatch;
41
+ const endOffset = startOffset + spec.length;
42
+ out.push({
43
+ lang,
44
+ specifier: spec,
45
+ options,
46
+ specifierRange: { startOffset, endOffset }
47
+ });
48
+ }
49
+ return out;
50
+ }
51
+ // Mirrors current plugin behavior:
52
+ // - If ext is not .less: try `${importPath}.less`, then `${importPath}`.
53
+ // - If ext is .less: try as-is.
54
+ export function expandLessImportCandidates(importPath) {
55
+ const ext = path.extname(importPath);
56
+ if (ext !== '.less') {
57
+ return [`${importPath}.less`, `${importPath}`];
58
+ }
59
+ return [importPath];
60
+ }
61
+ // Mirrors current scss plugin behavior (no .sass support):
62
+ // - If ext is provided:
63
+ // - try explicit path
64
+ // - also underscore partial variant if basename isn't already underscored
65
+ // - Else try:
66
+ // - foo.scss
67
+ // - _foo.scss
68
+ // - foo/index.scss
69
+ // - foo/_index.scss
70
+ export function expandScssImportCandidates(importPath) {
71
+ const ext = path.extname(importPath);
72
+ const base = ext ? importPath.slice(0, -ext.length) : importPath;
73
+ const candidates = [];
74
+ const pushUnique = (p) => {
75
+ if (!candidates.includes(p)) {
76
+ candidates.push(p);
77
+ }
78
+ };
79
+ const withExt = (p) => (p.endsWith('.scss') ? p : `${p}.scss`);
80
+ if (ext) {
81
+ pushUnique(importPath);
82
+ const dir = path.dirname(importPath);
83
+ const file = path.basename(importPath);
84
+ if (!file.startsWith('_')) {
85
+ pushUnique(path.join(dir, `_${file}`));
86
+ }
87
+ return candidates;
88
+ }
89
+ pushUnique(withExt(base));
90
+ pushUnique(withExt(path.join(path.dirname(base), `_${path.basename(base)}`)));
91
+ pushUnique(withExt(path.join(base, 'index')));
92
+ pushUnique(withExt(path.join(base, '_index')));
93
+ return candidates;
94
+ }
95
+ export function resolveImport(fs, opts) {
96
+ const specifier = opts.specifier.trim();
97
+ if (!specifier) {
98
+ return null;
99
+ }
100
+ // Don’t resolve URLs here.
101
+ if (/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(specifier) || specifier.startsWith('data:')) {
102
+ return null;
103
+ }
104
+ const { filePart, suffix } = stripQueryHash(specifier);
105
+ const fromDir = path.dirname(opts.fromFilePath);
106
+ const cfg = opts.config ?? {};
107
+ const searchPaths = [fromDir];
108
+ if (opts.lang === 'less' && Array.isArray(cfg.includePaths)) {
109
+ searchPaths.push(...cfg.includePaths);
110
+ }
111
+ if (opts.lang === 'scss' && Array.isArray(cfg.loadPaths)) {
112
+ searchPaths.push(...cfg.loadPaths);
113
+ }
114
+ if (cfg.rootDir) {
115
+ searchPaths.push(cfg.rootDir);
116
+ }
117
+ const candidatesRel = opts.lang === 'less'
118
+ ? expandLessImportCandidates(filePart)
119
+ : opts.lang === 'scss'
120
+ ? expandScssImportCandidates(filePart)
121
+ : [filePart.endsWith('.css') ? filePart : `${filePart}.css`, filePart];
122
+ const candidatesAbs = [];
123
+ for (const baseDir of uniq(searchPaths)) {
124
+ for (const rel of candidatesRel) {
125
+ const abs = path.resolve(baseDir, rel);
126
+ candidatesAbs.push({ p: abs, resolvedBy: baseDir === fromDir ? 'exact' : 'loadPath' });
127
+ }
128
+ }
129
+ for (const c of candidatesAbs) {
130
+ if (fs.exists(c.p)) {
131
+ return { filePath: c.p + suffix, resolvedBy: c.resolvedBy };
132
+ }
133
+ }
134
+ return null;
135
+ }
136
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAoClC,SAAS,IAAI,CAAI,KAAU;IACzB,MAAM,GAAG,GAAQ,EAAE,CAAC;IACpB,MAAM,IAAI,GAAG,IAAI,GAAG,EAAK,CAAC;IAC1B,KAAK,MAAM,EAAE,IAAI,KAAK,EAAE,CAAC;QACvB,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC;YACjB,SAAS;QACX,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACb,GAAG,CA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AAQ,CAAC;YACtC,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,MAAM,EAAE,QAAQ,CAAC,CAAC;IAE7E,MAAM,aAAa,GAAkE,EAAE,CAAC;IACxF,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC;QACxC,KAAK,MAAM,GAAG,IAAI,aAAa,EAAE,CAAC;YAChC,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;YACvC,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,UAAU,EAAE,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC;QACzF,CAAC;IACH,CAAC;IAED,KAAK,MAAM,CAAC,IAAI,aAAa,EAAE,CAAC;QAC9B,IAAI,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YACnB,OAAO,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC,UAAU,EAAE,CAAC;QAC9D,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@jesscss/style-resolver",
3
+ "version": "2.0.0-alpha.2",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "type": "module",
8
+ "main": "lib/index.js",
9
+ "types": "lib/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./lib/index.js",
13
+ "types": "./lib/index.d.ts",
14
+ "source": "./src/index.ts"
15
+ }
16
+ },
17
+ "files": [
18
+ "lib"
19
+ ],
20
+ "devDependencies": {
21
+ "typescript": "~5.8.2",
22
+ "vitest": "^3.2.4"
23
+ },
24
+ "scripts": {
25
+ "build": "pnpm compile",
26
+ "compile": "tsc -p tsconfig.build.json",
27
+ "test": "vitest --watch=false",
28
+ "lint": "eslint '**/*.{js,ts}'"
29
+ }
30
+ }