@knighted/css 1.0.0-rc.1 → 1.0.0-rc.11
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 +100 -22
- package/dist/cjs/css.d.cts +5 -6
- package/dist/cjs/generateTypes.cjs +683 -0
- package/dist/cjs/generateTypes.d.cts +108 -0
- package/dist/cjs/loader.cjs +65 -55
- package/dist/cjs/loader.d.cts +1 -0
- package/dist/cjs/loaderInternals.cjs +108 -0
- package/dist/cjs/loaderInternals.d.cts +23 -0
- package/dist/cjs/moduleGraph.cjs +431 -0
- package/dist/cjs/moduleGraph.d.cts +15 -0
- package/dist/cjs/moduleInfo.cjs +62 -0
- package/dist/cjs/moduleInfo.d.cts +10 -0
- package/dist/cjs/sassInternals.cjs +135 -0
- package/dist/cjs/sassInternals.d.cts +25 -0
- package/dist/cjs/stableNamespace.cjs +12 -0
- package/dist/cjs/stableNamespace.d.cts +3 -0
- package/dist/cjs/stableSelectors.cjs +44 -0
- package/dist/cjs/stableSelectors.d.cts +13 -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 -6
- package/dist/css.js +101 -23
- package/dist/generateTypes.d.ts +108 -0
- package/dist/generateTypes.js +675 -0
- package/dist/loader.d.ts +1 -0
- package/dist/loader.js +63 -53
- package/dist/loaderInternals.d.ts +23 -0
- package/dist/loaderInternals.js +96 -0
- package/dist/moduleGraph.d.ts +15 -0
- package/dist/moduleGraph.js +425 -0
- package/dist/moduleInfo.d.ts +10 -0
- package/dist/moduleInfo.js +55 -0
- package/dist/sassInternals.d.ts +25 -0
- package/dist/sassInternals.js +124 -0
- package/dist/stableNamespace.d.ts +3 -0
- package/dist/stableNamespace.js +8 -0
- package/dist/stableSelectors.d.ts +13 -0
- package/dist/stableSelectors.js +36 -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 +61 -0
- package/package.json +58 -8
- package/stable/_index.scss +57 -0
- package/stable/stable.css +15 -0
- package/types-stub/index.d.ts +5 -0
- package/types.d.ts +4 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { parse } from 'es-module-lexer';
|
|
2
|
+
export type ModuleDefaultSignal = 'has-default' | 'no-default' | 'unknown';
|
|
3
|
+
type LexerOverrides = {
|
|
4
|
+
parse?: typeof parse;
|
|
5
|
+
};
|
|
6
|
+
export declare function detectModuleDefaultExport(filePath: string): Promise<ModuleDefaultSignal>;
|
|
7
|
+
export declare const __moduleInfoInternals: {
|
|
8
|
+
setLexerOverrides(overrides?: LexerOverrides): void;
|
|
9
|
+
};
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { init, parse } from 'es-module-lexer';
|
|
4
|
+
const DETECTABLE_EXTENSIONS = new Set([
|
|
5
|
+
'.js',
|
|
6
|
+
'.jsx',
|
|
7
|
+
'.ts',
|
|
8
|
+
'.tsx',
|
|
9
|
+
'.mjs',
|
|
10
|
+
'.mts',
|
|
11
|
+
'.cjs',
|
|
12
|
+
'.cts',
|
|
13
|
+
]);
|
|
14
|
+
let lexerInit;
|
|
15
|
+
let lexerOverrides;
|
|
16
|
+
function ensureLexerInitialized() {
|
|
17
|
+
if (!lexerInit) {
|
|
18
|
+
lexerInit = init;
|
|
19
|
+
}
|
|
20
|
+
return lexerInit;
|
|
21
|
+
}
|
|
22
|
+
export async function detectModuleDefaultExport(filePath) {
|
|
23
|
+
if (!DETECTABLE_EXTENSIONS.has(path.extname(filePath))) {
|
|
24
|
+
return 'unknown';
|
|
25
|
+
}
|
|
26
|
+
let source;
|
|
27
|
+
try {
|
|
28
|
+
source = await readFile(filePath, 'utf8');
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return 'unknown';
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
await ensureLexerInitialized();
|
|
35
|
+
const [, exports] = (lexerOverrides?.parse ?? parse)(source, filePath);
|
|
36
|
+
if (exports.some(entry => entry.n === 'default')) {
|
|
37
|
+
return 'has-default';
|
|
38
|
+
}
|
|
39
|
+
if (exports.length === 0) {
|
|
40
|
+
return 'unknown';
|
|
41
|
+
}
|
|
42
|
+
return 'no-default';
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return 'unknown';
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export const __moduleInfoInternals = {
|
|
49
|
+
setLexerOverrides(overrides) {
|
|
50
|
+
lexerOverrides = overrides;
|
|
51
|
+
if (!overrides) {
|
|
52
|
+
lexerInit = undefined;
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { CssResolver } from './types.js';
|
|
2
|
+
export type { CssResolver } from './types.js';
|
|
3
|
+
export declare function createSassImporter({ cwd, resolver, }: {
|
|
4
|
+
cwd: string;
|
|
5
|
+
resolver?: CssResolver;
|
|
6
|
+
}): {
|
|
7
|
+
canonicalize(url: string, context?: {
|
|
8
|
+
containingUrl?: URL | null;
|
|
9
|
+
}): Promise<import("node:url").URL | null>;
|
|
10
|
+
load(canonicalUrl: URL): Promise<{
|
|
11
|
+
contents: string;
|
|
12
|
+
syntax: "scss" | "indented";
|
|
13
|
+
}>;
|
|
14
|
+
} | undefined;
|
|
15
|
+
export declare function resolveAliasSpecifier(specifier: string, resolver: CssResolver, cwd: string, from?: string): Promise<string | undefined>;
|
|
16
|
+
export declare function shouldNormalizeSpecifier(specifier: string): boolean;
|
|
17
|
+
export declare function ensureSassPath(filePath: string): string | undefined;
|
|
18
|
+
export declare function resolveRelativeSpecifier(specifier: string, containingUrl?: URL | null): string | undefined;
|
|
19
|
+
export declare const __sassInternals: {
|
|
20
|
+
createSassImporter: typeof createSassImporter;
|
|
21
|
+
resolveAliasSpecifier: typeof resolveAliasSpecifier;
|
|
22
|
+
shouldNormalizeSpecifier: typeof shouldNormalizeSpecifier;
|
|
23
|
+
ensureSassPath: typeof ensureSassPath;
|
|
24
|
+
resolveRelativeSpecifier: typeof resolveRelativeSpecifier;
|
|
25
|
+
};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { existsSync, promises as fs } from 'node:fs';
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
4
|
+
export function createSassImporter({ cwd, resolver, }) {
|
|
5
|
+
if (!resolver)
|
|
6
|
+
return undefined;
|
|
7
|
+
const debug = process.env.KNIGHTED_CSS_DEBUG_SASS === '1';
|
|
8
|
+
return {
|
|
9
|
+
async canonicalize(url, context) {
|
|
10
|
+
if (debug) {
|
|
11
|
+
console.error('[knighted-css:sass] canonicalize request:', url);
|
|
12
|
+
if (context?.containingUrl) {
|
|
13
|
+
console.error('[knighted-css:sass] containing url:', context.containingUrl.href);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const containingPath = context?.containingUrl
|
|
17
|
+
? fileURLToPath(context.containingUrl)
|
|
18
|
+
: undefined;
|
|
19
|
+
if (shouldNormalizeSpecifier(url)) {
|
|
20
|
+
const resolvedPath = await resolveAliasSpecifier(url, resolver, cwd, containingPath);
|
|
21
|
+
if (!resolvedPath) {
|
|
22
|
+
if (debug) {
|
|
23
|
+
console.error('[knighted-css:sass] resolver returned no result for', url);
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
const fileUrl = pathToFileURL(resolvedPath);
|
|
28
|
+
if (debug) {
|
|
29
|
+
console.error('[knighted-css:sass] canonical url:', fileUrl.href);
|
|
30
|
+
}
|
|
31
|
+
return fileUrl;
|
|
32
|
+
}
|
|
33
|
+
const relativePath = resolveRelativeSpecifier(url, context?.containingUrl);
|
|
34
|
+
if (relativePath) {
|
|
35
|
+
const fileUrl = pathToFileURL(relativePath);
|
|
36
|
+
if (debug) {
|
|
37
|
+
console.error('[knighted-css:sass] canonical url:', fileUrl.href);
|
|
38
|
+
}
|
|
39
|
+
return fileUrl;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
},
|
|
43
|
+
async load(canonicalUrl) {
|
|
44
|
+
if (debug) {
|
|
45
|
+
console.error('[knighted-css:sass] load request:', canonicalUrl.href);
|
|
46
|
+
}
|
|
47
|
+
const filePath = fileURLToPath(canonicalUrl);
|
|
48
|
+
const contents = await fs.readFile(filePath, 'utf8');
|
|
49
|
+
return {
|
|
50
|
+
contents,
|
|
51
|
+
syntax: inferSassSyntax(filePath),
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export async function resolveAliasSpecifier(specifier, resolver, cwd, from) {
|
|
57
|
+
const resolved = await resolver(specifier, { cwd, from });
|
|
58
|
+
if (!resolved) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
if (resolved.startsWith('file://')) {
|
|
62
|
+
return ensureSassPath(fileURLToPath(new URL(resolved)));
|
|
63
|
+
}
|
|
64
|
+
const normalized = path.isAbsolute(resolved) ? resolved : path.resolve(cwd, resolved);
|
|
65
|
+
return ensureSassPath(normalized);
|
|
66
|
+
}
|
|
67
|
+
export function shouldNormalizeSpecifier(specifier) {
|
|
68
|
+
const schemeMatch = specifier.match(/^([a-z][\w+.-]*):/i);
|
|
69
|
+
if (!schemeMatch) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
const scheme = schemeMatch[1].toLowerCase();
|
|
73
|
+
if (scheme === 'file' ||
|
|
74
|
+
scheme === 'http' ||
|
|
75
|
+
scheme === 'https' ||
|
|
76
|
+
scheme === 'data' ||
|
|
77
|
+
scheme === 'sass') {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
function inferSassSyntax(filePath) {
|
|
83
|
+
return filePath.endsWith('.sass') ? 'indented' : 'scss';
|
|
84
|
+
}
|
|
85
|
+
export function ensureSassPath(filePath) {
|
|
86
|
+
if (existsSync(filePath)) {
|
|
87
|
+
return filePath;
|
|
88
|
+
}
|
|
89
|
+
const ext = path.extname(filePath);
|
|
90
|
+
const dir = path.dirname(filePath);
|
|
91
|
+
const base = path.basename(filePath, ext);
|
|
92
|
+
const partialCandidate = path.join(dir, `_${base}${ext}`);
|
|
93
|
+
if (ext && existsSync(partialCandidate)) {
|
|
94
|
+
return partialCandidate;
|
|
95
|
+
}
|
|
96
|
+
const indexCandidate = path.join(dir, base, `index${ext}`);
|
|
97
|
+
if (ext && existsSync(indexCandidate)) {
|
|
98
|
+
return indexCandidate;
|
|
99
|
+
}
|
|
100
|
+
const partialIndexCandidate = path.join(dir, base, `_index${ext}`);
|
|
101
|
+
if (ext && existsSync(partialIndexCandidate)) {
|
|
102
|
+
return partialIndexCandidate;
|
|
103
|
+
}
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
export function resolveRelativeSpecifier(specifier, containingUrl) {
|
|
107
|
+
if (!containingUrl || containingUrl.protocol !== 'file:') {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
if (/^[a-z][\w+.-]*:/i.test(specifier)) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
const containingPath = fileURLToPath(containingUrl);
|
|
114
|
+
const baseDir = path.dirname(containingPath);
|
|
115
|
+
const candidate = path.resolve(baseDir, specifier);
|
|
116
|
+
return ensureSassPath(candidate);
|
|
117
|
+
}
|
|
118
|
+
export const __sassInternals = {
|
|
119
|
+
createSassImporter,
|
|
120
|
+
resolveAliasSpecifier,
|
|
121
|
+
shouldNormalizeSpecifier,
|
|
122
|
+
ensureSassPath,
|
|
123
|
+
resolveRelativeSpecifier,
|
|
124
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface StableSelectorOptions {
|
|
2
|
+
namespace?: string;
|
|
3
|
+
}
|
|
4
|
+
export interface StableClassNameOptions extends StableSelectorOptions {
|
|
5
|
+
token?: string;
|
|
6
|
+
join?: (values: string[]) => string;
|
|
7
|
+
}
|
|
8
|
+
export declare function stableToken(token: string, options?: StableSelectorOptions): string;
|
|
9
|
+
export declare function stableClass(token: string, options?: StableSelectorOptions): string;
|
|
10
|
+
export declare function stableSelector(token: string, options?: StableSelectorOptions): string;
|
|
11
|
+
export declare function createStableClassFactory(options?: StableSelectorOptions): (token: string) => string;
|
|
12
|
+
export declare function stableClassName<T extends Record<string, string>>(styles: T, key: keyof T | string, options?: StableClassNameOptions): string;
|
|
13
|
+
export declare const stableClassFromModule: typeof stableClassName;
|