@joycostudio/chopper 0.5.1 → 0.5.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.
@@ -1,50 +1,123 @@
1
+ /** Maximum image dimensions in pixels: `[width, height]`. Both values must be positive integers. */
1
2
  type TextureSize = readonly [width: number, height: number];
2
3
  interface SizedEncoding {
3
- /** Maximum dimensions. Aspect ratio is preserved and images are not enlarged. */
4
+ /**
5
+ * Maximum output dimensions in pixels. Aspect ratio is preserved, and an
6
+ * image smaller than both limits is not enlarged. Omit this property to keep
7
+ * the source dimensions.
8
+ */
4
9
  maxSize?: TextureSize;
5
10
  }
6
11
  interface WebpEncoding extends SizedEncoding {
12
+ /** Encode the image as WebP. */
7
13
  codec: 'webp';
14
+ /** Lossy quality from 1 (smallest, lowest fidelity) to 100. @defaultValue `75` */
8
15
  quality?: number;
16
+ /** Encoder effort from 0 (fastest) to 100 (slowest). @defaultValue `50` */
9
17
  effort?: number;
18
+ /** Use lossless WebP instead of lossy encoding. Lossy `quality` is then ignored. @defaultValue `false` */
10
19
  lossless?: boolean;
11
- /** WebP near-lossless preprocessing strength, from 0 (lossy) to 100. */
20
+ /**
21
+ * Near-lossless preprocessing quality for lossless encoding, from 0
22
+ * (strongest pixel changes and smallest output) to 100 (no preprocessing).
23
+ *
24
+ * @defaultValue `100`
25
+ */
12
26
  nearLossless?: number;
13
27
  }
14
28
  interface AvifEncoding extends SizedEncoding {
29
+ /** Encode the image as AVIF. */
15
30
  codec: 'avif';
31
+ /** Lossy quality from 1 (smallest, lowest fidelity) to 100. @defaultValue `50` */
16
32
  quality?: number;
33
+ /** Encoder effort from 0 (fastest) to 100 (slowest). @defaultValue `40` */
17
34
  effort?: number;
35
+ /** Use lossless AVIF instead of lossy encoding, overriding `quality` and chroma subsampling. @defaultValue `false` */
18
36
  lossless?: boolean;
37
+ /**
38
+ * Chroma resolution: `4:2:0` reduces color detail for smaller files, while
39
+ * `4:4:4` preserves full color resolution. Lossless encoding always uses
40
+ * full color resolution.
41
+ *
42
+ * @defaultValue `'4:2:0'`
43
+ */
19
44
  chromaSubsampling?: '4:2:0' | '4:4:4';
20
45
  }
21
46
  interface JpegEncoding extends SizedEncoding {
47
+ /** Encode the image as JPEG. Alpha is not preserved. */
22
48
  codec: 'jpeg';
49
+ /** Lossy quality from 1 (smallest, lowest fidelity) to 100. @defaultValue `75` */
23
50
  quality?: number;
51
+ /**
52
+ * Chroma resolution: `4:2:0` reduces color detail for smaller files, while
53
+ * `4:4:4` preserves full color resolution.
54
+ *
55
+ * @defaultValue `'4:2:0'`
56
+ */
24
57
  chromaSubsampling?: '4:2:0' | '4:4:4';
25
58
  }
26
59
  interface PngEncoding extends SizedEncoding {
60
+ /** Encode the image as lossless PNG. */
27
61
  codec: 'png';
28
62
  }
29
63
  interface UastcEncoding extends SizedEncoding {
64
+ /** Encode the image in a GPU-ready KTX2 container. */
30
65
  codec: 'ktx2';
66
+ /** Use high-quality UASTC block compression for LDR pixels. */
31
67
  mode: 'uastc';
68
+ /**
69
+ * How the texture is sampled: `srgb` for color data such as albedo and
70
+ * emissive maps, or `linear` for data and normal maps.
71
+ *
72
+ * @defaultValue `'srgb'`
73
+ */
32
74
  colorSpace?: 'srgb' | 'linear';
75
+ /** Tune the encoder for tangent-space normal-map data. @defaultValue `false` */
33
76
  normalMap?: boolean;
77
+ /** Encoder effort from 0 (fastest) to 4 (slowest, highest quality). @defaultValue `2` */
34
78
  level?: 0 | 1 | 2 | 3 | 4;
79
+ /** Generate a complete mip chain for minified rendering. @defaultValue `true` */
35
80
  mipmaps?: boolean;
81
+ /** Apply Zstandard supercompression to reduce download size. @defaultValue `true` */
36
82
  zstd?: boolean;
83
+ /**
84
+ * Enable rate-distortion optimization. This can substantially reduce files
85
+ * at the cost of image fidelity; omit it to disable RDO.
86
+ */
37
87
  rdo?: {
88
+ /**
89
+ * Compression-to-quality tradeoff from 0.001 to 10. Larger values usually
90
+ * produce smaller files with more distortion.
91
+ *
92
+ * @defaultValue `1`
93
+ */
38
94
  lambda?: number;
39
95
  };
40
96
  }
41
97
  interface Etc1sEncoding extends SizedEncoding {
98
+ /** Encode the image in a GPU-ready KTX2 container. */
42
99
  codec: 'ktx2';
100
+ /** Use ETC1S compression for the smallest download size at lower fidelity than UASTC. */
43
101
  mode: 'etc1s';
102
+ /**
103
+ * How the texture is sampled: `srgb` for color data such as albedo and
104
+ * emissive maps, or `linear` for data and normal maps.
105
+ *
106
+ * @defaultValue `'srgb'`
107
+ */
44
108
  colorSpace?: 'srgb' | 'linear';
109
+ /** Tune the encoder for tangent-space normal-map data. @defaultValue `false` */
45
110
  normalMap?: boolean;
111
+ /** Quality from 1 (smallest, lowest fidelity) to 255. @defaultValue `128` */
46
112
  quality?: number;
113
+ /**
114
+ * Encoder effort from 0 (fastest) to 6 (slowest). Higher values search
115
+ * harder for a smaller file at the requested quality.
116
+ *
117
+ * @defaultValue `1`
118
+ */
47
119
  compression?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
120
+ /** Generate a complete mip chain for minified rendering. @defaultValue `true` */
48
121
  mipmaps?: boolean;
49
122
  }
50
123
  /**
@@ -57,73 +130,133 @@ interface Etc1sEncoding extends SizedEncoding {
57
130
  * be authored at their target resolution.
58
131
  */
59
132
  interface UastcHdrEncoding {
133
+ /** Encode the image in a GPU-ready KTX2 container. */
60
134
  codec: 'ktx2';
135
+ /**
136
+ * Use UASTC HDR for half-float EXR or Radiance HDR input. This mode does not
137
+ * support resizing or alpha and is only available to standalone image recipes.
138
+ */
61
139
  mode: 'uastc-hdr';
62
- /** Encoder effort. Higher is slower; rarely helps on low-frequency lightmaps. */
140
+ /**
141
+ * Encoder effort from 0 (fastest) to 4 (slowest). Higher levels rarely help
142
+ * low-frequency content such as diffuse lightmaps.
143
+ *
144
+ * @defaultValue `1`
145
+ */
63
146
  level?: 0 | 1 | 2 | 3 | 4;
147
+ /** Generate a complete mip chain for minified rendering. @defaultValue `true` */
64
148
  mipmaps?: boolean;
149
+ /** Apply Zstandard supercompression to reduce download size. @defaultValue `true` */
65
150
  zstd?: boolean;
66
151
  }
67
152
  type RasterEncoding = WebpEncoding | AvifEncoding | JpegEncoding | PngEncoding;
68
153
  type ImageEncoding = RasterEncoding | UastcEncoding | Etc1sEncoding | UastcHdrEncoding;
69
154
  type GlbProperty = 'accessor' | 'animation' | 'buffer' | 'camera' | 'material' | 'mesh' | 'node' | 'primitive' | 'primitiveTarget' | 'skin' | 'texture';
70
155
  type GlbMethodRecipe = {
156
+ /** Merge equivalent accessors, materials, meshes, skins, or textures. */
71
157
  method: 'dedup';
158
+ /** Keep otherwise-identical properties separate when their names differ. @defaultValue `false` */
72
159
  keepUniqueNames?: boolean;
160
+ /**
161
+ * Property kinds to merge.
162
+ *
163
+ * @defaultValue `['accessor', 'material', 'mesh', 'skin', 'texture']`
164
+ */
73
165
  propertyTypes?: readonly Extract<GlbProperty, 'accessor' | 'material' | 'mesh' | 'skin' | 'texture'>[];
74
166
  } | {
167
+ /** Remove unused GLB properties and optionally simplify redundant data. */
75
168
  method: 'prune';
169
+ /** Keep unused vertex attributes, such as UVs without an assigned texture. @defaultValue `false` */
76
170
  keepAttributes?: boolean;
171
+ /** Prevent a property with custom `extras` data from being removed. @defaultValue `false` */
77
172
  keepExtras?: boolean;
173
+ /** Keep empty leaf nodes that would otherwise be removed. @defaultValue `false` */
78
174
  keepLeaves?: boolean;
175
+ /** Keep single-color textures instead of replacing them with material factors. @defaultValue `false` */
79
176
  keepSolidTextures?: boolean;
177
+ /** Property kinds eligible for pruning. By default, every supported kind is eligible. */
80
178
  propertyTypes?: readonly GlbProperty[];
81
179
  } | {
180
+ /** Remove redundant animation keyframes while keeping motion within a tolerance. */
82
181
  method: 'resample';
182
+ /** Maximum permitted animation-value error. Must be greater than zero. @defaultValue `0.0001` */
83
183
  tolerance?: number;
184
+ /**
185
+ * Deduplicate and prune temporary or unused resources after resampling.
186
+ * Disable this when those resources must be preserved for a later step.
187
+ *
188
+ * @defaultValue `true`
189
+ */
84
190
  cleanup?: boolean;
85
191
  };
86
192
  type TextureSelector = {
193
+ /** Select textures by the material slots that reference them. */
87
194
  by: 'slot';
195
+ /** glTF material texture-slot names, such as `baseColorTexture` or `normalTexture`. */
88
196
  slots: readonly string[];
89
197
  } | {
198
+ /** Select textures by their authored GLB names. */
90
199
  by: 'name';
200
+ /** Exact, case-sensitive texture names. Names should be unique within the GLB. */
91
201
  names: readonly string[];
92
202
  } | {
203
+ /** Select textures by their zero-based order in the GLB. */
93
204
  by: 'index';
205
+ /** Non-negative texture indices. Indices can change when a source GLB is re-exported. */
94
206
  indices: readonly number[];
95
207
  };
96
208
  interface EmbeddedTextureRecipe {
209
+ /**
210
+ * Unique, non-empty name for this texture group in progress events and
211
+ * optimization reports. It does not rename textures in the GLB.
212
+ */
97
213
  label: string;
214
+ /** Textures to encode. A texture cannot be claimed by more than one selector. */
98
215
  select: TextureSelector;
216
+ /** Output encoding for the selected textures. UASTC HDR is not supported inside GLBs. */
99
217
  encode: ImageEncoding;
100
218
  }
101
219
  type GeometryEncoding = {
220
+ /** Compress geometry with `KHR_draco_mesh_compression` for broad decoder support. */
102
221
  codec: 'draco';
103
222
  } | {
223
+ /** Compress geometry with `EXT_meshopt_compression` for fast runtime decoding. */
104
224
  codec: 'meshopt';
105
225
  };
106
226
  interface GlbAssetRecipe {
227
+ /** Optimize a binary glTF (`.glb`) asset. */
107
228
  kind: 'glb';
229
+ /** Structural transforms to run in array order before texture and geometry encoding. @defaultValue `[]` */
108
230
  methods?: readonly GlbMethodRecipe[];
231
+ /** Embedded texture groups to encode. Unselected textures keep their source encoding. @defaultValue `[]` */
109
232
  textures?: readonly EmbeddedTextureRecipe[];
233
+ /** Geometry codec to apply, or `false` to leave geometry uncompressed. @defaultValue `false` */
110
234
  geometry?: GeometryEncoding | false;
111
235
  }
112
236
  interface ImageAssetRecipe {
237
+ /** Optimize a standalone raster or HDR image. */
113
238
  kind: 'image';
239
+ /** Output codec and its quality, size, and performance tradeoffs. */
114
240
  encode: ImageEncoding;
115
241
  }
116
242
  type AssetRecipe = GlbAssetRecipe | ImageAssetRecipe;
117
243
  type NormalizedImageEncoding = (Required<Omit<WebpEncoding, 'maxSize'>> & Pick<WebpEncoding, 'maxSize'>) | (Required<Omit<AvifEncoding, 'maxSize'>> & Pick<AvifEncoding, 'maxSize'>) | (Required<Omit<JpegEncoding, 'maxSize'>> & Pick<JpegEncoding, 'maxSize'>) | PngEncoding | (Required<Omit<UastcEncoding, 'maxSize' | 'rdo'>> & Pick<UastcEncoding, 'maxSize' | 'rdo'>) | (Required<Omit<Etc1sEncoding, 'maxSize'>> & Pick<Etc1sEncoding, 'maxSize'>) | Required<UastcHdrEncoding>;
118
244
  type NormalizedAssetRecipe = {
245
+ /** Identifies the normalized recipe as a standalone image recipe. */
119
246
  kind: 'image';
247
+ /** Image encoding with every codec default resolved. */
120
248
  encode: NormalizedImageEncoding;
121
249
  } | {
250
+ /** Identifies the normalized recipe as a binary glTF recipe. */
122
251
  kind: 'glb';
252
+ /** Structural transforms to run, with an omitted value resolved to an empty array. */
123
253
  methods: readonly GlbMethodRecipe[];
254
+ /** Embedded texture groups with every codec default resolved. */
124
255
  textures: readonly (Omit<EmbeddedTextureRecipe, 'encode'> & {
256
+ /** Output encoding with every codec default resolved. */
125
257
  encode: NormalizedImageEncoding;
126
258
  })[];
259
+ /** Resolved geometry codec, or `false` when geometry compression is disabled. */
127
260
  geometry: GeometryEncoding | false;
128
261
  };
129
262
 
@@ -293,12 +426,33 @@ interface OptimizeFileOptions extends OptimizationOptions {
293
426
  declare function optimizeFile({ input, output, recipe, ...options }: OptimizeFileOptions): Promise<ChopperResult<OptimizationOutput>>;
294
427
 
295
428
  interface ChopperRule {
429
+ /**
430
+ * Glob patterns for source assets, evaluated relative to the project directory.
431
+ * A rule runs once for every file matched by at least one pattern.
432
+ */
296
433
  include: readonly string[];
434
+ /**
435
+ * Glob patterns removed from `include` matches, evaluated relative to the
436
+ * project directory. Omit this property to exclude nothing.
437
+ */
297
438
  exclude?: readonly string[];
439
+ /** Optimization recipe applied to every asset matched by this rule. */
298
440
  recipe: AssetRecipe;
441
+ /**
442
+ * Output path template relative to {@link ChopperConfig.output}. Supports
443
+ * `{dir}` (source directory), `{name}` (source basename), and `{ext}`
444
+ * (recipe output extension).
445
+ *
446
+ * @defaultValue `'{dir}/{name}.{ext}'`
447
+ */
299
448
  path?: string;
300
449
  }
301
450
  interface ChopperConfig {
451
+ /**
452
+ * Directory where optimized assets are written. Relative paths are resolved
453
+ * from the project directory, and the resolved directory must remain inside
454
+ * the project.
455
+ */
302
456
  output: string;
303
457
  /**
304
458
  * Every rule matches independently, so one file can be claimed by several of
@@ -308,6 +462,13 @@ interface ChopperConfig {
308
462
  * one input can still fan out to several encodings.
309
463
  */
310
464
  rules: readonly ChopperRule[];
465
+ /**
466
+ * Maximum number of assets optimized at the same time. Must be a positive
467
+ * integer; lower values reduce peak CPU and memory use, while higher values
468
+ * may finish a batch sooner.
469
+ *
470
+ * @defaultValue `2`
471
+ */
311
472
  concurrency?: number;
312
473
  }
313
474
 
@@ -321,6 +482,7 @@ interface BatchFileReport {
321
482
  beforeBytes?: number;
322
483
  afterBytes?: number;
323
484
  engineId?: string;
485
+ optimization?: OptimizationReport;
324
486
  error?: {
325
487
  code: string;
326
488
  message: string;
@@ -41,6 +41,12 @@ var failure = (code, message, cause) => ({
41
41
  error: cause === void 0 ? { code, message } : { code, message, cause }
42
42
  });
43
43
 
44
+ // src/core/object-validation.ts
45
+ function unknownFieldIssue(value, allowedFields, subject) {
46
+ const field = Object.keys(value).find((key) => !allowedFields.includes(key));
47
+ return field ? `Unknown ${subject} field: ${field}` : void 0;
48
+ }
49
+
44
50
  // src/core/validation.ts
45
51
  function validateRecipe(recipe) {
46
52
  const shapeIssue = recipeShapeIssue(recipe);
@@ -80,15 +86,26 @@ function recipeShapeIssue(recipe) {
80
86
  return `Unsupported recipe kind: ${String(candidate.kind)}`;
81
87
  }
82
88
  if (candidate.kind === "image") {
89
+ const unknownField2 = unknownFieldIssue(candidate, ["kind", "encode"], "image recipe");
90
+ if (unknownField2) return unknownField2;
83
91
  return encodingShapeIssue(candidate.encode);
84
92
  }
93
+ const unknownField = unknownFieldIssue(candidate, ["kind", "methods", "textures", "geometry"], "GLB recipe");
94
+ if (unknownField) return unknownField;
85
95
  if (candidate.methods !== void 0 && !Array.isArray(candidate.methods)) {
86
96
  return "GLB methods must be an array";
87
97
  }
88
98
  for (const method of candidate.methods ?? []) {
89
- if (!method || typeof method !== "object" || !["dedup", "prune", "resample"].includes(String(method.method))) {
99
+ if (!method || typeof method !== "object") {
100
+ return "GLB methods contain an unsupported transform";
101
+ }
102
+ const entry = method;
103
+ if (!["dedup", "prune", "resample"].includes(String(entry.method))) {
90
104
  return "GLB methods contain an unsupported transform";
91
105
  }
106
+ const allowedFields = entry.method === "dedup" ? ["method", "keepUniqueNames", "propertyTypes"] : entry.method === "prune" ? ["method", "keepAttributes", "keepExtras", "keepLeaves", "keepSolidTextures", "propertyTypes"] : ["method", "tolerance", "cleanup"];
107
+ const unknownField2 = unknownFieldIssue(entry, allowedFields, "GLB method");
108
+ if (unknownField2) return unknownField2;
92
109
  }
93
110
  if (candidate.textures !== void 0 && !Array.isArray(candidate.textures)) {
94
111
  return "GLB textures must be an array";
@@ -96,19 +113,32 @@ function recipeShapeIssue(recipe) {
96
113
  for (const texture of candidate.textures ?? []) {
97
114
  if (!texture || typeof texture !== "object") return "Texture recipes must be objects";
98
115
  const entry = texture;
116
+ const unknownField2 = unknownFieldIssue(entry, ["label", "select", "encode"], "texture recipe");
117
+ if (unknownField2) return unknownField2;
99
118
  if (typeof entry.label !== "string") return "Texture recipe labels must be strings";
100
119
  if (!entry.select || typeof entry.select !== "object") return "Texture recipes require a selector";
101
120
  const selector = entry.select;
102
- const values = selector[selector.by === "slot" ? "slots" : selector.by === "name" ? "names" : "indices"];
103
- if (!["slot", "name", "index"].includes(String(selector.by)) || !Array.isArray(values)) {
121
+ if (!["slot", "name", "index"].includes(String(selector.by))) {
104
122
  return "Texture recipe selectors are malformed";
105
123
  }
124
+ const valueField = selector.by === "slot" ? "slots" : selector.by === "name" ? "names" : "indices";
125
+ const selectorUnknownField = unknownFieldIssue(selector, ["by", valueField], "texture selector");
126
+ if (selectorUnknownField) return selectorUnknownField;
127
+ if (!Array.isArray(selector[valueField])) return "Texture recipe selectors are malformed";
106
128
  const encodingIssue2 = encodingShapeIssue(entry.encode);
107
129
  if (encodingIssue2) return encodingIssue2;
108
130
  }
109
131
  if (candidate.geometry !== void 0 && candidate.geometry !== false && (!candidate.geometry || typeof candidate.geometry !== "object" || !["draco", "meshopt"].includes(String(candidate.geometry.codec)))) {
110
132
  return "GLB geometry must be false, Draco, or Meshopt";
111
133
  }
134
+ if (candidate.geometry && typeof candidate.geometry === "object") {
135
+ const geometryUnknownField = unknownFieldIssue(
136
+ candidate.geometry,
137
+ ["codec"],
138
+ "GLB geometry"
139
+ );
140
+ if (geometryUnknownField) return geometryUnknownField;
141
+ }
112
142
  return void 0;
113
143
  }
114
144
  function encodingShapeIssue(encoding) {
@@ -123,6 +153,16 @@ function encodingShapeIssue(encoding) {
123
153
  if (candidate.codec === "ktx2" && candidate.mode === "uastc-hdr" && candidate.maxSize !== void 0) {
124
154
  return "UASTC HDR does not support maxSize; resizing requires a decoded 8-bit image. Author the source at its target resolution.";
125
155
  }
156
+ const allowedFields = candidate.codec === "webp" ? ["codec", "maxSize", "quality", "effort", "lossless", "nearLossless"] : candidate.codec === "avif" ? ["codec", "maxSize", "quality", "effort", "lossless", "chromaSubsampling"] : candidate.codec === "jpeg" ? ["codec", "maxSize", "quality", "chromaSubsampling"] : candidate.codec === "png" ? ["codec", "maxSize"] : candidate.mode === "uastc" ? ["codec", "mode", "maxSize", "colorSpace", "normalMap", "level", "mipmaps", "zstd", "rdo"] : candidate.mode === "etc1s" ? ["codec", "mode", "maxSize", "colorSpace", "normalMap", "quality", "compression", "mipmaps"] : ["codec", "mode", "level", "mipmaps", "zstd"];
157
+ const unknownField = unknownFieldIssue(candidate, allowedFields, "image encoding");
158
+ if (unknownField) return unknownField;
159
+ if (candidate.codec === "ktx2" && candidate.mode === "uastc" && candidate.rdo !== void 0) {
160
+ if (!candidate.rdo || typeof candidate.rdo !== "object" || Array.isArray(candidate.rdo)) {
161
+ return "UASTC RDO must be an object";
162
+ }
163
+ const rdoUnknownField = unknownFieldIssue(candidate.rdo, ["lambda"], "UASTC RDO");
164
+ if (rdoUnknownField) return rdoUnknownField;
165
+ }
126
166
  return void 0;
127
167
  }
128
168
  function recipeIssue(recipe) {
@@ -390,7 +430,7 @@ function latin1(bytes) {
390
430
 
391
431
  // package.json
392
432
  var package_default = {
393
- version: "0.5.1"};
433
+ version: "0.5.3"};
394
434
 
395
435
  // src/codecs/manifest.ts
396
436
  var CODEC_MANIFEST_HASH = "2046d60deb0f9f224743f4404f3c6e8ba5399f81b2811fa50466e93fce3bd322";
@@ -1283,6 +1323,31 @@ async function optimizeFile({
1283
1323
  return failure("io_failed", `Could not optimize ${input}`, cause);
1284
1324
  }
1285
1325
  }
1326
+ function validateConfig(config) {
1327
+ if (!config || typeof config !== "object" || Array.isArray(config)) {
1328
+ return failure("invalid_recipe", "Config must be an object");
1329
+ }
1330
+ const candidate = config;
1331
+ const unknownField = unknownFieldIssue(candidate, ["output", "rules", "concurrency"], "config");
1332
+ if (unknownField) return failure("invalid_recipe", unknownField);
1333
+ if (!Array.isArray(candidate.rules)) {
1334
+ return failure("invalid_recipe", "Config rules must be an array");
1335
+ }
1336
+ if (candidate.concurrency !== void 0 && (typeof candidate.concurrency !== "number" || !Number.isInteger(candidate.concurrency) || candidate.concurrency < 1)) {
1337
+ return failure("invalid_recipe", "Config concurrency must be a positive integer");
1338
+ }
1339
+ for (const [index, rule] of candidate.rules.entries()) {
1340
+ if (!rule || typeof rule !== "object" || Array.isArray(rule)) {
1341
+ return failure("invalid_recipe", `Config rule ${index} must be an object`);
1342
+ }
1343
+ const entry = rule;
1344
+ const ruleUnknownField = unknownFieldIssue(entry, ["include", "exclude", "recipe", "path"], `config rule ${index}`);
1345
+ if (ruleUnknownField) return failure("invalid_recipe", ruleUnknownField);
1346
+ const recipe = validateRecipe(entry.recipe);
1347
+ if (!recipe.ok) return recipe;
1348
+ }
1349
+ return success(config);
1350
+ }
1286
1351
  async function loadConfig(path = "chopper.config.ts") {
1287
1352
  const absolutePath = resolve(path);
1288
1353
  try {
@@ -1291,13 +1356,11 @@ async function loadConfig(path = "chopper.config.ts") {
1291
1356
  const config = await jiti.import(absolutePath, {
1292
1357
  default: true
1293
1358
  });
1294
- if (!config || !Array.isArray(config.rules)) {
1359
+ const validated = validateConfig(config);
1360
+ if (!validated.ok && validated.error.message === "Config must be an object") {
1295
1361
  return failure("invalid_recipe", `${path} must export a Chopper config`);
1296
1362
  }
1297
- if (config.concurrency !== void 0 && (!Number.isInteger(config.concurrency) || config.concurrency < 1)) {
1298
- return failure("invalid_recipe", "Config concurrency must be a positive integer");
1299
- }
1300
- return success(config);
1363
+ return validated;
1301
1364
  } catch (cause) {
1302
1365
  return failure("io_failed", `Could not load ${path}`, cause);
1303
1366
  }
@@ -1318,6 +1381,9 @@ async function runConfig(options = {}) {
1318
1381
  return success({ mode, output: relative(cwd, resolve(cwd, loaded.data.output)), files, totals: summarize(files) });
1319
1382
  }
1320
1383
  async function planAssets(config, cwd) {
1384
+ const validated = validateConfig(config);
1385
+ if (!validated.ok) return validated;
1386
+ config = validated.data;
1321
1387
  const planned = [];
1322
1388
  if (typeof config.output !== "string" || !config.output) {
1323
1389
  return failure("invalid_recipe", "Config output directory is required");
@@ -1382,7 +1448,8 @@ async function executeAsset(asset, mode, cwd, chopper2) {
1382
1448
  output: relative(cwd, asset.output),
1383
1449
  beforeBytes: source.byteLength,
1384
1450
  afterBytes: optimized.data.bytes.byteLength,
1385
- engineId: optimized.data.report.engineId
1451
+ engineId: optimized.data.report.engineId,
1452
+ optimization: optimized.data.report
1386
1453
  };
1387
1454
  if (mode === "report") return { ...base, status: "reported" };
1388
1455
  if (mode === "check") {