@knighted/css 1.0.0-rc.7 → 1.0.0-rc.9
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/bin/generate-types.js +31 -0
- package/dist/cjs/css.cjs +73 -21
- package/dist/cjs/css.d.cts +5 -4
- package/dist/cjs/generateTypes.cjs +529 -0
- package/dist/cjs/generateTypes.d.cts +54 -0
- package/dist/cjs/loader.cjs +51 -27
- package/dist/cjs/loader.d.cts +1 -0
- package/dist/cjs/loaderInternals.cjs +38 -1
- package/dist/cjs/loaderInternals.d.cts +7 -0
- package/dist/cjs/moduleGraph.cjs +431 -0
- package/dist/cjs/moduleGraph.d.cts +15 -0
- package/dist/cjs/sassInternals.cjs +6 -3
- package/dist/cjs/sassInternals.d.cts +3 -4
- package/dist/cjs/stableNamespace.cjs +12 -0
- package/dist/cjs/stableNamespace.d.cts +3 -0
- package/dist/cjs/stableSelectorsLiteral.cjs +104 -0
- package/dist/cjs/stableSelectorsLiteral.d.cts +19 -0
- package/dist/cjs/types.cjs +2 -0
- package/dist/cjs/types.d.cts +4 -0
- package/dist/css.d.ts +5 -4
- package/dist/css.js +73 -21
- package/dist/generateTypes.d.ts +54 -0
- package/dist/generateTypes.js +521 -0
- package/dist/loader.d.ts +1 -0
- package/dist/loader.js +50 -26
- package/dist/loaderInternals.d.ts +7 -0
- package/dist/loaderInternals.js +33 -0
- package/dist/moduleGraph.d.ts +15 -0
- package/dist/moduleGraph.js +425 -0
- package/dist/sassInternals.d.ts +3 -4
- package/dist/sassInternals.js +6 -3
- package/dist/stableNamespace.d.ts +3 -0
- package/dist/stableNamespace.js +8 -0
- package/dist/stableSelectorsLiteral.d.ts +19 -0
- package/dist/stableSelectorsLiteral.js +98 -0
- package/dist/types.d.ts +4 -0
- package/dist/types.js +1 -0
- package/loader-queries.d.ts +28 -0
- package/package.json +23 -11
- package/types-stub/index.d.ts +5 -0
- package/types.d.ts +1 -0
package/dist/loaderInternals.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export const COMBINED_QUERY_FLAG = 'combined';
|
|
2
|
+
export const TYPES_QUERY_FLAG = 'types';
|
|
2
3
|
export const NAMED_ONLY_QUERY_FLAGS = ['named-only', 'no-default'];
|
|
3
4
|
export function splitQuery(query) {
|
|
4
5
|
const trimmed = query.startsWith('?') ? query.slice(1) : query;
|
|
@@ -25,6 +26,9 @@ export function buildSanitizedQuery(query) {
|
|
|
25
26
|
if (isQueryFlag(part, 'knighted-css')) {
|
|
26
27
|
return false;
|
|
27
28
|
}
|
|
29
|
+
if (isQueryFlag(part, TYPES_QUERY_FLAG)) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
28
32
|
if (NAMED_ONLY_QUERY_FLAGS.some(flag => isQueryFlag(part, flag))) {
|
|
29
33
|
return false;
|
|
30
34
|
}
|
|
@@ -32,6 +36,22 @@ export function buildSanitizedQuery(query) {
|
|
|
32
36
|
});
|
|
33
37
|
return entries.length > 0 ? `?${entries.join('&')}` : '';
|
|
34
38
|
}
|
|
39
|
+
export function hasQueryFlag(query, flag) {
|
|
40
|
+
if (!query)
|
|
41
|
+
return false;
|
|
42
|
+
const entries = splitQuery(query);
|
|
43
|
+
if (entries.length === 0)
|
|
44
|
+
return false;
|
|
45
|
+
return entries.some(part => isQueryFlag(part, flag));
|
|
46
|
+
}
|
|
47
|
+
function safeDecode(value) {
|
|
48
|
+
try {
|
|
49
|
+
return decodeURIComponent(value);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
35
55
|
export function shouldForwardDefaultExport(request) {
|
|
36
56
|
const [pathPart] = request.split('?');
|
|
37
57
|
if (!pathPart)
|
|
@@ -42,6 +62,18 @@ export function shouldForwardDefaultExport(request) {
|
|
|
42
62
|
}
|
|
43
63
|
return true;
|
|
44
64
|
}
|
|
65
|
+
export function hasCombinedQuery(query) {
|
|
66
|
+
return hasQueryFlag(query, COMBINED_QUERY_FLAG);
|
|
67
|
+
}
|
|
68
|
+
export function hasNamedOnlyQueryFlag(query) {
|
|
69
|
+
return NAMED_ONLY_QUERY_FLAGS.some(flag => hasQueryFlag(query, flag));
|
|
70
|
+
}
|
|
71
|
+
export function determineSelectorVariant(query) {
|
|
72
|
+
if (hasCombinedQuery(query)) {
|
|
73
|
+
return hasNamedOnlyQueryFlag(query) ? 'combinedWithoutDefault' : 'combined';
|
|
74
|
+
}
|
|
75
|
+
return 'types';
|
|
76
|
+
}
|
|
45
77
|
export function shouldEmitCombinedDefault(options) {
|
|
46
78
|
if (options.skipSyntheticDefault) {
|
|
47
79
|
return false;
|
|
@@ -60,4 +92,5 @@ export function shouldEmitCombinedDefault(options) {
|
|
|
60
92
|
export const __loaderInternals = {
|
|
61
93
|
buildSanitizedQuery,
|
|
62
94
|
shouldEmitCombinedDefault,
|
|
95
|
+
determineSelectorVariant,
|
|
63
96
|
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CssResolver } from './types.js';
|
|
2
|
+
export interface ModuleGraphOptions {
|
|
3
|
+
tsConfig?: string | Record<string, unknown>;
|
|
4
|
+
extensions?: string[];
|
|
5
|
+
conditions?: string[];
|
|
6
|
+
}
|
|
7
|
+
interface CollectOptions {
|
|
8
|
+
cwd: string;
|
|
9
|
+
styleExtensions: string[];
|
|
10
|
+
filter: (filePath: string) => boolean;
|
|
11
|
+
resolver?: CssResolver;
|
|
12
|
+
graphOptions?: ModuleGraphOptions;
|
|
13
|
+
}
|
|
14
|
+
export declare function collectStyleImports(entryPath: string, options: CollectOptions): Promise<string[]>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { builtinModules } from 'node:module';
|
|
3
|
+
import { existsSync, promises as fs, statSync } from 'node:fs';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { parseSync, Visitor } from 'oxc-parser';
|
|
6
|
+
import { ResolverFactory, } from 'oxc-resolver';
|
|
7
|
+
import { createMatchPath } from 'tsconfig-paths';
|
|
8
|
+
import { getTsconfig } from 'get-tsconfig';
|
|
9
|
+
const SCRIPT_EXTENSIONS = ['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs'];
|
|
10
|
+
const BUILTIN_SPECIFIERS = new Set([
|
|
11
|
+
...builtinModules,
|
|
12
|
+
...builtinModules.map(mod => `node:${mod}`),
|
|
13
|
+
]);
|
|
14
|
+
const tsconfigResultCache = new Map();
|
|
15
|
+
const tsconfigFsCache = new Map();
|
|
16
|
+
export async function collectStyleImports(entryPath, options) {
|
|
17
|
+
const { cwd, styleExtensions, filter, resolver, graphOptions } = options;
|
|
18
|
+
const normalizedStyles = normalizeExtensions(styleExtensions);
|
|
19
|
+
const scriptExtensions = normalizeExtensions([
|
|
20
|
+
...SCRIPT_EXTENSIONS,
|
|
21
|
+
...(graphOptions?.extensions ?? []),
|
|
22
|
+
]);
|
|
23
|
+
const resolutionExtensions = dedupeExtensions([
|
|
24
|
+
...scriptExtensions,
|
|
25
|
+
...normalizedStyles,
|
|
26
|
+
]);
|
|
27
|
+
const tsconfigMatcher = createTsconfigMatcher(graphOptions?.tsConfig, cwd, resolutionExtensions);
|
|
28
|
+
const seenScripts = new Set();
|
|
29
|
+
const seenStyles = new Set();
|
|
30
|
+
const styleOrder = [];
|
|
31
|
+
const resolutionCache = new Map();
|
|
32
|
+
const resolverFactory = createResolverFactory(cwd, resolutionExtensions, scriptExtensions, graphOptions);
|
|
33
|
+
async function walk(filePath) {
|
|
34
|
+
const absolutePath = path.resolve(filePath);
|
|
35
|
+
if (seenScripts.has(absolutePath)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
seenScripts.add(absolutePath);
|
|
39
|
+
const source = await readSourceFile(absolutePath);
|
|
40
|
+
if (!source) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const specifiers = extractModuleSpecifiers(source, absolutePath);
|
|
44
|
+
for (const specifier of specifiers) {
|
|
45
|
+
if (!specifier || isBuiltinSpecifier(specifier)) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const resolved = await resolveImport(specifier, absolutePath);
|
|
49
|
+
if (!resolved) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const normalized = path.resolve(resolved);
|
|
53
|
+
if (!filter(normalized)) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (isStyleExtension(normalized, normalizedStyles)) {
|
|
57
|
+
if (!seenStyles.has(normalized)) {
|
|
58
|
+
seenStyles.add(normalized);
|
|
59
|
+
styleOrder.push(normalized);
|
|
60
|
+
}
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (isScriptExtension(normalized, scriptExtensions)) {
|
|
64
|
+
await walk(normalized);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async function resolveImport(specifier, importer) {
|
|
69
|
+
const cacheKey = `${importer}::${specifier}`;
|
|
70
|
+
if (resolutionCache.has(cacheKey)) {
|
|
71
|
+
return resolutionCache.get(cacheKey);
|
|
72
|
+
}
|
|
73
|
+
let resolved;
|
|
74
|
+
if (resolver) {
|
|
75
|
+
resolved = normalizeResolverResult(await resolver(specifier, { cwd, from: importer }), cwd);
|
|
76
|
+
}
|
|
77
|
+
if (!resolved && tsconfigMatcher) {
|
|
78
|
+
resolved = tsconfigMatcher(specifier);
|
|
79
|
+
}
|
|
80
|
+
if (!resolved) {
|
|
81
|
+
resolved = resolveWithFactory(resolverFactory, specifier, importer, resolutionExtensions);
|
|
82
|
+
}
|
|
83
|
+
resolutionCache.set(cacheKey, resolved);
|
|
84
|
+
return resolved;
|
|
85
|
+
}
|
|
86
|
+
await walk(entryPath);
|
|
87
|
+
return styleOrder;
|
|
88
|
+
}
|
|
89
|
+
function normalizeExtensions(extensions) {
|
|
90
|
+
const result = new Set();
|
|
91
|
+
for (const ext of extensions) {
|
|
92
|
+
if (!ext)
|
|
93
|
+
continue;
|
|
94
|
+
const normalized = ext.startsWith('.') ? ext.toLowerCase() : `.${ext.toLowerCase()}`;
|
|
95
|
+
result.add(normalized);
|
|
96
|
+
}
|
|
97
|
+
return Array.from(result);
|
|
98
|
+
}
|
|
99
|
+
function dedupeExtensions(extensions) {
|
|
100
|
+
const result = new Set();
|
|
101
|
+
for (const ext of extensions) {
|
|
102
|
+
result.add(ext);
|
|
103
|
+
}
|
|
104
|
+
return Array.from(result);
|
|
105
|
+
}
|
|
106
|
+
function isBuiltinSpecifier(specifier) {
|
|
107
|
+
if (BUILTIN_SPECIFIERS.has(specifier)) {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
if (specifier.startsWith('node:')) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
function isStyleExtension(filePath, extensions) {
|
|
116
|
+
const lower = filePath.toLowerCase();
|
|
117
|
+
return extensions.some(ext => lower.endsWith(ext));
|
|
118
|
+
}
|
|
119
|
+
function isScriptExtension(filePath, extensions) {
|
|
120
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
121
|
+
return extensions.includes(ext);
|
|
122
|
+
}
|
|
123
|
+
async function readSourceFile(filePath) {
|
|
124
|
+
try {
|
|
125
|
+
return await fs.readFile(filePath, 'utf8');
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function extractModuleSpecifiers(sourceText, filePath) {
|
|
132
|
+
let program;
|
|
133
|
+
try {
|
|
134
|
+
;
|
|
135
|
+
({ program } = parseSync(filePath, sourceText, { sourceType: 'unambiguous' }));
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
return [];
|
|
139
|
+
}
|
|
140
|
+
const specifiers = [];
|
|
141
|
+
const addSpecifier = (raw) => {
|
|
142
|
+
if (!raw) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const normalized = normalizeSpecifier(raw);
|
|
146
|
+
if (normalized) {
|
|
147
|
+
specifiers.push(normalized);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
const visitor = new Visitor({
|
|
151
|
+
ImportDeclaration(node) {
|
|
152
|
+
addSpecifier(node.source?.value);
|
|
153
|
+
},
|
|
154
|
+
ExportNamedDeclaration(node) {
|
|
155
|
+
if (node.source) {
|
|
156
|
+
addSpecifier(node.source.value);
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
ExportAllDeclaration(node) {
|
|
160
|
+
addSpecifier(node.source?.value);
|
|
161
|
+
},
|
|
162
|
+
TSImportEqualsDeclaration(node) {
|
|
163
|
+
const specifier = extractImportEqualsSpecifier(node);
|
|
164
|
+
if (specifier) {
|
|
165
|
+
addSpecifier(specifier);
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
ImportExpression(node) {
|
|
169
|
+
const specifier = getStringFromExpression(node.source);
|
|
170
|
+
if (specifier) {
|
|
171
|
+
addSpecifier(specifier);
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
CallExpression(node) {
|
|
175
|
+
if (!isRequireLikeCallee(node.callee)) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const specifier = getStringFromArgument(node.arguments[0]);
|
|
179
|
+
if (specifier) {
|
|
180
|
+
addSpecifier(specifier);
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
visitor.visit(program);
|
|
185
|
+
return specifiers;
|
|
186
|
+
}
|
|
187
|
+
function normalizeSpecifier(raw) {
|
|
188
|
+
if (!raw)
|
|
189
|
+
return '';
|
|
190
|
+
const trimmed = raw.trim();
|
|
191
|
+
if (!trimmed || trimmed.startsWith('\0')) {
|
|
192
|
+
return '';
|
|
193
|
+
}
|
|
194
|
+
const queryIndex = trimmed.search(/[?#]/);
|
|
195
|
+
const withoutQuery = queryIndex === -1 ? trimmed : trimmed.slice(0, queryIndex);
|
|
196
|
+
if (!withoutQuery) {
|
|
197
|
+
return '';
|
|
198
|
+
}
|
|
199
|
+
if (/^[a-z][\w+.-]*:/i.test(withoutQuery) && !withoutQuery.startsWith('file:')) {
|
|
200
|
+
return '';
|
|
201
|
+
}
|
|
202
|
+
return withoutQuery;
|
|
203
|
+
}
|
|
204
|
+
function extractImportEqualsSpecifier(node) {
|
|
205
|
+
if (node.moduleReference.type === 'TSExternalModuleReference') {
|
|
206
|
+
return node.moduleReference.expression.value;
|
|
207
|
+
}
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
function getStringFromArgument(argument) {
|
|
211
|
+
if (!argument || argument.type === 'SpreadElement') {
|
|
212
|
+
return undefined;
|
|
213
|
+
}
|
|
214
|
+
return getStringFromExpression(argument);
|
|
215
|
+
}
|
|
216
|
+
function getStringFromExpression(expression) {
|
|
217
|
+
if (!expression) {
|
|
218
|
+
return undefined;
|
|
219
|
+
}
|
|
220
|
+
if (expression.type === 'Literal') {
|
|
221
|
+
const literalValue = expression.value;
|
|
222
|
+
return typeof literalValue === 'string' ? literalValue : undefined;
|
|
223
|
+
}
|
|
224
|
+
if (expression.type === 'TemplateLiteral' && expression.expressions.length === 0) {
|
|
225
|
+
const [first] = expression.quasis;
|
|
226
|
+
return first?.value.cooked ?? first?.value.raw ?? undefined;
|
|
227
|
+
}
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
function isRequireLikeCallee(expression) {
|
|
231
|
+
const target = unwrapExpression(expression);
|
|
232
|
+
if (target.type === 'Identifier') {
|
|
233
|
+
return target.name === 'require';
|
|
234
|
+
}
|
|
235
|
+
if (target.type === 'MemberExpression') {
|
|
236
|
+
const object = target.object;
|
|
237
|
+
if (object.type === 'Identifier') {
|
|
238
|
+
return object.name === 'require';
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
function unwrapExpression(expression) {
|
|
244
|
+
if (expression.type === 'ChainExpression') {
|
|
245
|
+
const inner = expression.expression;
|
|
246
|
+
if (inner.type === 'CallExpression') {
|
|
247
|
+
return unwrapExpression(inner.callee);
|
|
248
|
+
}
|
|
249
|
+
return unwrapExpression(inner);
|
|
250
|
+
}
|
|
251
|
+
if (expression.type === 'TSNonNullExpression') {
|
|
252
|
+
return unwrapExpression(expression.expression);
|
|
253
|
+
}
|
|
254
|
+
return expression;
|
|
255
|
+
}
|
|
256
|
+
function normalizeResolverResult(result, cwd) {
|
|
257
|
+
if (!result) {
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
if (result.startsWith('file://')) {
|
|
261
|
+
try {
|
|
262
|
+
return fileURLToPath(new URL(result));
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
return undefined;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return path.isAbsolute(result) ? result : path.resolve(cwd, result);
|
|
269
|
+
}
|
|
270
|
+
function resolveWithFactory(factory, specifier, importer, extensions) {
|
|
271
|
+
if (specifier.startsWith('file://')) {
|
|
272
|
+
try {
|
|
273
|
+
return findExistingFile(fileURLToPath(new URL(specifier)), extensions);
|
|
274
|
+
}
|
|
275
|
+
catch {
|
|
276
|
+
return undefined;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (/^[a-z][\w+.-]*:/i.test(specifier)) {
|
|
280
|
+
return undefined;
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
const result = factory.resolveFileSync(importer, specifier);
|
|
284
|
+
return result?.path;
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
return undefined;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
function createResolverFactory(cwd, extensions, scriptExtensions, graphOptions) {
|
|
291
|
+
const options = {
|
|
292
|
+
extensions,
|
|
293
|
+
conditionNames: graphOptions?.conditions,
|
|
294
|
+
};
|
|
295
|
+
const extensionAlias = buildExtensionAlias(scriptExtensions);
|
|
296
|
+
if (extensionAlias) {
|
|
297
|
+
options.extensionAlias = extensionAlias;
|
|
298
|
+
}
|
|
299
|
+
const tsconfigOption = resolveResolverTsconfig(graphOptions?.tsConfig, cwd);
|
|
300
|
+
if (tsconfigOption) {
|
|
301
|
+
options.tsconfig = tsconfigOption;
|
|
302
|
+
}
|
|
303
|
+
return new ResolverFactory(options);
|
|
304
|
+
}
|
|
305
|
+
function buildExtensionAlias(scriptExtensions) {
|
|
306
|
+
const alias = {};
|
|
307
|
+
const jsTargets = dedupeExtensions(scriptExtensions.filter(ext => ['.js', '.ts', '.tsx', '.mjs', '.cjs', '.mts', '.cts'].includes(ext)));
|
|
308
|
+
if (jsTargets.length > 0) {
|
|
309
|
+
for (const key of ['.js', '.mjs', '.cjs']) {
|
|
310
|
+
alias[key] = jsTargets;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
const jsxTargets = dedupeExtensions(scriptExtensions.filter(ext => ext === '.jsx' || ext === '.tsx'));
|
|
314
|
+
if (jsxTargets.length > 0) {
|
|
315
|
+
alias['.jsx'] = jsxTargets;
|
|
316
|
+
}
|
|
317
|
+
return Object.keys(alias).length > 0 ? alias : undefined;
|
|
318
|
+
}
|
|
319
|
+
function resolveResolverTsconfig(input, cwd) {
|
|
320
|
+
if (!input || typeof input !== 'string') {
|
|
321
|
+
return undefined;
|
|
322
|
+
}
|
|
323
|
+
const resolved = resolveTsconfigPath(input, cwd);
|
|
324
|
+
if (!resolved) {
|
|
325
|
+
return undefined;
|
|
326
|
+
}
|
|
327
|
+
return { configFile: resolved };
|
|
328
|
+
}
|
|
329
|
+
function createTsconfigMatcher(input, cwd, extensions) {
|
|
330
|
+
const config = loadTsconfigPaths(input, cwd);
|
|
331
|
+
if (!config) {
|
|
332
|
+
return undefined;
|
|
333
|
+
}
|
|
334
|
+
const matchPath = createMatchPath(config.absoluteBaseUrl, config.paths);
|
|
335
|
+
return (specifier) => {
|
|
336
|
+
const matched = matchPath(specifier, undefined, undefined, extensions);
|
|
337
|
+
if (!matched) {
|
|
338
|
+
return undefined;
|
|
339
|
+
}
|
|
340
|
+
return findExistingFile(matched, extensions) ?? matched;
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
function loadTsconfigPaths(input, cwd) {
|
|
344
|
+
if (!input) {
|
|
345
|
+
return undefined;
|
|
346
|
+
}
|
|
347
|
+
if (typeof input === 'string') {
|
|
348
|
+
const target = path.isAbsolute(input) ? input : path.resolve(cwd, input);
|
|
349
|
+
const cached = tsconfigResultCache.get(target);
|
|
350
|
+
if (cached !== undefined) {
|
|
351
|
+
return cached ?? undefined;
|
|
352
|
+
}
|
|
353
|
+
const result = getTsconfig(target, undefined, tsconfigFsCache);
|
|
354
|
+
if (!result) {
|
|
355
|
+
tsconfigResultCache.set(target, null);
|
|
356
|
+
return undefined;
|
|
357
|
+
}
|
|
358
|
+
const normalized = normalizeTsconfigCompilerOptions(result.config.compilerOptions, path.dirname(result.path));
|
|
359
|
+
tsconfigResultCache.set(target, normalized ?? null);
|
|
360
|
+
return normalized;
|
|
361
|
+
}
|
|
362
|
+
const compilerOptions = input.compilerOptions;
|
|
363
|
+
return normalizeTsconfigCompilerOptions(compilerOptions, cwd);
|
|
364
|
+
}
|
|
365
|
+
function normalizeTsconfigCompilerOptions(compilerOptions, configDir) {
|
|
366
|
+
if (!compilerOptions?.baseUrl || !compilerOptions.paths) {
|
|
367
|
+
return undefined;
|
|
368
|
+
}
|
|
369
|
+
const normalizedPaths = {};
|
|
370
|
+
for (const [pattern, replacements] of Object.entries(compilerOptions.paths)) {
|
|
371
|
+
if (!replacements || replacements.length === 0) {
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
normalizedPaths[pattern] = Array.isArray(replacements)
|
|
375
|
+
? [...replacements]
|
|
376
|
+
: [replacements];
|
|
377
|
+
}
|
|
378
|
+
if (Object.keys(normalizedPaths).length === 0) {
|
|
379
|
+
return undefined;
|
|
380
|
+
}
|
|
381
|
+
const absoluteBaseUrl = path.isAbsolute(compilerOptions.baseUrl)
|
|
382
|
+
? compilerOptions.baseUrl
|
|
383
|
+
: path.resolve(configDir, compilerOptions.baseUrl);
|
|
384
|
+
return { absoluteBaseUrl, paths: normalizedPaths };
|
|
385
|
+
}
|
|
386
|
+
function resolveTsconfigPath(tsconfigPath, cwd) {
|
|
387
|
+
const absolute = path.isAbsolute(tsconfigPath)
|
|
388
|
+
? tsconfigPath
|
|
389
|
+
: path.resolve(cwd, tsconfigPath);
|
|
390
|
+
if (!existsSync(absolute)) {
|
|
391
|
+
return undefined;
|
|
392
|
+
}
|
|
393
|
+
const stats = statSync(absolute);
|
|
394
|
+
if (stats.isDirectory()) {
|
|
395
|
+
const candidate = path.join(absolute, 'tsconfig.json');
|
|
396
|
+
return existsSync(candidate) ? candidate : undefined;
|
|
397
|
+
}
|
|
398
|
+
return absolute;
|
|
399
|
+
}
|
|
400
|
+
function findExistingFile(candidate, extensions) {
|
|
401
|
+
const candidateHasExt = hasExtension(candidate);
|
|
402
|
+
if (candidateHasExt && existsSync(candidate)) {
|
|
403
|
+
return candidate;
|
|
404
|
+
}
|
|
405
|
+
if (!candidateHasExt) {
|
|
406
|
+
for (const ext of extensions) {
|
|
407
|
+
const withExt = `${candidate}${ext}`;
|
|
408
|
+
if (existsSync(withExt)) {
|
|
409
|
+
return withExt;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (existsSync(candidate) && statSync(candidate).isDirectory()) {
|
|
414
|
+
for (const ext of extensions) {
|
|
415
|
+
const indexPath = path.join(candidate, `index${ext}`);
|
|
416
|
+
if (existsSync(indexPath)) {
|
|
417
|
+
return indexPath;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return undefined;
|
|
422
|
+
}
|
|
423
|
+
function hasExtension(filePath) {
|
|
424
|
+
return Boolean(path.extname(filePath));
|
|
425
|
+
}
|
package/dist/sassInternals.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
}) => string | Promise<string | undefined>;
|
|
1
|
+
import type { CssResolver } from './types.js';
|
|
2
|
+
export type { CssResolver } from './types.js';
|
|
4
3
|
export declare function createSassImporter({ cwd, resolver, }: {
|
|
5
4
|
cwd: string;
|
|
6
5
|
resolver?: CssResolver;
|
|
@@ -13,7 +12,7 @@ export declare function createSassImporter({ cwd, resolver, }: {
|
|
|
13
12
|
syntax: "scss" | "indented";
|
|
14
13
|
}>;
|
|
15
14
|
} | undefined;
|
|
16
|
-
export declare function resolveAliasSpecifier(specifier: string, resolver: CssResolver, cwd: string): Promise<string | undefined>;
|
|
15
|
+
export declare function resolveAliasSpecifier(specifier: string, resolver: CssResolver, cwd: string, from?: string): Promise<string | undefined>;
|
|
17
16
|
export declare function shouldNormalizeSpecifier(specifier: string): boolean;
|
|
18
17
|
export declare function ensureSassPath(filePath: string): string | undefined;
|
|
19
18
|
export declare function resolveRelativeSpecifier(specifier: string, containingUrl?: URL | null): string | undefined;
|
package/dist/sassInternals.js
CHANGED
|
@@ -13,8 +13,11 @@ export function createSassImporter({ cwd, resolver, }) {
|
|
|
13
13
|
console.error('[knighted-css:sass] containing url:', context.containingUrl.href);
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
+
const containingPath = context?.containingUrl
|
|
17
|
+
? fileURLToPath(context.containingUrl)
|
|
18
|
+
: undefined;
|
|
16
19
|
if (shouldNormalizeSpecifier(url)) {
|
|
17
|
-
const resolvedPath = await resolveAliasSpecifier(url, resolver, cwd);
|
|
20
|
+
const resolvedPath = await resolveAliasSpecifier(url, resolver, cwd, containingPath);
|
|
18
21
|
if (!resolvedPath) {
|
|
19
22
|
if (debug) {
|
|
20
23
|
console.error('[knighted-css:sass] resolver returned no result for', url);
|
|
@@ -50,8 +53,8 @@ export function createSassImporter({ cwd, resolver, }) {
|
|
|
50
53
|
},
|
|
51
54
|
};
|
|
52
55
|
}
|
|
53
|
-
export async function resolveAliasSpecifier(specifier, resolver, cwd) {
|
|
54
|
-
const resolved = await resolver(specifier, { cwd });
|
|
56
|
+
export async function resolveAliasSpecifier(specifier, resolver, cwd, from) {
|
|
57
|
+
const resolved = await resolver(specifier, { cwd, from });
|
|
55
58
|
if (!resolved) {
|
|
56
59
|
return undefined;
|
|
57
60
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface StableSelectorsLiteralResult {
|
|
2
|
+
literal: string;
|
|
3
|
+
selectorMap: Map<string, string>;
|
|
4
|
+
}
|
|
5
|
+
export declare function buildStableSelectorsLiteral(options: {
|
|
6
|
+
css: string;
|
|
7
|
+
namespace: string;
|
|
8
|
+
resourcePath: string;
|
|
9
|
+
emitWarning: (message: string) => void;
|
|
10
|
+
}): StableSelectorsLiteralResult;
|
|
11
|
+
export declare function collectStableSelectors(css: string, namespace: string, filename?: string): Map<string, string>;
|
|
12
|
+
declare function collectStableSelectorsByRegex(css: string, namespace: string): Map<string, string>;
|
|
13
|
+
export declare function formatStableSelectorMap(map: Map<string, string>): string;
|
|
14
|
+
export declare const __stableSelectorsLiteralInternals: {
|
|
15
|
+
collectStableSelectors: typeof collectStableSelectors;
|
|
16
|
+
collectStableSelectorsByRegex: typeof collectStableSelectorsByRegex;
|
|
17
|
+
formatStableSelectorMap: typeof formatStableSelectorMap;
|
|
18
|
+
};
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { transform as lightningTransform } from 'lightningcss';
|
|
2
|
+
import { escapeRegex, serializeSelector } from './helpers.js';
|
|
3
|
+
export function buildStableSelectorsLiteral(options) {
|
|
4
|
+
const trimmedNamespace = options.namespace.trim();
|
|
5
|
+
if (!trimmedNamespace) {
|
|
6
|
+
options.emitWarning(`stableSelectors requested for ${options.resourcePath} but "stableNamespace" resolved to an empty value.`);
|
|
7
|
+
return {
|
|
8
|
+
literal: 'export const stableSelectors = {} as const;\n',
|
|
9
|
+
selectorMap: new Map(),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
const selectorMap = collectStableSelectors(options.css, trimmedNamespace, options.resourcePath);
|
|
13
|
+
if (selectorMap.size === 0) {
|
|
14
|
+
options.emitWarning(`stableSelectors requested for ${options.resourcePath} but no selectors matched namespace "${trimmedNamespace}".`);
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
literal: `export const stableSelectors = ${formatStableSelectorMap(selectorMap)} as const;\n`,
|
|
18
|
+
selectorMap,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export function collectStableSelectors(css, namespace, filename) {
|
|
22
|
+
if (!namespace)
|
|
23
|
+
return new Map();
|
|
24
|
+
const astResult = collectStableSelectorsFromAst(css, namespace, filename);
|
|
25
|
+
if (astResult) {
|
|
26
|
+
return astResult;
|
|
27
|
+
}
|
|
28
|
+
return collectStableSelectorsByRegex(css, namespace);
|
|
29
|
+
}
|
|
30
|
+
function collectStableSelectorsFromAst(css, namespace, filename) {
|
|
31
|
+
try {
|
|
32
|
+
const tokens = new Map();
|
|
33
|
+
const escaped = escapeRegex(namespace);
|
|
34
|
+
const pattern = new RegExp(`\\.${escaped}-([A-Za-z0-9_-]+)`, 'g');
|
|
35
|
+
lightningTransform({
|
|
36
|
+
filename: filename ?? 'knighted-types-probe.css',
|
|
37
|
+
code: Buffer.from(css),
|
|
38
|
+
minify: false,
|
|
39
|
+
visitor: {
|
|
40
|
+
Rule: {
|
|
41
|
+
style(rule) {
|
|
42
|
+
const target = Array.isArray(rule?.selectors)
|
|
43
|
+
? rule
|
|
44
|
+
: rule?.value && Array.isArray(rule.value.selectors)
|
|
45
|
+
? rule.value
|
|
46
|
+
: undefined;
|
|
47
|
+
if (!target)
|
|
48
|
+
return rule;
|
|
49
|
+
for (const selector of target.selectors) {
|
|
50
|
+
const selectorStr = serializeSelector(selector);
|
|
51
|
+
pattern.lastIndex = 0;
|
|
52
|
+
let match;
|
|
53
|
+
while ((match = pattern.exec(selectorStr)) !== null) {
|
|
54
|
+
const token = match[1];
|
|
55
|
+
if (!token)
|
|
56
|
+
continue;
|
|
57
|
+
tokens.set(token, `${namespace}-${token}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return rule;
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
return tokens;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function collectStableSelectorsByRegex(css, namespace) {
|
|
72
|
+
const escaped = escapeRegex(namespace);
|
|
73
|
+
const pattern = new RegExp(`\\.${escaped}-([A-Za-z0-9_-]+)`, 'g');
|
|
74
|
+
const tokens = new Map();
|
|
75
|
+
let match;
|
|
76
|
+
while ((match = pattern.exec(css)) !== null) {
|
|
77
|
+
const token = match[1];
|
|
78
|
+
if (!token)
|
|
79
|
+
continue;
|
|
80
|
+
tokens.set(token, `${namespace}-${token}`);
|
|
81
|
+
}
|
|
82
|
+
return tokens;
|
|
83
|
+
}
|
|
84
|
+
export function formatStableSelectorMap(map) {
|
|
85
|
+
if (map.size === 0) {
|
|
86
|
+
return 'Object.freeze({})';
|
|
87
|
+
}
|
|
88
|
+
const entries = Array.from(map.entries()).sort(([a], [b]) => a.localeCompare(b));
|
|
89
|
+
const lines = entries.map(([token, selector]) => {
|
|
90
|
+
return ` ${JSON.stringify(token)}: ${JSON.stringify(selector)}`;
|
|
91
|
+
});
|
|
92
|
+
return `Object.freeze({\n${lines.join(',\n')}\n})`;
|
|
93
|
+
}
|
|
94
|
+
export const __stableSelectorsLiteralInternals = {
|
|
95
|
+
collectStableSelectors,
|
|
96
|
+
collectStableSelectorsByRegex,
|
|
97
|
+
formatStableSelectorMap,
|
|
98
|
+
};
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|