styles-config 2.0.0-alpha.1

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/src/options.ts ADDED
@@ -0,0 +1,172 @@
1
+ import type { StylesConfig, FileMatchOptions } from './types.js';
2
+ import picomatch from 'picomatch';
3
+ import path from 'path';
4
+
5
+ /**
6
+ * Options for retrieving merged configuration
7
+ */
8
+ export interface GetOptionsParams {
9
+ /**
10
+ * Language key to get options for (e.g., 'less', 'scss', 'jess').
11
+ * If omitted but `input` is provided, language is inferred from the file extension.
12
+ */
13
+ language?: string;
14
+ /**
15
+ * Input file path to match against input options.
16
+ * Also used to infer language if `language` is not specified.
17
+ */
18
+ input?: string;
19
+ /**
20
+ * Output file path to match against output options
21
+ */
22
+ output?: string;
23
+ }
24
+
25
+ /**
26
+ * Map of file extensions to language keys
27
+ */
28
+ const extensionToLanguage = new Map<string, string>([
29
+ ['.less', 'less'],
30
+ ['.scss', 'scss'],
31
+ ['.sass', 'scss'],
32
+ ['.jess', 'jess'],
33
+ ['.css', 'css']
34
+ ]);
35
+
36
+ /**
37
+ * Infer language from a file path's extension
38
+ */
39
+ function inferLanguage(filePath: string | undefined): string | undefined {
40
+ if (!filePath) {
41
+ return undefined;
42
+ }
43
+ const ext = path.extname(filePath).toLowerCase();
44
+ return extensionToLanguage.get(ext);
45
+ }
46
+
47
+ /**
48
+ * Check if a file path matches a pattern (exact path, relative path, or glob)
49
+ */
50
+ function matchesFile(pattern: string | undefined, filePath: string | undefined): boolean {
51
+ if (!pattern || !filePath) {
52
+ return false;
53
+ }
54
+
55
+ // Normalize paths for comparison
56
+ const normalizedPattern = path.normalize(pattern);
57
+ const normalizedFile = path.normalize(filePath);
58
+
59
+ // Try exact match first
60
+ if (normalizedPattern === normalizedFile) {
61
+ return true;
62
+ }
63
+
64
+ // Try basename match (e.g., pattern "styles.less" matches "/path/to/styles.less")
65
+ if (path.basename(normalizedFile) === normalizedPattern) {
66
+ return true;
67
+ }
68
+
69
+ // Try glob/pattern match using picomatch
70
+ const isMatch = picomatch(pattern, { dot: true });
71
+ return isMatch(filePath) || isMatch(normalizedFile);
72
+ }
73
+
74
+ /**
75
+ * Get matching options from an array of file-based options.
76
+ * Returns merged options from:
77
+ * 1. All entries without a `file` property (defaults)
78
+ * 2. All entries whose `file` pattern matches the given path
79
+ *
80
+ * Later entries override earlier ones.
81
+ */
82
+ function getMatchingOptions<T extends FileMatchOptions>(
83
+ options: T | T[] | undefined,
84
+ filePath?: string
85
+ ): Partial<T> {
86
+ if (!options) {
87
+ return {};
88
+ }
89
+
90
+ const optionsArray = Array.isArray(options) ? options : [options];
91
+ let result: Partial<T> = {};
92
+
93
+ for (const opt of optionsArray) {
94
+ // Include if: no file pattern (default), or file pattern matches
95
+ if (!opt.file || (filePath && matchesFile(opt.file, filePath))) {
96
+ // Merge this entry's options, excluding the 'file' property
97
+ const rest = { ...opt } as Record<string, unknown>;
98
+ delete rest.file;
99
+ result = { ...result, ...rest };
100
+ }
101
+ }
102
+
103
+ return result;
104
+ }
105
+ /**
106
+ * Get merged options by combining compile, language, input, and output settings.
107
+ *
108
+ * Merge priority (later wins):
109
+ * 1. compile options (base)
110
+ * 2. language-specific options (inferred from input extension or explicitly specified)
111
+ * 3. matched input options (if input path provided and matches)
112
+ * 4. matched output options (if output path provided and matches)
113
+ *
114
+ * @param config - The styles configuration object
115
+ * @param params - Options specifying language, input file, and output file
116
+ * @returns Merged options object
117
+ *
118
+ * @example
119
+ * // Get Less options for a specific input/output (language inferred from .less extension)
120
+ * const options = getOptions(config, {
121
+ * input: 'src/styles/main.less',
122
+ * output: 'dist/main.css'
123
+ * });
124
+ *
125
+ * @example
126
+ * // Explicitly specify language
127
+ * const options = getOptions(config, { language: 'less' });
128
+ *
129
+ * @example
130
+ * // Get base options without language-specific settings
131
+ * const options = getOptions(config);
132
+ */
133
+ export function getOptions(
134
+ config: StylesConfig = {},
135
+ params: GetOptionsParams = {}
136
+ ): Record<string, any> {
137
+ const { input: inputFile, output: outputFile } = params;
138
+ const { compile = {}, input, output, language: languageConfig = {} } = config;
139
+
140
+ // Determine language: explicit param > inferred from input extension
141
+ const language = params.language ?? inferLanguage(inputFile);
142
+
143
+ // Get language-specific options if language is determined
144
+ const languageOptions = language ? (languageConfig[language] ?? {}) : {};
145
+
146
+ // Get matched input and output options
147
+ const matchedInput = getMatchingOptions(input, inputFile);
148
+ const matchedOutput = getMatchingOptions(output, outputFile);
149
+
150
+ // Build result with proper merge priority:
151
+ // 1. compile (base)
152
+ // 2. language-specific
153
+ // 3. matched input
154
+ // 4. matched output
155
+ return {
156
+ // Start with compile-level settings
157
+ mathMode: compile.mathMode,
158
+ unitMode: compile.unitMode,
159
+ equalityMode: compile.equalityMode,
160
+ paths: compile.searchPaths,
161
+ javascriptEnabled: compile.enableJavaScript,
162
+
163
+ // Override with language-specific settings
164
+ ...languageOptions,
165
+
166
+ // Override with matched input settings
167
+ ...matchedInput,
168
+
169
+ // Override with matched output settings
170
+ ...matchedOutput
171
+ };
172
+ }
package/src/types.ts ADDED
@@ -0,0 +1,359 @@
1
+ /**
2
+ * Math processing modes
3
+ */
4
+ export type MathMode = 'always' | 'parens-division' | 'parens' | 'strict';
5
+
6
+ /**
7
+ * Unit conversion modes
8
+ */
9
+ export type UnitMode = 'loose' | 'preserve' | 'strict';
10
+
11
+ /**
12
+ * Equality/coercion modes for guard comparisons.
13
+ */
14
+ export type EqualityMode = 'coerce' | 'strict';
15
+
16
+ export interface JavaScriptSandboxConfig {
17
+ /**
18
+ * Allow network access for script execution runtime.
19
+ * @default false
20
+ */
21
+ allowHttp?: boolean;
22
+ /**
23
+ * Optional host allowlist when `allowHttp` is enabled.
24
+ */
25
+ allowNetHosts?: string[];
26
+ /**
27
+ * Optional explicit filesystem root for script reads.
28
+ * If omitted, compiler resolves using entry/config roots.
29
+ */
30
+ jsReadRoot?: string;
31
+ }
32
+
33
+ export type CompileJavaScriptOption = true | JavaScriptSandboxConfig;
34
+
35
+ /**
36
+ * Less compiler options
37
+ * Based on less.js default-options.js and bin/lessc
38
+ */
39
+ export interface LessOptions {
40
+ /**
41
+ * Inline Javascript - @plugin still allowed
42
+ * @default false
43
+ */
44
+ javascriptEnabled?: boolean;
45
+
46
+ /**
47
+ * Outputs a makefile import dependency list to stdout.
48
+ * @default false
49
+ */
50
+ depends?: boolean;
51
+
52
+ /**
53
+ * @deprecated Compress using less built-in compression.
54
+ * This does an okay job but does not utilise all the tricks of
55
+ * dedicated css compression.
56
+ * @default false
57
+ */
58
+ compress?: boolean;
59
+
60
+ /**
61
+ * Runs the less parser and just reports errors without any output.
62
+ * @default false
63
+ */
64
+ lint?: boolean;
65
+
66
+ /**
67
+ * Sets available include paths.
68
+ * If the file in an @import rule does not exist at that exact location,
69
+ * less will look for it at the location(s) passed to this option.
70
+ * @default []
71
+ */
72
+ paths?: string[];
73
+
74
+ /**
75
+ * Color output in the terminal
76
+ * @default true
77
+ */
78
+ color?: boolean;
79
+
80
+ /**
81
+ * @deprecated This option has confusing behavior and may be removed in a future version.
82
+ *
83
+ * Controls how @import statements for .less files are handled inside selector blocks (rulesets).
84
+ *
85
+ * Behavior:
86
+ * - @import at root level: Always processed
87
+ * - @import inside @-rules (@media, @supports, etc.): Processed (these are not selector blocks)
88
+ * - @import inside selector blocks (.class, #id, etc.): Behavior depends on this option
89
+ *
90
+ * Options:
91
+ * - `false` (default): All @import statements are processed regardless of context.
92
+ * - `true`: @import statements inside selector blocks are silently ignored and not output.
93
+ * - `'error'`: @import statements inside selector blocks will throw an error instead of being silently ignored.
94
+ *
95
+ * Note: Only affects .less file imports. CSS imports (url(...) or .css files) are
96
+ * always output as CSS @import statements regardless of this setting.
97
+ *
98
+ * @see https://github.com/less/less.js/issues/656
99
+ * @default false
100
+ */
101
+ strictImports?: boolean | 'error';
102
+
103
+ /**
104
+ * Allow Imports from Insecure HTTPS Hosts
105
+ * @default false
106
+ */
107
+ insecure?: boolean;
108
+
109
+ /**
110
+ * Allows you to add a path to every generated import and url in your css.
111
+ * This does not affect less import statements that are processed, just ones
112
+ * that are left in the output css.
113
+ * @default ''
114
+ */
115
+ rootpath?: string;
116
+
117
+ /**
118
+ * By default URLs are kept as-is, so if you import a file in a sub-directory
119
+ * that references an image, exactly the same URL will be output in the css.
120
+ * This option allows you to re-write URL's in imported files so that the
121
+ * URL is always relative to the base imported file
122
+ * @default false
123
+ */
124
+ rewriteUrls?: boolean | 'all' | 'local' | 'off';
125
+
126
+ /**
127
+ * How to process math operations
128
+ * - 'always': eagerly try to solve all operations
129
+ * - 'parens-division': require parens for division "/"
130
+ * - 'parens' or 'strict': require parens for all operations
131
+ * @default 'parens-division'
132
+ */
133
+ mathMode?: MathMode;
134
+
135
+ /**
136
+ * How to handle unit conversions in math operations
137
+ * - 'loose': Less's default 1.x-4.x behavior
138
+ * - 'preserve': Create calc() expressions for unit errors
139
+ * - 'strict': strict unit mode
140
+ * @default 'preserve'
141
+ */
142
+ unitMode?: UnitMode;
143
+
144
+ /**
145
+ * How to handle equality/coercion in guards and comparisons.
146
+ * - 'coerce': Less-compatible coercion behavior
147
+ * - 'strict': type-strict behavior
148
+ * @default 'coerce'
149
+ */
150
+ equalityMode?: EqualityMode;
151
+
152
+ /**
153
+ * @deprecated Use `mathMode` instead. This option maps to `mathMode` as follows:
154
+ * - 0 or 'always' → 'always'
155
+ * - 1 or 'parens-division' → 'parens-division'
156
+ * - 2 or 'parens' or 'strict' → 'parens'
157
+ * - 3 or 'strict-legacy' → 'parens' (removed, will default to 'strict)
158
+ * @default undefined (uses mathMode if provided, otherwise 'parens-division')
159
+ */
160
+ math?: 0 | 1 | 2 | 3 | MathMode | 'strict-legacy';
161
+
162
+ /**
163
+ * @deprecated Use `unitMode` instead. If `true`, sets `unitMode` to 'strict'.
164
+ * If `false`, sets the unitMode to 'loose.
165
+ * If undefined, uses the `unitMode` value (defaults to 'preserve').
166
+ * @default false
167
+ */
168
+ strictUnits?: boolean;
169
+
170
+ /**
171
+ * Effectively the declaration is put at the top of your base Less file,
172
+ * meaning it can be used but it also can be overridden if this variable
173
+ * is defined in the file.
174
+ * @default null
175
+ */
176
+ globalVars?: Record<string, string> | null;
177
+
178
+ /**
179
+ * As opposed to the global variable option, this puts the declaration at the
180
+ * end of your base file, meaning it will override anything defined in your Less file.
181
+ * @default null
182
+ */
183
+ modifyVars?: Record<string, string> | null;
184
+
185
+ /**
186
+ * This option allows you to specify a argument to go on to every URL.
187
+ * @default ''
188
+ */
189
+ urlArgs?: string;
190
+
191
+ /**
192
+ * @removed The dumpLineNumbers option is not useful nor supported in browsers. Use sourcemaps instead.
193
+ *
194
+ * @default undefined
195
+ */
196
+ dumpLineNumbers?: string;
197
+
198
+ /**
199
+ * Source map options
200
+ * @default undefined
201
+ */
202
+ sourceMap?: boolean | {
203
+ sourceMapFullFilename?: string;
204
+ sourceMapRootpath?: string;
205
+ sourceMapBasepath?: string;
206
+ sourceMapURL?: string;
207
+ sourceMapFileInline?: boolean;
208
+ outputSourceFiles?: boolean;
209
+ disableSourcemapAnnotation?: boolean;
210
+ sourceMapOutputFilename?: string;
211
+ sourceMapFilename?: string;
212
+ };
213
+
214
+ /**
215
+ * Verbose output
216
+ * @default false
217
+ */
218
+ verbose?: boolean;
219
+
220
+ /**
221
+ * Silent mode (suppress errors)
222
+ * @default false
223
+ */
224
+ silent?: boolean;
225
+
226
+ /**
227
+ * Quiet mode (suppress warnings)
228
+ * @default false
229
+ */
230
+ quiet?: boolean;
231
+
232
+ /**
233
+ * @deprecated This is legacy Less behavior.
234
+ *
235
+ * Controls whether mixins and detached rulesets "leak" their inner rules.
236
+ * When true:
237
+ * - Mixins: Mixin and VarDeclaration nodes are 'public' and 'optional' respectively
238
+ * - Detached rulesets: Mixin and VarDeclaration nodes are 'public' and 'private' respectively
239
+ * When false:
240
+ * - Both mixins and detached rulesets: Mixin and VarDeclaration nodes are 'private'
241
+ * @default true
242
+ */
243
+ leakyRules?: boolean;
244
+
245
+ /**
246
+ * Whether to collapse nested selectors (Less 1.x-4.x style flattening)
247
+ * When true, nested selectors like `.parent { .child { } }` are flattened to `.parent .child { }`
248
+ * @default false
249
+ */
250
+ collapseNesting?: boolean;
251
+
252
+ /**
253
+ * @deprecated This is legacy Less behavior.
254
+ *
255
+ * Whether to bubble root-only at-rules (@font-face, @keyframes, etc.) to the root
256
+ * when they are nested inside rulesets. Modern CSS supports nesting these at-rules.
257
+ * @default true
258
+ */
259
+ bubbleRootAtRules?: boolean;
260
+ }
261
+
262
+ /**
263
+ * Base interface for file-matching options
264
+ */
265
+ export interface FileMatchOptions {
266
+ /**
267
+ * File path, relative path, or glob pattern for matching.
268
+ * If omitted, the options serve as defaults.
269
+ */
270
+ file?: string;
271
+ }
272
+
273
+ /**
274
+ * Input file options - can override compile and language settings per input file
275
+ */
276
+ export interface InputOptions extends FileMatchOptions {
277
+ // Compile-level options that can be overridden per-input
278
+ mathMode?: MathMode;
279
+ unitMode?: UnitMode;
280
+ equalityMode?: EqualityMode;
281
+ searchPaths?: string[];
282
+ enableJavaScript?: boolean;
283
+
284
+ // Less-specific options that can be overridden per-input
285
+ javascriptEnabled?: boolean;
286
+ paths?: string[];
287
+ globalVars?: Record<string, string> | null;
288
+ modifyVars?: Record<string, string> | null;
289
+ strictImports?: boolean | 'error';
290
+ rewriteUrls?: boolean | 'all' | 'local' | 'off';
291
+ rootpath?: string;
292
+ leakyRules?: boolean;
293
+ collapseNesting?: boolean;
294
+ bubbleRootAtRules?: boolean;
295
+
296
+ /** Allow additional language-specific options */
297
+ [key: string]: any;
298
+ }
299
+
300
+ /**
301
+ * Output file options - can override output settings per output file
302
+ */
303
+ export interface OutputOptions extends FileMatchOptions {
304
+ collapseNesting?: boolean;
305
+ compress?: boolean;
306
+ sourceMap?: boolean | {
307
+ sourceMapFullFilename?: string;
308
+ sourceMapRootpath?: string;
309
+ sourceMapBasepath?: string;
310
+ sourceMapURL?: string;
311
+ sourceMapFileInline?: boolean;
312
+ outputSourceFiles?: boolean;
313
+ disableSourcemapAnnotation?: boolean;
314
+ sourceMapOutputFilename?: string;
315
+ sourceMapFilename?: string;
316
+ };
317
+
318
+ /** Allow additional options */
319
+ [key: string]: any;
320
+ }
321
+
322
+ export interface StylesConfig {
323
+ compile?: {
324
+ /**
325
+ * Plugins can be specified as:
326
+ * - Plugin instances (PluginInterface from @jesscss/core)
327
+ * - String keys (plugin names that get resolved elsewhere)
328
+ *
329
+ * @note - Using `any` here to avoid circular dependency with @jesscss/core.
330
+ * The actual type is PluginInterface from @jesscss/core.
331
+ */
332
+ plugins?: Array<any | string>;
333
+ searchPaths?: string[];
334
+ enableJavaScript?: boolean;
335
+ javascript?: CompileJavaScriptOption;
336
+ mathMode?: MathMode;
337
+ unitMode?: UnitMode;
338
+ equalityMode?: EqualityMode;
339
+ };
340
+ /**
341
+ * Input file options. Can be a single object for defaults, or an array
342
+ * where entries can have a `file` property (path or glob) to match specific inputs.
343
+ * Entries without a `file` property serve as defaults.
344
+ */
345
+ input?: InputOptions | InputOptions[];
346
+ /**
347
+ * Output file options. Can be a single object for defaults, or an array
348
+ * where entries can have a `file` property (path or glob) to match specific outputs.
349
+ * Entries without a `file` property serve as defaults.
350
+ */
351
+ output?: OutputOptions | OutputOptions[];
352
+ language?: {
353
+ less?: LessOptions;
354
+ scss?: Record<string, any>;
355
+ css?: Record<string, any>;
356
+ jess?: Record<string, any>;
357
+ [key: string]: LessOptions | Record<string, any> | undefined;
358
+ };
359
+ }