@knighted/css 1.0.0-rc.8 → 1.0.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.
- package/dist/cjs/css.cjs +19 -15
- package/dist/cjs/css.d.cts +5 -4
- package/dist/cjs/generateTypes.cjs +214 -139
- package/dist/cjs/generateTypes.d.cts +65 -16
- package/dist/cjs/loader-helpers.cjs +6 -0
- package/dist/cjs/loader-helpers.d.cts +4 -0
- package/dist/cjs/loader.cjs +2 -0
- package/dist/cjs/loader.d.cts +2 -1
- package/dist/cjs/moduleGraph.cjs +432 -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/stableSelectors.cjs +29 -0
- package/dist/cjs/stableSelectors.d.cts +15 -0
- package/dist/cjs/stableSelectorsLiteral.cjs +8 -5
- package/dist/cjs/stableSelectorsLiteral.d.cts +2 -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 +19 -15
- package/dist/generateTypes.d.ts +65 -16
- package/dist/generateTypes.js +214 -139
- package/dist/loader-helpers.d.ts +4 -0
- package/dist/loader-helpers.js +3 -0
- package/dist/loader.d.ts +2 -1
- package/dist/loader.js +2 -0
- package/dist/moduleGraph.d.ts +15 -0
- package/dist/moduleGraph.js +426 -0
- package/dist/sassInternals.d.ts +3 -4
- package/dist/sassInternals.js +6 -3
- package/dist/stableSelectors.d.ts +15 -0
- package/dist/stableSelectors.js +28 -0
- package/dist/stableSelectorsLiteral.d.ts +2 -0
- package/dist/stableSelectorsLiteral.js +8 -5
- package/dist/types.d.ts +4 -0
- package/dist/types.js +1 -0
- package/loader-queries.d.ts +6 -1
- package/package.json +14 -8
|
@@ -6,8 +6,10 @@ exports.stableClass = stableClass;
|
|
|
6
6
|
exports.stableSelector = stableSelector;
|
|
7
7
|
exports.createStableClassFactory = createStableClassFactory;
|
|
8
8
|
exports.stableClassName = stableClassName;
|
|
9
|
+
exports.mergeStableClass = mergeStableClass;
|
|
9
10
|
const DEFAULT_NAMESPACE = 'knighted';
|
|
10
11
|
const defaultJoin = (values) => values.filter(Boolean).join(' ');
|
|
12
|
+
const toArray = (value) => Array.isArray(value) ? value : [value];
|
|
11
13
|
const normalizeToken = (token) => {
|
|
12
14
|
const sanitized = token
|
|
13
15
|
.trim()
|
|
@@ -42,3 +44,30 @@ function stableClassName(styles, key, options) {
|
|
|
42
44
|
return join([hashed, stable]);
|
|
43
45
|
}
|
|
44
46
|
exports.stableClassFromModule = stableClassName;
|
|
47
|
+
function mergeStableClass(input) {
|
|
48
|
+
if ('token' in input) {
|
|
49
|
+
return mergeSingle(input);
|
|
50
|
+
}
|
|
51
|
+
return mergeBatch(input);
|
|
52
|
+
}
|
|
53
|
+
function mergeSingle(input) {
|
|
54
|
+
const join = input.join ?? defaultJoin;
|
|
55
|
+
const hashed = toArray(input.hashed);
|
|
56
|
+
const stable = input.selector?.trim().length
|
|
57
|
+
? input.selector
|
|
58
|
+
: stableClass(input.token, { namespace: input.namespace });
|
|
59
|
+
return join([...hashed, stable]);
|
|
60
|
+
}
|
|
61
|
+
function mergeBatch(input) {
|
|
62
|
+
const join = input.join ?? defaultJoin;
|
|
63
|
+
const output = {};
|
|
64
|
+
for (const key of Object.keys(input.hashed)) {
|
|
65
|
+
const hashedValue = input.hashed[key];
|
|
66
|
+
const selector = input.selectors?.[String(key)];
|
|
67
|
+
const stable = selector?.trim().length
|
|
68
|
+
? selector
|
|
69
|
+
: stableClass(String(key), { namespace: input.namespace });
|
|
70
|
+
output[key] = join([...toArray(hashedValue), stable]);
|
|
71
|
+
}
|
|
72
|
+
return output;
|
|
73
|
+
}
|
|
@@ -5,9 +5,24 @@ export interface StableClassNameOptions extends StableSelectorOptions {
|
|
|
5
5
|
token?: string;
|
|
6
6
|
join?: (values: string[]) => string;
|
|
7
7
|
}
|
|
8
|
+
export interface MergeStableClassSingleInput extends StableSelectorOptions {
|
|
9
|
+
hashed: string | string[];
|
|
10
|
+
selector?: string;
|
|
11
|
+
token: string;
|
|
12
|
+
join?: (values: string[]) => string;
|
|
13
|
+
}
|
|
14
|
+
export interface MergeStableClassBatchInput<Hashed extends Record<string, string | string[]>, Selectors extends Record<string, string> | undefined = Record<string, string>> extends StableSelectorOptions {
|
|
15
|
+
hashed: Hashed;
|
|
16
|
+
selectors?: Selectors;
|
|
17
|
+
join?: (values: string[]) => string;
|
|
18
|
+
}
|
|
8
19
|
export declare function stableToken(token: string, options?: StableSelectorOptions): string;
|
|
9
20
|
export declare function stableClass(token: string, options?: StableSelectorOptions): string;
|
|
10
21
|
export declare function stableSelector(token: string, options?: StableSelectorOptions): string;
|
|
11
22
|
export declare function createStableClassFactory(options?: StableSelectorOptions): (token: string) => string;
|
|
12
23
|
export declare function stableClassName<T extends Record<string, string>>(styles: T, key: keyof T | string, options?: StableClassNameOptions): string;
|
|
13
24
|
export declare const stableClassFromModule: typeof stableClassName;
|
|
25
|
+
export declare function mergeStableClass(input: MergeStableClassSingleInput): string;
|
|
26
|
+
export declare function mergeStableClass<Hashed extends Record<string, string | string[]>>(input: MergeStableClassBatchInput<Hashed>): {
|
|
27
|
+
[Key in keyof Hashed]: string;
|
|
28
|
+
};
|
|
@@ -7,20 +7,23 @@ exports.formatStableSelectorMap = formatStableSelectorMap;
|
|
|
7
7
|
const lightningcss_1 = require("lightningcss");
|
|
8
8
|
const helpers_js_1 = require("./helpers.cjs");
|
|
9
9
|
function buildStableSelectorsLiteral(options) {
|
|
10
|
+
const target = options.target ?? 'ts';
|
|
10
11
|
const trimmedNamespace = options.namespace.trim();
|
|
11
12
|
if (!trimmedNamespace) {
|
|
12
13
|
options.emitWarning(`stableSelectors requested for ${options.resourcePath} but "stableNamespace" resolved to an empty value.`);
|
|
13
|
-
return
|
|
14
|
-
literal: 'export const stableSelectors = {} as const;\n',
|
|
15
|
-
selectorMap: new Map(),
|
|
16
|
-
};
|
|
14
|
+
return finalizeLiteral(new Map(), target);
|
|
17
15
|
}
|
|
18
16
|
const selectorMap = collectStableSelectors(options.css, trimmedNamespace, options.resourcePath);
|
|
19
17
|
if (selectorMap.size === 0) {
|
|
20
18
|
options.emitWarning(`stableSelectors requested for ${options.resourcePath} but no selectors matched namespace "${trimmedNamespace}".`);
|
|
21
19
|
}
|
|
20
|
+
return finalizeLiteral(selectorMap, target);
|
|
21
|
+
}
|
|
22
|
+
function finalizeLiteral(selectorMap, target) {
|
|
23
|
+
const formatted = formatStableSelectorMap(selectorMap);
|
|
24
|
+
const suffix = target === 'ts' ? ' as const' : '';
|
|
22
25
|
return {
|
|
23
|
-
literal: `export const stableSelectors = ${
|
|
26
|
+
literal: `export const stableSelectors = ${formatted}${suffix};\n`,
|
|
24
27
|
selectorMap,
|
|
25
28
|
};
|
|
26
29
|
}
|
|
@@ -2,11 +2,13 @@ export interface StableSelectorsLiteralResult {
|
|
|
2
2
|
literal: string;
|
|
3
3
|
selectorMap: Map<string, string>;
|
|
4
4
|
}
|
|
5
|
+
type StableSelectorsLiteralTarget = 'ts' | 'js';
|
|
5
6
|
export declare function buildStableSelectorsLiteral(options: {
|
|
6
7
|
css: string;
|
|
7
8
|
namespace: string;
|
|
8
9
|
resourcePath: string;
|
|
9
10
|
emitWarning: (message: string) => void;
|
|
11
|
+
target?: StableSelectorsLiteralTarget;
|
|
10
12
|
}): StableSelectorsLiteralResult;
|
|
11
13
|
export declare function collectStableSelectors(css: string, namespace: string, filename?: string): Map<string, string>;
|
|
12
14
|
declare function collectStableSelectorsByRegex(css: string, namespace: string): Map<string, string>;
|
package/dist/css.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import type { Options as DependencyTreeOpts } from 'dependency-tree';
|
|
2
1
|
import { type TransformOptions as LightningTransformOptions } from 'lightningcss';
|
|
3
2
|
import { type SpecificitySelector, type SpecificityStrategy } from './helpers.js';
|
|
4
|
-
import type {
|
|
5
|
-
|
|
3
|
+
import type { ModuleGraphOptions } from './moduleGraph.js';
|
|
4
|
+
import type { CssResolver } from './types.js';
|
|
5
|
+
export type { CssResolver } from './types.js';
|
|
6
|
+
export type { ModuleGraphOptions } from './moduleGraph.js';
|
|
6
7
|
export declare const DEFAULT_EXTENSIONS: string[];
|
|
7
8
|
type LightningCssConfig = boolean | Partial<Omit<LightningTransformOptions<never>, 'code'>>;
|
|
8
9
|
type PeerLoader = (name: string) => Promise<unknown>;
|
|
@@ -16,7 +17,7 @@ export interface CssOptions {
|
|
|
16
17
|
strategy?: SpecificityStrategy;
|
|
17
18
|
match?: SpecificitySelector[];
|
|
18
19
|
};
|
|
19
|
-
|
|
20
|
+
moduleGraph?: ModuleGraphOptions;
|
|
20
21
|
resolver?: CssResolver;
|
|
21
22
|
peerResolver?: PeerLoader;
|
|
22
23
|
}
|
package/dist/css.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import { existsSync, promises as fs } from 'node:fs';
|
|
3
|
-
import dependencyTree from 'dependency-tree';
|
|
4
3
|
import { composeVisitors, transform as lightningTransform, } from 'lightningcss';
|
|
5
4
|
import { applyStringSpecificityBoost, buildSpecificityVisitor, } from './helpers.js';
|
|
5
|
+
import { collectStyleImports } from './moduleGraph.js';
|
|
6
6
|
import { createSassImporter } from './sassInternals.js';
|
|
7
7
|
export const DEFAULT_EXTENSIONS = ['.css', '.scss', '.sass', '.less', '.css.ts'];
|
|
8
8
|
export async function css(entry, options = {}) {
|
|
@@ -13,11 +13,12 @@ export async function cssWithMeta(entry, options = {}) {
|
|
|
13
13
|
const cwd = options.cwd ? path.resolve(options.cwd) : process.cwd();
|
|
14
14
|
const entryPath = await resolveEntry(entry, cwd, options.resolver);
|
|
15
15
|
const extensions = (options.extensions ?? DEFAULT_EXTENSIONS).map(ext => ext.toLowerCase());
|
|
16
|
-
const files = collectStyleDependencies(entryPath, {
|
|
16
|
+
const files = await collectStyleDependencies(entryPath, {
|
|
17
17
|
cwd,
|
|
18
18
|
extensions,
|
|
19
19
|
filter: options.filter,
|
|
20
|
-
|
|
20
|
+
graphOptions: options.moduleGraph,
|
|
21
|
+
resolver: options.resolver,
|
|
21
22
|
});
|
|
22
23
|
if (files.length === 0) {
|
|
23
24
|
return { css: '', files: [] };
|
|
@@ -67,30 +68,33 @@ async function resolveEntry(entry, cwd, resolver) {
|
|
|
67
68
|
}
|
|
68
69
|
return path.resolve(cwd, entry);
|
|
69
70
|
}
|
|
70
|
-
function collectStyleDependencies(entryPath, { cwd, extensions, filter,
|
|
71
|
+
async function collectStyleDependencies(entryPath, { cwd, extensions, filter, graphOptions, resolver, }) {
|
|
71
72
|
const seen = new Set();
|
|
72
73
|
const order = [];
|
|
73
74
|
const shouldInclude = typeof filter === 'function'
|
|
74
75
|
? filter
|
|
75
76
|
: (filePath) => !filePath.includes('node_modules');
|
|
76
77
|
const entryIsStyle = Boolean(matchExtension(entryPath, extensions));
|
|
77
|
-
let
|
|
78
|
+
let discoveredStyles = [];
|
|
78
79
|
if (!entryIsStyle) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
directory: cwd,
|
|
80
|
+
discoveredStyles = await collectStyleImports(entryPath, {
|
|
81
|
+
cwd,
|
|
82
|
+
styleExtensions: extensions,
|
|
83
83
|
filter: shouldInclude,
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
resolver,
|
|
85
|
+
graphOptions,
|
|
86
|
+
});
|
|
86
87
|
}
|
|
87
|
-
const candidates = entryIsStyle ? [entryPath] : [entryPath, ...
|
|
88
|
+
const candidates = entryIsStyle ? [entryPath] : [entryPath, ...discoveredStyles];
|
|
88
89
|
for (const candidate of candidates) {
|
|
89
90
|
const match = matchExtension(candidate, extensions);
|
|
90
|
-
if (!match
|
|
91
|
+
if (!match)
|
|
92
|
+
continue;
|
|
93
|
+
const resolvedCandidate = path.resolve(candidate);
|
|
94
|
+
if (seen.has(resolvedCandidate))
|
|
91
95
|
continue;
|
|
92
|
-
seen.add(
|
|
93
|
-
order.push({ path:
|
|
96
|
+
seen.add(resolvedCandidate);
|
|
97
|
+
order.push({ path: resolvedCandidate, ext: match });
|
|
94
98
|
}
|
|
95
99
|
return order;
|
|
96
100
|
}
|
package/dist/generateTypes.d.ts
CHANGED
|
@@ -1,54 +1,103 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { moduleType } from 'node-module-type';
|
|
3
|
+
import { getTsconfig } from 'get-tsconfig';
|
|
4
|
+
import { type MatchPath } from 'tsconfig-paths';
|
|
5
|
+
import { cssWithMeta } from './css.js';
|
|
6
|
+
interface ImportMatch {
|
|
3
7
|
specifier: string;
|
|
4
|
-
|
|
8
|
+
importer: string;
|
|
5
9
|
}
|
|
10
|
+
interface ManifestEntry {
|
|
11
|
+
file: string;
|
|
12
|
+
hash: string;
|
|
13
|
+
}
|
|
14
|
+
type SelectorModuleManifest = Record<string, ManifestEntry>;
|
|
15
|
+
interface TsconfigResolutionContext {
|
|
16
|
+
absoluteBaseUrl?: string;
|
|
17
|
+
matchPath?: MatchPath;
|
|
18
|
+
}
|
|
19
|
+
type CssWithMetaFn = typeof cssWithMeta;
|
|
6
20
|
export interface GenerateTypesResult {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
declarations: DeclarationRecord[];
|
|
21
|
+
selectorModulesWritten: number;
|
|
22
|
+
selectorModulesRemoved: number;
|
|
10
23
|
warnings: string[];
|
|
11
|
-
|
|
12
|
-
typesIndexPath: string;
|
|
24
|
+
manifestPath: string;
|
|
13
25
|
}
|
|
14
26
|
export interface GenerateTypesOptions {
|
|
15
27
|
rootDir?: string;
|
|
16
28
|
include?: string[];
|
|
17
29
|
outDir?: string;
|
|
18
|
-
typesRoot?: string;
|
|
19
30
|
stableNamespace?: string;
|
|
20
31
|
}
|
|
32
|
+
type ModuleTypeDetector = () => ReturnType<typeof moduleType>;
|
|
33
|
+
declare function resolvePackageRoot(): string;
|
|
21
34
|
export declare function generateTypes(options?: GenerateTypesOptions): Promise<GenerateTypesResult>;
|
|
22
35
|
declare function normalizeIncludeOptions(include: string[] | undefined, rootDir: string): string[];
|
|
36
|
+
declare function collectCandidateFiles(entries: string[]): Promise<string[]>;
|
|
37
|
+
declare function findSpecifierImports(filePath: string): Promise<ImportMatch[]>;
|
|
23
38
|
declare function stripInlineLoader(specifier: string): string;
|
|
24
39
|
declare function splitResourceAndQuery(specifier: string): {
|
|
25
40
|
resource: string;
|
|
26
41
|
query: string;
|
|
27
42
|
};
|
|
28
|
-
declare function
|
|
29
|
-
declare function
|
|
30
|
-
declare function
|
|
43
|
+
declare function extractSelectorSourceSpecifier(specifier: string): string | undefined;
|
|
44
|
+
declare function resolveImportPath(resourceSpecifier: string, importerPath: string, rootDir: string, tsconfig?: TsconfigResolutionContext): Promise<string | undefined>;
|
|
45
|
+
declare function buildSelectorModuleManifestKey(resolvedPath: string): string;
|
|
46
|
+
declare function buildSelectorModulePath(resolvedPath: string): string;
|
|
47
|
+
declare function formatSelectorModuleSource(selectors: Map<string, string>): string;
|
|
48
|
+
declare function readManifest(manifestPath: string): Promise<SelectorModuleManifest>;
|
|
49
|
+
declare function writeManifest(manifestPath: string, manifest: SelectorModuleManifest): Promise<void>;
|
|
50
|
+
declare function removeStaleSelectorModules(previous: SelectorModuleManifest, next: SelectorModuleManifest): Promise<number>;
|
|
51
|
+
declare function relativeToRoot(filePath: string, rootDir: string): string;
|
|
52
|
+
declare function ensureSelectorModule(resolvedPath: string, selectors: Map<string, string>, previousManifest: SelectorModuleManifest, nextManifest: SelectorModuleManifest): Promise<boolean>;
|
|
53
|
+
declare function resolveWithTsconfigPaths(specifier: string, tsconfig?: TsconfigResolutionContext): Promise<string | undefined>;
|
|
54
|
+
declare function loadTsconfigResolutionContext(rootDir: string, loader?: typeof getTsconfig): TsconfigResolutionContext | undefined;
|
|
55
|
+
declare function normalizeTsconfigPaths(paths: Record<string, string[] | string> | undefined): Record<string, string[]> | undefined;
|
|
56
|
+
declare function isNonRelativeSpecifier(specifier: string): boolean;
|
|
57
|
+
declare function createProjectPeerResolver(rootDir: string): (name: string) => Promise<any>;
|
|
58
|
+
declare function getProjectRequire(rootDir: string): ReturnType<typeof createRequire>;
|
|
31
59
|
export declare function runGenerateTypesCli(argv?: string[]): Promise<void>;
|
|
32
60
|
export interface ParsedCliArgs {
|
|
33
61
|
rootDir: string;
|
|
34
62
|
include?: string[];
|
|
35
63
|
outDir?: string;
|
|
36
|
-
typesRoot?: string;
|
|
37
64
|
stableNamespace?: string;
|
|
38
65
|
help?: boolean;
|
|
39
66
|
}
|
|
40
67
|
declare function parseCliArgs(argv: string[]): ParsedCliArgs;
|
|
41
68
|
declare function printHelp(): void;
|
|
42
69
|
declare function reportCliResult(result: GenerateTypesResult): void;
|
|
70
|
+
declare function setCssWithMetaImplementation(impl?: CssWithMetaFn): void;
|
|
71
|
+
declare function setModuleTypeDetector(detector?: ModuleTypeDetector): void;
|
|
72
|
+
declare function setImportMetaUrlProvider(provider?: () => string | undefined): void;
|
|
43
73
|
export declare const __generateTypesInternals: {
|
|
44
74
|
stripInlineLoader: typeof stripInlineLoader;
|
|
45
75
|
splitResourceAndQuery: typeof splitResourceAndQuery;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
76
|
+
extractSelectorSourceSpecifier: typeof extractSelectorSourceSpecifier;
|
|
77
|
+
findSpecifierImports: typeof findSpecifierImports;
|
|
78
|
+
resolveImportPath: typeof resolveImportPath;
|
|
79
|
+
resolvePackageRoot: typeof resolvePackageRoot;
|
|
80
|
+
relativeToRoot: typeof relativeToRoot;
|
|
81
|
+
collectCandidateFiles: typeof collectCandidateFiles;
|
|
49
82
|
normalizeIncludeOptions: typeof normalizeIncludeOptions;
|
|
83
|
+
normalizeTsconfigPaths: typeof normalizeTsconfigPaths;
|
|
84
|
+
setCssWithMetaImplementation: typeof setCssWithMetaImplementation;
|
|
85
|
+
setModuleTypeDetector: typeof setModuleTypeDetector;
|
|
86
|
+
setImportMetaUrlProvider: typeof setImportMetaUrlProvider;
|
|
87
|
+
isNonRelativeSpecifier: typeof isNonRelativeSpecifier;
|
|
88
|
+
createProjectPeerResolver: typeof createProjectPeerResolver;
|
|
89
|
+
getProjectRequire: typeof getProjectRequire;
|
|
90
|
+
loadTsconfigResolutionContext: typeof loadTsconfigResolutionContext;
|
|
91
|
+
resolveWithTsconfigPaths: typeof resolveWithTsconfigPaths;
|
|
50
92
|
parseCliArgs: typeof parseCliArgs;
|
|
51
93
|
printHelp: typeof printHelp;
|
|
52
94
|
reportCliResult: typeof reportCliResult;
|
|
95
|
+
buildSelectorModuleManifestKey: typeof buildSelectorModuleManifestKey;
|
|
96
|
+
buildSelectorModulePath: typeof buildSelectorModulePath;
|
|
97
|
+
formatSelectorModuleSource: typeof formatSelectorModuleSource;
|
|
98
|
+
ensureSelectorModule: typeof ensureSelectorModule;
|
|
99
|
+
removeStaleSelectorModules: typeof removeStaleSelectorModules;
|
|
100
|
+
readManifest: typeof readManifest;
|
|
101
|
+
writeManifest: typeof writeManifest;
|
|
53
102
|
};
|
|
54
103
|
export {};
|