@knighted/css 1.0.0-rc.10 → 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.
@@ -108,9 +108,6 @@ async function generateDeclarations(options) {
108
108
  if (!query || !(0, loaderInternals_js_1.hasQueryFlag)(query, loaderInternals_js_1.TYPES_QUERY_FLAG)) {
109
109
  continue;
110
110
  }
111
- if (processedSpecifiers.has(cleaned)) {
112
- continue;
113
- }
114
111
  const resolvedNamespace = (0, stableNamespace_js_1.resolveStableNamespace)(options.stableNamespace);
115
112
  const resolvedPath = await resolveImportPath(resource, match.importer, options.rootDir, options.tsconfig);
116
113
  if (!resolvedPath) {
@@ -138,22 +135,26 @@ async function generateDeclarations(options) {
138
135
  }
139
136
  selectorCache.set(cacheKey, selectorMap);
140
137
  }
138
+ const canonicalSpecifier = buildDeclarationModuleSpecifier(resolvedPath, options.outDir, query);
139
+ if (processedSpecifiers.has(canonicalSpecifier)) {
140
+ continue;
141
+ }
141
142
  const variant = (0, loaderInternals_js_1.determineSelectorVariant)(query);
142
- const declaration = formatModuleDeclaration(cleaned, variant, selectorMap);
143
+ const declaration = formatModuleDeclaration(canonicalSpecifier, variant, selectorMap);
143
144
  const declarationHash = hashContent(declaration);
144
- const fileName = buildDeclarationFileName(cleaned);
145
+ const fileName = buildDeclarationFileName(canonicalSpecifier);
145
146
  const targetPath = node_path_1.default.join(options.outDir, fileName);
146
- const previousEntry = previousManifest[cleaned];
147
+ const previousEntry = previousManifest[canonicalSpecifier];
147
148
  const needsWrite = previousEntry?.hash !== declarationHash || !(await fileExists(targetPath));
148
149
  if (needsWrite) {
149
150
  await promises_1.default.writeFile(targetPath, declaration, 'utf8');
150
151
  writes += 1;
151
152
  }
152
- nextManifest[cleaned] = { file: fileName, hash: declarationHash };
153
+ nextManifest[canonicalSpecifier] = { file: fileName, hash: declarationHash };
153
154
  if (needsWrite) {
154
- declarations.push({ specifier: cleaned, filePath: targetPath });
155
+ declarations.push({ specifier: canonicalSpecifier, filePath: targetPath });
155
156
  }
156
- processedSpecifiers.add(cleaned);
157
+ processedSpecifiers.add(canonicalSpecifier);
157
158
  }
158
159
  }
159
160
  const removed = await removeStaleDeclarations(previousManifest, nextManifest, options.outDir);
@@ -325,6 +326,50 @@ function formatSelectorType(selectors) {
325
326
  ${lines.join('\n')}
326
327
  }>`;
327
328
  }
329
+ function buildDeclarationModuleSpecifier(resolvedPath, declarationDir, query) {
330
+ const relativePath = node_path_1.default.relative(declarationDir, resolvedPath);
331
+ const normalizedPath = normalizeRelativePath(relativePath);
332
+ const canonicalQuery = buildCanonicalQuery(query);
333
+ return `${normalizedPath}${canonicalQuery}`;
334
+ }
335
+ function normalizeRelativePath(relativePath) {
336
+ let normalized = relativePath.split(node_path_1.default.sep).join('/');
337
+ if (!normalized || normalized === '') {
338
+ normalized = '.';
339
+ }
340
+ if (normalized === '.') {
341
+ return './';
342
+ }
343
+ if (normalized.startsWith('./') || normalized.startsWith('../')) {
344
+ return normalized;
345
+ }
346
+ if (normalized.startsWith('.')) {
347
+ return normalized;
348
+ }
349
+ return `./${normalized}`;
350
+ }
351
+ function buildCanonicalQuery(query) {
352
+ if (!query) {
353
+ return '';
354
+ }
355
+ const sanitized = (0, loaderInternals_js_1.buildSanitizedQuery)(query);
356
+ const extraParts = sanitized ? sanitized.slice(1).split('&').filter(Boolean) : [];
357
+ const parts = [];
358
+ parts.push('knighted-css');
359
+ if ((0, loaderInternals_js_1.hasQueryFlag)(query, loaderInternals_js_1.COMBINED_QUERY_FLAG)) {
360
+ parts.push(loaderInternals_js_1.COMBINED_QUERY_FLAG);
361
+ }
362
+ for (const flag of loaderInternals_js_1.NAMED_ONLY_QUERY_FLAGS) {
363
+ if ((0, loaderInternals_js_1.hasQueryFlag)(query, flag)) {
364
+ parts.push(flag);
365
+ }
366
+ }
367
+ if ((0, loaderInternals_js_1.hasQueryFlag)(query, loaderInternals_js_1.TYPES_QUERY_FLAG)) {
368
+ parts.push(loaderInternals_js_1.TYPES_QUERY_FLAG);
369
+ }
370
+ const merged = [...parts, ...extraParts];
371
+ return merged.length > 0 ? `?${merged.join('&')}` : '';
372
+ }
328
373
  function hashContent(content) {
329
374
  return node_crypto_1.default.createHash('sha1').update(content).digest('hex');
330
375
  }
@@ -618,6 +663,8 @@ exports.__generateTypesInternals = {
618
663
  buildDeclarationFileName,
619
664
  formatModuleDeclaration,
620
665
  formatSelectorType,
666
+ buildDeclarationModuleSpecifier,
667
+ buildCanonicalQuery,
621
668
  relativeToRoot,
622
669
  collectCandidateFiles,
623
670
  normalizeIncludeOptions,
@@ -52,6 +52,8 @@ declare function resolveImportPath(resourceSpecifier: string, importerPath: stri
52
52
  declare function buildDeclarationFileName(specifier: string): string;
53
53
  declare function formatModuleDeclaration(specifier: string, variant: SelectorTypeVariant, selectors: Map<string, string>): string;
54
54
  declare function formatSelectorType(selectors: Map<string, string>): string;
55
+ declare function buildDeclarationModuleSpecifier(resolvedPath: string, declarationDir: string, query: string): string;
56
+ declare function buildCanonicalQuery(query: string): string;
55
57
  declare function writeTypesIndex(indexPath: string, manifest: Manifest, outDir: string): Promise<void>;
56
58
  declare function relativeToRoot(filePath: string, rootDir: string): string;
57
59
  declare function resolveWithTsconfigPaths(specifier: string, tsconfig?: TsconfigResolutionContext): Promise<string | undefined>;
@@ -85,6 +87,8 @@ export declare const __generateTypesInternals: {
85
87
  buildDeclarationFileName: typeof buildDeclarationFileName;
86
88
  formatModuleDeclaration: typeof formatModuleDeclaration;
87
89
  formatSelectorType: typeof formatSelectorType;
90
+ buildDeclarationModuleSpecifier: typeof buildDeclarationModuleSpecifier;
91
+ buildCanonicalQuery: typeof buildCanonicalQuery;
88
92
  relativeToRoot: typeof relativeToRoot;
89
93
  collectCandidateFiles: typeof collectCandidateFiles;
90
94
  normalizeIncludeOptions: typeof normalizeIncludeOptions;
@@ -52,6 +52,8 @@ declare function resolveImportPath(resourceSpecifier: string, importerPath: stri
52
52
  declare function buildDeclarationFileName(specifier: string): string;
53
53
  declare function formatModuleDeclaration(specifier: string, variant: SelectorTypeVariant, selectors: Map<string, string>): string;
54
54
  declare function formatSelectorType(selectors: Map<string, string>): string;
55
+ declare function buildDeclarationModuleSpecifier(resolvedPath: string, declarationDir: string, query: string): string;
56
+ declare function buildCanonicalQuery(query: string): string;
55
57
  declare function writeTypesIndex(indexPath: string, manifest: Manifest, outDir: string): Promise<void>;
56
58
  declare function relativeToRoot(filePath: string, rootDir: string): string;
57
59
  declare function resolveWithTsconfigPaths(specifier: string, tsconfig?: TsconfigResolutionContext): Promise<string | undefined>;
@@ -85,6 +87,8 @@ export declare const __generateTypesInternals: {
85
87
  buildDeclarationFileName: typeof buildDeclarationFileName;
86
88
  formatModuleDeclaration: typeof formatModuleDeclaration;
87
89
  formatSelectorType: typeof formatSelectorType;
90
+ buildDeclarationModuleSpecifier: typeof buildDeclarationModuleSpecifier;
91
+ buildCanonicalQuery: typeof buildCanonicalQuery;
88
92
  relativeToRoot: typeof relativeToRoot;
89
93
  collectCandidateFiles: typeof collectCandidateFiles;
90
94
  normalizeIncludeOptions: typeof normalizeIncludeOptions;
@@ -8,7 +8,7 @@ import { moduleType } from 'node-module-type';
8
8
  import { getTsconfig } from 'get-tsconfig';
9
9
  import { createMatchPath } from 'tsconfig-paths';
10
10
  import { cssWithMeta } from './css.js';
11
- import { determineSelectorVariant, hasQueryFlag, TYPES_QUERY_FLAG, } from './loaderInternals.js';
11
+ import { determineSelectorVariant, hasQueryFlag, TYPES_QUERY_FLAG, buildSanitizedQuery, COMBINED_QUERY_FLAG, NAMED_ONLY_QUERY_FLAGS, } from './loaderInternals.js';
12
12
  import { buildStableSelectorsLiteral } from './stableSelectorsLiteral.js';
13
13
  import { resolveStableNamespace } from './stableNamespace.js';
14
14
  let activeCssWithMeta = cssWithMeta;
@@ -100,9 +100,6 @@ async function generateDeclarations(options) {
100
100
  if (!query || !hasQueryFlag(query, TYPES_QUERY_FLAG)) {
101
101
  continue;
102
102
  }
103
- if (processedSpecifiers.has(cleaned)) {
104
- continue;
105
- }
106
103
  const resolvedNamespace = resolveStableNamespace(options.stableNamespace);
107
104
  const resolvedPath = await resolveImportPath(resource, match.importer, options.rootDir, options.tsconfig);
108
105
  if (!resolvedPath) {
@@ -130,22 +127,26 @@ async function generateDeclarations(options) {
130
127
  }
131
128
  selectorCache.set(cacheKey, selectorMap);
132
129
  }
130
+ const canonicalSpecifier = buildDeclarationModuleSpecifier(resolvedPath, options.outDir, query);
131
+ if (processedSpecifiers.has(canonicalSpecifier)) {
132
+ continue;
133
+ }
133
134
  const variant = determineSelectorVariant(query);
134
- const declaration = formatModuleDeclaration(cleaned, variant, selectorMap);
135
+ const declaration = formatModuleDeclaration(canonicalSpecifier, variant, selectorMap);
135
136
  const declarationHash = hashContent(declaration);
136
- const fileName = buildDeclarationFileName(cleaned);
137
+ const fileName = buildDeclarationFileName(canonicalSpecifier);
137
138
  const targetPath = path.join(options.outDir, fileName);
138
- const previousEntry = previousManifest[cleaned];
139
+ const previousEntry = previousManifest[canonicalSpecifier];
139
140
  const needsWrite = previousEntry?.hash !== declarationHash || !(await fileExists(targetPath));
140
141
  if (needsWrite) {
141
142
  await fs.writeFile(targetPath, declaration, 'utf8');
142
143
  writes += 1;
143
144
  }
144
- nextManifest[cleaned] = { file: fileName, hash: declarationHash };
145
+ nextManifest[canonicalSpecifier] = { file: fileName, hash: declarationHash };
145
146
  if (needsWrite) {
146
- declarations.push({ specifier: cleaned, filePath: targetPath });
147
+ declarations.push({ specifier: canonicalSpecifier, filePath: targetPath });
147
148
  }
148
- processedSpecifiers.add(cleaned);
149
+ processedSpecifiers.add(canonicalSpecifier);
149
150
  }
150
151
  }
151
152
  const removed = await removeStaleDeclarations(previousManifest, nextManifest, options.outDir);
@@ -317,6 +318,50 @@ function formatSelectorType(selectors) {
317
318
  ${lines.join('\n')}
318
319
  }>`;
319
320
  }
321
+ function buildDeclarationModuleSpecifier(resolvedPath, declarationDir, query) {
322
+ const relativePath = path.relative(declarationDir, resolvedPath);
323
+ const normalizedPath = normalizeRelativePath(relativePath);
324
+ const canonicalQuery = buildCanonicalQuery(query);
325
+ return `${normalizedPath}${canonicalQuery}`;
326
+ }
327
+ function normalizeRelativePath(relativePath) {
328
+ let normalized = relativePath.split(path.sep).join('/');
329
+ if (!normalized || normalized === '') {
330
+ normalized = '.';
331
+ }
332
+ if (normalized === '.') {
333
+ return './';
334
+ }
335
+ if (normalized.startsWith('./') || normalized.startsWith('../')) {
336
+ return normalized;
337
+ }
338
+ if (normalized.startsWith('.')) {
339
+ return normalized;
340
+ }
341
+ return `./${normalized}`;
342
+ }
343
+ function buildCanonicalQuery(query) {
344
+ if (!query) {
345
+ return '';
346
+ }
347
+ const sanitized = buildSanitizedQuery(query);
348
+ const extraParts = sanitized ? sanitized.slice(1).split('&').filter(Boolean) : [];
349
+ const parts = [];
350
+ parts.push('knighted-css');
351
+ if (hasQueryFlag(query, COMBINED_QUERY_FLAG)) {
352
+ parts.push(COMBINED_QUERY_FLAG);
353
+ }
354
+ for (const flag of NAMED_ONLY_QUERY_FLAGS) {
355
+ if (hasQueryFlag(query, flag)) {
356
+ parts.push(flag);
357
+ }
358
+ }
359
+ if (hasQueryFlag(query, TYPES_QUERY_FLAG)) {
360
+ parts.push(TYPES_QUERY_FLAG);
361
+ }
362
+ const merged = [...parts, ...extraParts];
363
+ return merged.length > 0 ? `?${merged.join('&')}` : '';
364
+ }
320
365
  function hashContent(content) {
321
366
  return crypto.createHash('sha1').update(content).digest('hex');
322
367
  }
@@ -610,6 +655,8 @@ export const __generateTypesInternals = {
610
655
  buildDeclarationFileName,
611
656
  formatModuleDeclaration,
612
657
  formatSelectorType,
658
+ buildDeclarationModuleSpecifier,
659
+ buildCanonicalQuery,
613
660
  relativeToRoot,
614
661
  collectCandidateFiles,
615
662
  normalizeIncludeOptions,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knighted/css",
3
- "version": "1.0.0-rc.10",
3
+ "version": "1.0.0-rc.11",
4
4
  "description": "A build-time utility that traverses JavaScript/TypeScript module dependency graphs to extract, compile, and optimize all imported CSS into a single, in-memory string.",
5
5
  "type": "module",
6
6
  "main": "./dist/css.js",