@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.
Files changed (41) hide show
  1. package/bin/generate-types.js +31 -0
  2. package/dist/cjs/css.cjs +73 -21
  3. package/dist/cjs/css.d.cts +5 -4
  4. package/dist/cjs/generateTypes.cjs +529 -0
  5. package/dist/cjs/generateTypes.d.cts +54 -0
  6. package/dist/cjs/loader.cjs +51 -27
  7. package/dist/cjs/loader.d.cts +1 -0
  8. package/dist/cjs/loaderInternals.cjs +38 -1
  9. package/dist/cjs/loaderInternals.d.cts +7 -0
  10. package/dist/cjs/moduleGraph.cjs +431 -0
  11. package/dist/cjs/moduleGraph.d.cts +15 -0
  12. package/dist/cjs/sassInternals.cjs +6 -3
  13. package/dist/cjs/sassInternals.d.cts +3 -4
  14. package/dist/cjs/stableNamespace.cjs +12 -0
  15. package/dist/cjs/stableNamespace.d.cts +3 -0
  16. package/dist/cjs/stableSelectorsLiteral.cjs +104 -0
  17. package/dist/cjs/stableSelectorsLiteral.d.cts +19 -0
  18. package/dist/cjs/types.cjs +2 -0
  19. package/dist/cjs/types.d.cts +4 -0
  20. package/dist/css.d.ts +5 -4
  21. package/dist/css.js +73 -21
  22. package/dist/generateTypes.d.ts +54 -0
  23. package/dist/generateTypes.js +521 -0
  24. package/dist/loader.d.ts +1 -0
  25. package/dist/loader.js +50 -26
  26. package/dist/loaderInternals.d.ts +7 -0
  27. package/dist/loaderInternals.js +33 -0
  28. package/dist/moduleGraph.d.ts +15 -0
  29. package/dist/moduleGraph.js +425 -0
  30. package/dist/sassInternals.d.ts +3 -4
  31. package/dist/sassInternals.js +6 -3
  32. package/dist/stableNamespace.d.ts +3 -0
  33. package/dist/stableNamespace.js +8 -0
  34. package/dist/stableSelectorsLiteral.d.ts +19 -0
  35. package/dist/stableSelectorsLiteral.js +98 -0
  36. package/dist/types.d.ts +4 -0
  37. package/dist/types.js +1 -0
  38. package/loader-queries.d.ts +28 -0
  39. package/package.json +23 -11
  40. package/types-stub/index.d.ts +5 -0
  41. package/types.d.ts +1 -0
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env node
2
+
3
+ import path from 'node:path'
4
+ import { fileURLToPath, pathToFileURL } from 'node:url'
5
+
6
+ const __dirname = path.dirname(fileURLToPath(import.meta.url))
7
+ const distPath = path.resolve(__dirname, '../dist/generateTypes.js')
8
+
9
+ async function main() {
10
+ try {
11
+ const mod = await import(pathToFileURL(distPath).href)
12
+ const runner =
13
+ typeof mod.runGenerateTypesCli === 'function'
14
+ ? mod.runGenerateTypesCli
15
+ : typeof mod.default === 'function'
16
+ ? mod.default
17
+ : undefined
18
+ if (typeof runner !== 'function') {
19
+ console.error('[knighted-css] Unable to load generateTypes CLI entry point.')
20
+ process.exitCode = 1
21
+ return
22
+ }
23
+ await runner(process.argv.slice(2))
24
+ } catch (error) {
25
+ console.error('[knighted-css] Failed to run generateTypes CLI.')
26
+ console.error(error)
27
+ process.exitCode = 1
28
+ }
29
+ }
30
+
31
+ void main()
package/dist/cjs/css.cjs CHANGED
@@ -9,9 +9,9 @@ exports.cssWithMeta = cssWithMeta;
9
9
  exports.compileVanillaModule = compileVanillaModule;
10
10
  const node_path_1 = __importDefault(require("node:path"));
11
11
  const node_fs_1 = require("node:fs");
12
- const dependency_tree_1 = __importDefault(require("dependency-tree"));
13
12
  const lightningcss_1 = require("lightningcss");
14
13
  const helpers_js_1 = require("./helpers.cjs");
14
+ const moduleGraph_js_1 = require("./moduleGraph.cjs");
15
15
  const sassInternals_js_1 = require("./sassInternals.cjs");
16
16
  exports.DEFAULT_EXTENSIONS = ['.css', '.scss', '.sass', '.less', '.css.ts'];
17
17
  async function css(entry, options = {}) {
@@ -22,11 +22,12 @@ async function cssWithMeta(entry, options = {}) {
22
22
  const cwd = options.cwd ? node_path_1.default.resolve(options.cwd) : process.cwd();
23
23
  const entryPath = await resolveEntry(entry, cwd, options.resolver);
24
24
  const extensions = (options.extensions ?? exports.DEFAULT_EXTENSIONS).map(ext => ext.toLowerCase());
25
- const files = collectStyleDependencies(entryPath, {
25
+ const files = await collectStyleDependencies(entryPath, {
26
26
  cwd,
27
27
  extensions,
28
28
  filter: options.filter,
29
- dependencyTreeOptions: options.dependencyTree,
29
+ graphOptions: options.moduleGraph,
30
+ resolver: options.resolver,
30
31
  });
31
32
  if (files.length === 0) {
32
33
  return { css: '', files: [] };
@@ -76,30 +77,33 @@ async function resolveEntry(entry, cwd, resolver) {
76
77
  }
77
78
  return node_path_1.default.resolve(cwd, entry);
78
79
  }
79
- function collectStyleDependencies(entryPath, { cwd, extensions, filter, dependencyTreeOptions, }) {
80
+ async function collectStyleDependencies(entryPath, { cwd, extensions, filter, graphOptions, resolver, }) {
80
81
  const seen = new Set();
81
82
  const order = [];
82
83
  const shouldInclude = typeof filter === 'function'
83
84
  ? filter
84
85
  : (filePath) => !filePath.includes('node_modules');
85
86
  const entryIsStyle = Boolean(matchExtension(entryPath, extensions));
86
- let treeList = [];
87
+ let discoveredStyles = [];
87
88
  if (!entryIsStyle) {
88
- const dependencyConfig = {
89
- ...dependencyTreeOptions,
90
- filename: entryPath,
91
- directory: cwd,
89
+ discoveredStyles = await (0, moduleGraph_js_1.collectStyleImports)(entryPath, {
90
+ cwd,
91
+ styleExtensions: extensions,
92
92
  filter: shouldInclude,
93
- };
94
- treeList = dependency_tree_1.default.toList(dependencyConfig);
93
+ resolver,
94
+ graphOptions,
95
+ });
95
96
  }
96
- const candidates = entryIsStyle ? [entryPath] : [entryPath, ...treeList];
97
+ const candidates = entryIsStyle ? [entryPath] : [entryPath, ...discoveredStyles];
97
98
  for (const candidate of candidates) {
98
99
  const match = matchExtension(candidate, extensions);
99
- if (!match || seen.has(candidate))
100
+ if (!match)
101
+ continue;
102
+ const resolvedCandidate = node_path_1.default.resolve(candidate);
103
+ if (seen.has(resolvedCandidate))
100
104
  continue;
101
- seen.add(candidate);
102
- order.push({ path: node_path_1.default.resolve(candidate), ext: match });
105
+ seen.add(resolvedCandidate);
106
+ order.push({ path: resolvedCandidate, ext: match });
103
107
  }
104
108
  return order;
105
109
  }
@@ -128,14 +132,41 @@ async function compileStyleModule(file, { cwd, peerResolver, resolver, }) {
128
132
  }
129
133
  async function compileSass(filePath, indented, { cwd, peerResolver, resolver, }) {
130
134
  const sassModule = await optionalPeer('sass', 'Sass', peerResolver);
131
- const sass = sassModule;
135
+ const sass = resolveSassNamespace(sassModule);
132
136
  const importer = (0, sassInternals_js_1.createSassImporter)({ cwd, resolver });
133
- const result = await sass.compileAsync(filePath, {
134
- style: 'expanded',
135
- loadPaths: buildSassLoadPaths(filePath),
136
- importers: importer ? [importer] : undefined,
137
+ const loadPaths = buildSassLoadPaths(filePath);
138
+ if (typeof sass.compileAsync === 'function') {
139
+ const result = await sass.compileAsync(filePath, {
140
+ style: 'expanded',
141
+ loadPaths,
142
+ importers: importer ? [importer] : undefined,
143
+ });
144
+ return result.css;
145
+ }
146
+ if (typeof sass.render === 'function') {
147
+ return renderLegacySass(sass, filePath, indented, loadPaths);
148
+ }
149
+ throw new Error('@knighted/css: Installed "sass" package does not expose compileAsync or render APIs. Please update "sass" to a supported version.');
150
+ }
151
+ function renderLegacySass(sass, filePath, indented, loadPaths) {
152
+ return new Promise((resolve, reject) => {
153
+ sass.render({
154
+ file: filePath,
155
+ indentedSyntax: indented,
156
+ outputStyle: 'expanded',
157
+ includePaths: loadPaths,
158
+ }, (error, result) => {
159
+ if (error) {
160
+ reject(error);
161
+ return;
162
+ }
163
+ if (!result || typeof result.css === 'undefined') {
164
+ resolve('');
165
+ return;
166
+ }
167
+ resolve(result.css.toString());
168
+ });
137
169
  });
138
- return result.css;
139
170
  }
140
171
  // Ensure Sass can resolve bare module specifiers by walking node_modules folders.
141
172
  function buildSassLoadPaths(filePath) {
@@ -223,6 +254,27 @@ async function optionalPeer(name, label, loader) {
223
254
  throw error;
224
255
  }
225
256
  }
257
+ function resolveSassNamespace(mod) {
258
+ if (isSassNamespace(mod)) {
259
+ return mod;
260
+ }
261
+ if (typeof mod === 'object' &&
262
+ mod !== null &&
263
+ 'default' in mod) {
264
+ const candidate = mod.default;
265
+ if (isSassNamespace(candidate)) {
266
+ return candidate;
267
+ }
268
+ }
269
+ return mod;
270
+ }
271
+ function isSassNamespace(candidate) {
272
+ if (typeof candidate !== 'object' || !candidate) {
273
+ return false;
274
+ }
275
+ const namespace = candidate;
276
+ return typeof namespace.compile === 'function' || typeof namespace.render === 'function';
277
+ }
226
278
  function unwrapModuleNamespace(mod) {
227
279
  if (typeof mod === 'object' &&
228
280
  mod !== null &&
@@ -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.cjs';
4
- import type { CssResolver } from './sassInternals.cjs';
5
- export type { CssResolver } from './sassInternals.cjs';
3
+ import type { ModuleGraphOptions } from './moduleGraph.cjs';
4
+ import type { CssResolver } from './types.cjs';
5
+ export type { CssResolver } from './types.cjs';
6
+ export type { ModuleGraphOptions } from './moduleGraph.cjs';
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
- dependencyTree?: Partial<Omit<DependencyTreeOpts, 'filename' | 'directory'>>;
20
+ moduleGraph?: ModuleGraphOptions;
20
21
  resolver?: CssResolver;
21
22
  peerResolver?: PeerLoader;
22
23
  }