@knighted/css 1.1.0-rc.1 → 1.1.0-rc.3

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.
@@ -16,6 +16,7 @@ const get_tsconfig_1 = require("get-tsconfig");
16
16
  const tsconfig_paths_1 = require("tsconfig-paths");
17
17
  const css_js_1 = require("./css.cjs");
18
18
  const lexer_js_1 = require("./lexer.cjs");
19
+ const moduleResolution_js_1 = require("./moduleResolution.cjs");
19
20
  const stableSelectorsLiteral_js_1 = require("./stableSelectorsLiteral.cjs");
20
21
  const stableNamespace_js_1 = require("./stableNamespace.cjs");
21
22
  let activeCssWithMeta = css_js_1.cssWithMeta;
@@ -66,6 +67,8 @@ function getImportMetaUrl() {
66
67
  const SELECTOR_REFERENCE = '.knighted-css';
67
68
  const SELECTOR_MODULE_SUFFIX = '.knighted-css.ts';
68
69
  const STYLE_EXTENSIONS = css_js_1.DEFAULT_EXTENSIONS.map(ext => ext.toLowerCase());
70
+ const SCRIPT_EXTENSIONS = Array.from(SUPPORTED_EXTENSIONS);
71
+ const RESOLUTION_EXTENSIONS = Array.from(new Set([...SCRIPT_EXTENSIONS, ...STYLE_EXTENSIONS]));
69
72
  const EXTENSION_FALLBACKS = {
70
73
  '.js': ['.ts', '.tsx', '.jsx', '.mjs', '.cjs'],
71
74
  '.mjs': ['.mts', '.mjs', '.js', '.ts', '.tsx'],
@@ -73,7 +76,7 @@ const EXTENSION_FALLBACKS = {
73
76
  '.jsx': ['.tsx', '.jsx'],
74
77
  };
75
78
  async function generateTypes(options = {}) {
76
- const rootDir = node_path_1.default.resolve(options.rootDir ?? process.cwd());
79
+ const rootDir = await resolveRootDir(node_path_1.default.resolve(options.rootDir ?? process.cwd()));
77
80
  const include = normalizeIncludeOptions(options.include, rootDir);
78
81
  const cacheDir = node_path_1.default.resolve(options.outDir ?? node_path_1.default.join(rootDir, '.knighted-css'));
79
82
  const tsconfig = loadTsconfigResolutionContext(rootDir);
@@ -88,8 +91,17 @@ async function generateTypes(options = {}) {
88
91
  };
89
92
  return generateDeclarations(internalOptions);
90
93
  }
94
+ async function resolveRootDir(rootDir) {
95
+ try {
96
+ return await promises_1.default.realpath(rootDir);
97
+ }
98
+ catch {
99
+ return rootDir;
100
+ }
101
+ }
91
102
  async function generateDeclarations(options) {
92
103
  const peerResolver = createProjectPeerResolver(options.rootDir);
104
+ const resolverFactory = (0, moduleResolution_js_1.createResolverFactory)(options.rootDir, RESOLUTION_EXTENSIONS, SCRIPT_EXTENSIONS);
93
105
  const files = await collectCandidateFiles(options.include);
94
106
  const selectorModulesManifestPath = node_path_1.default.join(options.cacheDir, 'selector-modules.json');
95
107
  const previousSelectorManifest = await readManifest(selectorModulesManifestPath);
@@ -110,7 +122,7 @@ async function generateDeclarations(options) {
110
122
  continue;
111
123
  }
112
124
  const resolvedNamespace = (0, stableNamespace_js_1.resolveStableNamespace)(options.stableNamespace);
113
- const resolvedPath = await resolveImportPath(selectorSource, match.importer, options.rootDir, options.tsconfig);
125
+ const resolvedPath = await resolveImportPath(selectorSource, match.importer, options.rootDir, options.tsconfig, resolverFactory, RESOLUTION_EXTENSIONS);
114
126
  if (!resolvedPath) {
115
127
  warnings.push(`Unable to resolve ${selectorSource} referenced by ${relativeToRoot(match.importer, options.rootDir)}.`);
116
128
  continue;
@@ -251,7 +263,8 @@ function stripInlineLoader(specifier) {
251
263
  return idx >= 0 ? specifier.slice(idx + 1) : specifier;
252
264
  }
253
265
  function splitResourceAndQuery(specifier) {
254
- const hashIndex = specifier.indexOf('#');
266
+ const hashOffset = specifier.startsWith('#') ? 1 : 0;
267
+ const hashIndex = specifier.indexOf('#', hashOffset);
255
268
  const trimmed = hashIndex >= 0 ? specifier.slice(0, hashIndex) : specifier;
256
269
  const queryIndex = trimmed.indexOf('?');
257
270
  if (queryIndex < 0) {
@@ -282,7 +295,7 @@ function extractSelectorSourceSpecifier(specifier) {
282
295
  return base;
283
296
  }
284
297
  const projectRequireCache = new Map();
285
- async function resolveImportPath(resourceSpecifier, importerPath, rootDir, tsconfig) {
298
+ async function resolveImportPath(resourceSpecifier, importerPath, rootDir, tsconfig, resolverFactory, resolutionExtensions = RESOLUTION_EXTENSIONS) {
286
299
  if (!resourceSpecifier)
287
300
  return undefined;
288
301
  if (resourceSpecifier.startsWith('.')) {
@@ -295,6 +308,12 @@ async function resolveImportPath(resourceSpecifier, importerPath, rootDir, tscon
295
308
  if (tsconfigResolved) {
296
309
  return resolveWithExtensionFallback(tsconfigResolved);
297
310
  }
311
+ if (resolverFactory) {
312
+ const resolved = (0, moduleResolution_js_1.resolveWithFactory)(resolverFactory, resourceSpecifier, importerPath, resolutionExtensions);
313
+ if (resolved) {
314
+ return resolved;
315
+ }
316
+ }
298
317
  const requireFromRoot = getProjectRequire(rootDir);
299
318
  try {
300
319
  return requireFromRoot.resolve(resourceSpecifier);
@@ -315,7 +334,7 @@ function buildSelectorModulePath(resolvedPath) {
315
334
  return `${base}${SELECTOR_MODULE_SUFFIX}`;
316
335
  }
317
336
  function formatSelectorModuleSource(selectors, proxyInfo) {
318
- const header = '// Generated by @knighted/css/generate-types\n// Do not edit.\n\n';
337
+ const header = '// Generated by @knighted/css/generate-types\n// Do not edit.';
319
338
  const entries = Array.from(selectors.entries()).sort(([a], [b]) => a.localeCompare(b));
320
339
  const lines = entries.map(([token, selector]) => ` ${JSON.stringify(token)}: ${JSON.stringify(selector)},`);
321
340
  const literal = lines.length > 0
@@ -330,14 +349,15 @@ ${lines.join('\n')}
330
349
  proxyLines.push(`export { default } from '${proxyInfo.moduleSpecifier}'`);
331
350
  }
332
351
  proxyLines.push(`export { knightedCss } from '${proxyInfo.moduleSpecifier}?knighted-css'`);
333
- proxyLines.push('');
334
352
  }
335
- const defaultExport = proxyInfo ? '' : '\nexport default stableSelectors\n';
336
- return `${header}${proxyLines.join('\n')}
337
- export const stableSelectors = ${literal}
353
+ const defaultExport = proxyInfo ? '' : '\nexport default stableSelectors';
354
+ const stableBlock = `export const stableSelectors = ${literal}
338
355
 
339
356
  export type KnightedCssStableSelectors = typeof stableSelectors
340
357
  export type KnightedCssStableSelectorToken = keyof typeof stableSelectors${defaultExport}`;
358
+ const sections = [header, proxyLines.join('\n'), stableBlock].filter(Boolean);
359
+ return `${sections.join('\n\n')}
360
+ `;
341
361
  }
342
362
  function hashContent(content) {
343
363
  return node_crypto_1.default.createHash('sha1').update(content).digest('hex');