@joycostudio/chopper 0.5.4 → 0.7.0
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/README.md +65 -1
- package/codecs/manifest.json +1 -1
- package/dist/browser/{chunk-G4APMK7P.js → chunk-33SSR3TA.js} +3 -3
- package/dist/browser/{chunk-G4APMK7P.js.map → chunk-33SSR3TA.js.map} +1 -1
- package/dist/browser/{in-process-5DUPLJNY.js → in-process-KCP5E5RP.js} +3 -3
- package/dist/browser/{in-process-5DUPLJNY.js.map → in-process-KCP5E5RP.js.map} +1 -1
- package/dist/browser/index.js +2 -2
- package/dist/browser/worker.js +4 -2
- package/dist/browser/worker.js.map +1 -1
- package/dist/cli/index.js +217 -42
- package/dist/cli/index.js.map +1 -1
- package/dist/config/index.d.ts +68 -7
- package/dist/config/index.js +416 -2
- package/dist/config/index.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/node/index.d.ts +16 -4
- package/dist/node/index.js +219 -42
- package/dist/node/index.js.map +1 -1
- package/package.json +4 -2
package/dist/config/index.d.ts
CHANGED
|
@@ -252,6 +252,21 @@ declare const DATA_TEXTURE_SLOTS: readonly ["occlusionTexture", "metallicRoughne
|
|
|
252
252
|
/** Defines a type-checked optimization recipe while preserving its inferred literal types. */
|
|
253
253
|
declare const defineRecipe: <const T extends AssetRecipe>(recipe: T & NoUnknownFields<T, AssetRecipe>) => T;
|
|
254
254
|
|
|
255
|
+
type ChopperErrorCode = 'unsupported_asset' | 'invalid_recipe' | 'codec_load_failed' | 'decode_failed' | 'encode_failed' | 'validation_failed' | 'aborted' | 'io_failed';
|
|
256
|
+
interface ChopperFailure {
|
|
257
|
+
ok: false;
|
|
258
|
+
error: {
|
|
259
|
+
code: ChopperErrorCode;
|
|
260
|
+
message: string;
|
|
261
|
+
cause?: unknown;
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
interface ChopperSuccess<T> {
|
|
265
|
+
ok: true;
|
|
266
|
+
data: T;
|
|
267
|
+
}
|
|
268
|
+
type ChopperResult<T> = ChopperSuccess<T> | ChopperFailure;
|
|
269
|
+
|
|
255
270
|
interface ChopperRule {
|
|
256
271
|
/**
|
|
257
272
|
* Glob patterns for source assets, evaluated relative to the project directory.
|
|
@@ -276,11 +291,23 @@ interface ChopperRule {
|
|
|
276
291
|
}
|
|
277
292
|
interface ChopperConfig {
|
|
278
293
|
/**
|
|
279
|
-
*
|
|
280
|
-
* from the project directory, and the resolved directory
|
|
281
|
-
* the project.
|
|
294
|
+
* Filesystem root used by Node and the CLI for generated bytes. Relative
|
|
295
|
+
* paths are resolved from the project directory, and the resolved directory
|
|
296
|
+
* must remain inside the project. This directory is not included in
|
|
297
|
+
* {@link PlannedConfigAsset.output}.
|
|
298
|
+
*
|
|
299
|
+
* @defaultValue `'.chopper-output'`
|
|
282
300
|
*/
|
|
283
|
-
output
|
|
301
|
+
output?: string;
|
|
302
|
+
/**
|
|
303
|
+
* Reserved sibling directory used for logical generated asset paths.
|
|
304
|
+
*
|
|
305
|
+
* Given `assets/wall.png` and `outputDirectory: 'compressed'`, a WebP
|
|
306
|
+
* recipe resolves to `assets/compressed/wall.webp`. Inputs already inside
|
|
307
|
+
* this directory are classified as generated outputs and are never
|
|
308
|
+
* processed again.
|
|
309
|
+
*/
|
|
310
|
+
outputDirectory?: string;
|
|
284
311
|
/**
|
|
285
312
|
* Every rule matches independently, so one file can be claimed by several of
|
|
286
313
|
* them. Where two rules resolve the same output path for the same input, the
|
|
@@ -298,7 +325,41 @@ interface ChopperConfig {
|
|
|
298
325
|
*/
|
|
299
326
|
concurrency?: number;
|
|
300
327
|
}
|
|
301
|
-
|
|
302
|
-
|
|
328
|
+
interface PlannedConfigAsset {
|
|
329
|
+
/** Project-relative POSIX path tested against the config globs. */
|
|
330
|
+
input: string;
|
|
331
|
+
/** Logical generated asset path, relative to the filesystem output root. */
|
|
332
|
+
output: string;
|
|
333
|
+
/** The winning Chopper rule that produces this output. */
|
|
334
|
+
rule: ChopperRule;
|
|
335
|
+
}
|
|
336
|
+
type ConfigRoute = {
|
|
337
|
+
kind: 'source';
|
|
338
|
+
input: string;
|
|
339
|
+
outputs: PlannedConfigAsset[];
|
|
340
|
+
} | {
|
|
341
|
+
kind: 'output';
|
|
342
|
+
input: string;
|
|
343
|
+
} | {
|
|
344
|
+
kind: 'unmatched';
|
|
345
|
+
input: string;
|
|
346
|
+
};
|
|
347
|
+
interface ConfigAssetPlanner {
|
|
348
|
+
/** Classifies and plans one known project-relative asset without I/O. */
|
|
349
|
+
route(input: string): ChopperResult<ConfigRoute>;
|
|
350
|
+
/** Plans one known project-relative asset without reading the filesystem. */
|
|
351
|
+
resolve(input: string): ChopperResult<PlannedConfigAsset[]>;
|
|
352
|
+
}
|
|
353
|
+
type ConfiguredChopper<T extends ChopperConfig = ChopperConfig> = Omit<T, 'output'> & {
|
|
354
|
+
output: T['output'] extends string ? T['output'] : string;
|
|
355
|
+
} & ConfigAssetPlanner;
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Defines a type-checked Chopper project config and binds a browser-safe asset
|
|
359
|
+
* planner to it while preserving its inferred literal types.
|
|
360
|
+
*/
|
|
361
|
+
declare function createChopper<const T extends ChopperConfig>(config: T & NoUnknownFields<T, ChopperConfig>): ConfiguredChopper<T>;
|
|
362
|
+
/** @deprecated Use {@link createChopper} instead. */
|
|
363
|
+
declare const defineConfig: typeof createChopper;
|
|
303
364
|
|
|
304
|
-
export { COLOR_TEXTURE_SLOTS, type ChopperConfig, type ChopperRule, DATA_TEXTURE_SLOTS, DIRECTIONAL_TEXTURE_SLOTS, defineConfig, defineRecipe };
|
|
365
|
+
export { COLOR_TEXTURE_SLOTS, type ChopperConfig, type ChopperRule, type ConfigAssetPlanner, type ConfigRoute, type ConfiguredChopper, DATA_TEXTURE_SLOTS, DIRECTIONAL_TEXTURE_SLOTS, type PlannedConfigAsset, createChopper, defineConfig, defineRecipe };
|
package/dist/config/index.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import picomatch from 'picomatch';
|
|
2
|
+
|
|
3
|
+
// src/config/planner.ts
|
|
4
|
+
|
|
1
5
|
// src/core/recipes.ts
|
|
2
6
|
var COLOR_TEXTURE_SLOTS = [
|
|
3
7
|
"baseColorTexture",
|
|
@@ -19,10 +23,420 @@ var DATA_TEXTURE_SLOTS = [
|
|
|
19
23
|
"iridescenceThicknessTexture"
|
|
20
24
|
];
|
|
21
25
|
var defineRecipe = (recipe) => recipe;
|
|
26
|
+
function outputExtension(encoding) {
|
|
27
|
+
if (encoding.codec === "ktx2") return ".ktx2";
|
|
28
|
+
if (encoding.codec === "jpeg") return ".jpg";
|
|
29
|
+
return `.${encoding.codec}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/core/result.ts
|
|
33
|
+
var success = (data) => ({ ok: true, data });
|
|
34
|
+
var failure = (code, message, cause) => ({
|
|
35
|
+
ok: false,
|
|
36
|
+
error: cause === void 0 ? { code, message } : { code, message, cause }
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// src/core/object-validation.ts
|
|
40
|
+
function unknownFieldIssue(value, allowedFields, subject) {
|
|
41
|
+
const field = Object.keys(value).find((key) => !allowedFields.includes(key));
|
|
42
|
+
return field ? `Unknown ${subject} field: ${field}` : void 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/core/validation.ts
|
|
46
|
+
function validateRecipe(recipe) {
|
|
47
|
+
const shapeIssue = recipeShapeIssue(recipe);
|
|
48
|
+
if (shapeIssue) return failure("invalid_recipe", shapeIssue);
|
|
49
|
+
try {
|
|
50
|
+
const typedRecipe = recipe;
|
|
51
|
+
const issue = recipeIssue(typedRecipe);
|
|
52
|
+
return issue ? failure("invalid_recipe", issue) : success(typedRecipe);
|
|
53
|
+
} catch {
|
|
54
|
+
return failure("invalid_recipe", "Recipe contains malformed fields");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function recipeShapeIssue(recipe) {
|
|
58
|
+
if (!recipe || typeof recipe !== "object") return "Recipe must be an object";
|
|
59
|
+
const candidate = recipe;
|
|
60
|
+
if (candidate.kind !== "image" && candidate.kind !== "glb") {
|
|
61
|
+
return `Unsupported recipe kind: ${String(candidate.kind)}`;
|
|
62
|
+
}
|
|
63
|
+
if (candidate.kind === "image") {
|
|
64
|
+
const unknownField2 = unknownFieldIssue(candidate, ["kind", "encode"], "image recipe");
|
|
65
|
+
if (unknownField2) return unknownField2;
|
|
66
|
+
return encodingShapeIssue(candidate.encode);
|
|
67
|
+
}
|
|
68
|
+
const unknownField = unknownFieldIssue(candidate, ["kind", "methods", "textures", "geometry"], "GLB recipe");
|
|
69
|
+
if (unknownField) return unknownField;
|
|
70
|
+
if (candidate.methods !== void 0 && !Array.isArray(candidate.methods)) {
|
|
71
|
+
return "GLB methods must be an array";
|
|
72
|
+
}
|
|
73
|
+
for (const method of candidate.methods ?? []) {
|
|
74
|
+
if (!method || typeof method !== "object") {
|
|
75
|
+
return "GLB methods contain an unsupported transform";
|
|
76
|
+
}
|
|
77
|
+
const entry = method;
|
|
78
|
+
if (!["dedup", "prune", "resample"].includes(String(entry.method))) {
|
|
79
|
+
return "GLB methods contain an unsupported transform";
|
|
80
|
+
}
|
|
81
|
+
const allowedFields = entry.method === "dedup" ? ["method", "keepUniqueNames", "propertyTypes"] : entry.method === "prune" ? ["method", "keepAttributes", "keepExtras", "keepLeaves", "keepSolidTextures", "propertyTypes"] : ["method", "tolerance", "cleanup"];
|
|
82
|
+
const unknownField2 = unknownFieldIssue(entry, allowedFields, "GLB method");
|
|
83
|
+
if (unknownField2) return unknownField2;
|
|
84
|
+
}
|
|
85
|
+
if (candidate.textures !== void 0 && !Array.isArray(candidate.textures)) {
|
|
86
|
+
return "GLB textures must be an array";
|
|
87
|
+
}
|
|
88
|
+
for (const texture of candidate.textures ?? []) {
|
|
89
|
+
if (!texture || typeof texture !== "object") return "Texture recipes must be objects";
|
|
90
|
+
const entry = texture;
|
|
91
|
+
const unknownField2 = unknownFieldIssue(entry, ["label", "select", "encode"], "texture recipe");
|
|
92
|
+
if (unknownField2) return unknownField2;
|
|
93
|
+
if (typeof entry.label !== "string") return "Texture recipe labels must be strings";
|
|
94
|
+
if (!entry.select || typeof entry.select !== "object") return "Texture recipes require a selector";
|
|
95
|
+
const selector = entry.select;
|
|
96
|
+
if (!["slot", "name", "index"].includes(String(selector.by))) {
|
|
97
|
+
return "Texture recipe selectors are malformed";
|
|
98
|
+
}
|
|
99
|
+
const valueField = selector.by === "slot" ? "slots" : selector.by === "name" ? "names" : "indices";
|
|
100
|
+
const selectorUnknownField = unknownFieldIssue(selector, ["by", valueField], "texture selector");
|
|
101
|
+
if (selectorUnknownField) return selectorUnknownField;
|
|
102
|
+
if (!Array.isArray(selector[valueField])) return "Texture recipe selectors are malformed";
|
|
103
|
+
const encodingIssue2 = encodingShapeIssue(entry.encode);
|
|
104
|
+
if (encodingIssue2) return encodingIssue2;
|
|
105
|
+
}
|
|
106
|
+
if (candidate.geometry !== void 0 && candidate.geometry !== false && (!candidate.geometry || typeof candidate.geometry !== "object" || !["draco", "meshopt"].includes(String(candidate.geometry.codec)))) {
|
|
107
|
+
return "GLB geometry must be false, Draco, or Meshopt";
|
|
108
|
+
}
|
|
109
|
+
if (candidate.geometry && typeof candidate.geometry === "object") {
|
|
110
|
+
const geometryUnknownField = unknownFieldIssue(
|
|
111
|
+
candidate.geometry,
|
|
112
|
+
["codec"],
|
|
113
|
+
"GLB geometry"
|
|
114
|
+
);
|
|
115
|
+
if (geometryUnknownField) return geometryUnknownField;
|
|
116
|
+
}
|
|
117
|
+
return void 0;
|
|
118
|
+
}
|
|
119
|
+
function encodingShapeIssue(encoding) {
|
|
120
|
+
if (!encoding || typeof encoding !== "object") return "Image encoding must be an object";
|
|
121
|
+
const candidate = encoding;
|
|
122
|
+
if (!["webp", "avif", "jpeg", "png", "ktx2"].includes(String(candidate.codec))) {
|
|
123
|
+
return `Unsupported image codec: ${String(candidate.codec)}`;
|
|
124
|
+
}
|
|
125
|
+
if (candidate.codec === "ktx2" && candidate.mode !== "uastc" && candidate.mode !== "etc1s" && candidate.mode !== "uastc-hdr") {
|
|
126
|
+
return `Unsupported KTX2 mode: ${String(candidate.mode)}`;
|
|
127
|
+
}
|
|
128
|
+
if (candidate.codec === "ktx2" && candidate.mode === "uastc-hdr" && candidate.maxSize !== void 0) {
|
|
129
|
+
return "UASTC HDR does not support maxSize; resizing requires a decoded 8-bit image. Author the source at its target resolution.";
|
|
130
|
+
}
|
|
131
|
+
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"];
|
|
132
|
+
const unknownField = unknownFieldIssue(candidate, allowedFields, "image encoding");
|
|
133
|
+
if (unknownField) return unknownField;
|
|
134
|
+
if (candidate.codec === "ktx2" && candidate.mode === "uastc" && candidate.rdo !== void 0) {
|
|
135
|
+
if (!candidate.rdo || typeof candidate.rdo !== "object" || Array.isArray(candidate.rdo)) {
|
|
136
|
+
return "UASTC RDO must be an object";
|
|
137
|
+
}
|
|
138
|
+
const rdoUnknownField = unknownFieldIssue(candidate.rdo, ["lambda"], "UASTC RDO");
|
|
139
|
+
if (rdoUnknownField) return rdoUnknownField;
|
|
140
|
+
}
|
|
141
|
+
return void 0;
|
|
142
|
+
}
|
|
143
|
+
function recipeIssue(recipe) {
|
|
144
|
+
if (recipe.kind === "image") return encodingIssue(recipe.encode);
|
|
145
|
+
const labels = /* @__PURE__ */ new Set();
|
|
146
|
+
const selectors = /* @__PURE__ */ new Set();
|
|
147
|
+
for (const texture of recipe.textures ?? []) {
|
|
148
|
+
if (!texture.label) return "Texture recipe labels cannot be empty";
|
|
149
|
+
if (labels.has(texture.label)) {
|
|
150
|
+
return `Duplicate texture recipe label: ${texture.label}`;
|
|
151
|
+
}
|
|
152
|
+
labels.add(texture.label);
|
|
153
|
+
const values = selectorValues(texture.select);
|
|
154
|
+
if (!values.ok) return values.error.message;
|
|
155
|
+
if (!values.data.length) {
|
|
156
|
+
return `Texture recipe "${texture.label}" has an empty selector`;
|
|
157
|
+
}
|
|
158
|
+
for (const value of values.data) {
|
|
159
|
+
const key = `${texture.select.by}:${value}`;
|
|
160
|
+
if (selectors.has(key)) {
|
|
161
|
+
return `Texture selector appears in more than one recipe: ${key}`;
|
|
162
|
+
}
|
|
163
|
+
selectors.add(key);
|
|
164
|
+
}
|
|
165
|
+
if (texture.encode.codec === "ktx2" && texture.encode.mode === "uastc-hdr") {
|
|
166
|
+
return `Texture recipe "${texture.label}" uses UASTC HDR, which KHR_texture_basisu does not cover; HDR is only supported for standalone image recipes`;
|
|
167
|
+
}
|
|
168
|
+
const issue = encodingIssue(texture.encode);
|
|
169
|
+
if (issue) return issue;
|
|
170
|
+
}
|
|
171
|
+
for (const method of recipe.methods ?? []) {
|
|
172
|
+
if (method.method === "resample" && (method.tolerance ?? 1e-4) <= 0) {
|
|
173
|
+
return "Resample tolerance must be greater than zero";
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return void 0;
|
|
177
|
+
}
|
|
178
|
+
function selectorValues(selector) {
|
|
179
|
+
if (selector.by === "slot") return success(selector.slots);
|
|
180
|
+
if (selector.by === "name") return success(selector.names);
|
|
181
|
+
for (const index of selector.indices) {
|
|
182
|
+
if (!Number.isInteger(index) || index < 0) {
|
|
183
|
+
return failure("invalid_recipe", "Texture indices must be non-negative integers");
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return success(selector.indices);
|
|
187
|
+
}
|
|
188
|
+
function encodingIssue(encoding) {
|
|
189
|
+
if ("maxSize" in encoding && encoding.maxSize) {
|
|
190
|
+
const [width, height] = encoding.maxSize;
|
|
191
|
+
if (!Number.isInteger(width) || width < 1 || !Number.isInteger(height) || height < 1) {
|
|
192
|
+
return "Texture maxSize must contain positive integers";
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if ("quality" in encoding && encoding.quality !== void 0) {
|
|
196
|
+
const max = encoding.codec === "ktx2" ? 255 : 100;
|
|
197
|
+
if (encoding.quality < 1 || encoding.quality > max) {
|
|
198
|
+
return `${encoding.codec} quality must be between 1 and ${max}`;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if ("effort" in encoding && encoding.effort !== void 0) {
|
|
202
|
+
if (encoding.effort < 0 || encoding.effort > 100) {
|
|
203
|
+
return `${encoding.codec} effort must be between 0 and 100`;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (encoding.codec === "webp" && encoding.nearLossless !== void 0 && (encoding.nearLossless < 0 || encoding.nearLossless > 100)) {
|
|
207
|
+
return "WebP nearLossless must be between 0 and 100";
|
|
208
|
+
}
|
|
209
|
+
if (encoding.codec === "ktx2" && encoding.mode === "uastc") {
|
|
210
|
+
if (encoding.level !== void 0 && (!Number.isInteger(encoding.level) || encoding.level < 0 || encoding.level > 4)) {
|
|
211
|
+
return "UASTC level must be an integer between 0 and 4";
|
|
212
|
+
}
|
|
213
|
+
const lambda = encoding.rdo?.lambda;
|
|
214
|
+
if (lambda !== void 0 && (lambda < 1e-3 || lambda > 10)) {
|
|
215
|
+
return "UASTC RDO lambda must be between 0.001 and 10";
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (encoding.codec === "ktx2" && encoding.mode === "uastc-hdr" && encoding.level !== void 0 && (!Number.isInteger(encoding.level) || encoding.level < 0 || encoding.level > 4)) {
|
|
219
|
+
return "UASTC HDR level must be an integer between 0 and 4";
|
|
220
|
+
}
|
|
221
|
+
if (encoding.codec === "ktx2" && encoding.mode === "etc1s" && encoding.compression !== void 0 && (!Number.isInteger(encoding.compression) || encoding.compression < 0 || encoding.compression > 6)) {
|
|
222
|
+
return "ETC1S compression must be an integer between 0 and 6";
|
|
223
|
+
}
|
|
224
|
+
return void 0;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// src/config/validation.ts
|
|
228
|
+
var DEFAULT_OUTPUT = ".chopper-output";
|
|
229
|
+
function validateConfig(config) {
|
|
230
|
+
if (!config || typeof config !== "object" || Array.isArray(config)) {
|
|
231
|
+
return failure("invalid_recipe", "Config must be an object");
|
|
232
|
+
}
|
|
233
|
+
const candidate = config;
|
|
234
|
+
const unknownField = unknownFieldIssue(candidate, ["output", "outputDirectory", "rules", "concurrency"], "config");
|
|
235
|
+
if (unknownField) return failure("invalid_recipe", unknownField);
|
|
236
|
+
if (candidate.output !== void 0 && (typeof candidate.output !== "string" || !candidate.output)) {
|
|
237
|
+
return failure("invalid_recipe", "Config output must be a non-empty string");
|
|
238
|
+
}
|
|
239
|
+
if (candidate.output !== void 0 && !isProjectRelativePath(candidate.output, true)) {
|
|
240
|
+
return failure("invalid_recipe", `Output directory must be a project-relative POSIX path: ${candidate.output}`);
|
|
241
|
+
}
|
|
242
|
+
if (candidate.outputDirectory !== void 0 && !isSafePathSegment(candidate.outputDirectory)) {
|
|
243
|
+
return failure(
|
|
244
|
+
"invalid_recipe",
|
|
245
|
+
`Config outputDirectory must be exactly one safe path segment: ${candidate.outputDirectory}`
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
if (!Array.isArray(candidate.rules)) {
|
|
249
|
+
return failure("invalid_recipe", "Config rules must be an array");
|
|
250
|
+
}
|
|
251
|
+
if (candidate.concurrency !== void 0 && (typeof candidate.concurrency !== "number" || !Number.isInteger(candidate.concurrency) || candidate.concurrency < 1)) {
|
|
252
|
+
return failure("invalid_recipe", "Config concurrency must be a positive integer");
|
|
253
|
+
}
|
|
254
|
+
for (const [index, rule] of candidate.rules.entries()) {
|
|
255
|
+
if (!rule || typeof rule !== "object" || Array.isArray(rule)) {
|
|
256
|
+
return failure("invalid_recipe", `Config rule ${index} must be an object`);
|
|
257
|
+
}
|
|
258
|
+
const entry = rule;
|
|
259
|
+
const ruleUnknownField = unknownFieldIssue(entry, ["include", "exclude", "recipe", "path"], `config rule ${index}`);
|
|
260
|
+
if (ruleUnknownField) return failure("invalid_recipe", ruleUnknownField);
|
|
261
|
+
if (!isStringArray(entry.include)) {
|
|
262
|
+
return failure("invalid_recipe", `Config rule ${index} include must be an array of glob patterns`);
|
|
263
|
+
}
|
|
264
|
+
if (entry.exclude !== void 0 && !isStringArray(entry.exclude)) {
|
|
265
|
+
return failure("invalid_recipe", `Config rule ${index} exclude must be an array of glob patterns`);
|
|
266
|
+
}
|
|
267
|
+
const invalidGlob = [...entry.include, ...entry.exclude ?? []].find(
|
|
268
|
+
(pattern) => !isProjectRelativeGlob(pattern)
|
|
269
|
+
);
|
|
270
|
+
if (invalidGlob) {
|
|
271
|
+
return failure("invalid_recipe", `Config rule ${index} glob must be project-relative: ${invalidGlob}`);
|
|
272
|
+
}
|
|
273
|
+
if (entry.path !== void 0 && (typeof entry.path !== "string" || !entry.path)) {
|
|
274
|
+
return failure("invalid_recipe", `Config rule ${index} path must be a non-empty string`);
|
|
275
|
+
}
|
|
276
|
+
if (candidate.outputDirectory !== void 0 && entry.path !== void 0) {
|
|
277
|
+
return failure(
|
|
278
|
+
"invalid_recipe",
|
|
279
|
+
`Config rule ${index} path cannot be combined with outputDirectory; authoritative sibling output routing determines the output path`
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
const recipe = validateRecipe(entry.recipe);
|
|
283
|
+
if (!recipe.ok) return recipe;
|
|
284
|
+
}
|
|
285
|
+
return success({ ...config, output: candidate.output ?? DEFAULT_OUTPUT });
|
|
286
|
+
}
|
|
287
|
+
function normalizeProjectPath(path) {
|
|
288
|
+
if (typeof path !== "string") return void 0;
|
|
289
|
+
if (!isProjectRelativePath(path, false)) return void 0;
|
|
290
|
+
const parts = [];
|
|
291
|
+
for (const part of path.split("/")) {
|
|
292
|
+
if (!part || part === ".") continue;
|
|
293
|
+
if (part === "..") {
|
|
294
|
+
if (!parts.length) return void 0;
|
|
295
|
+
parts.pop();
|
|
296
|
+
} else {
|
|
297
|
+
parts.push(part);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return parts.join("/") || void 0;
|
|
301
|
+
}
|
|
302
|
+
function isStringArray(value) {
|
|
303
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string" && item.length > 0);
|
|
304
|
+
}
|
|
305
|
+
function isSafePathSegment(value) {
|
|
306
|
+
return typeof value === "string" && value.length > 0 && value !== "." && value !== ".." && !value.includes("/") && !value.includes("\\") && !value.includes("\0") && !value.includes(":");
|
|
307
|
+
}
|
|
308
|
+
function isProjectRelativeGlob(pattern) {
|
|
309
|
+
const path = pattern.startsWith("!") && !pattern.startsWith("!(") ? pattern.slice(1) : pattern;
|
|
310
|
+
return path.length > 0 && !path.includes("\0") && !path.startsWith("/") && !/^[A-Za-z]:\//.test(path) && !path.split("/").includes("..");
|
|
311
|
+
}
|
|
312
|
+
function isProjectRelativePath(path, allowCurrentDirectory) {
|
|
313
|
+
if (!path || path.includes("\\") || path.includes("\0") || path.startsWith("/") || /^[A-Za-z]:\//.test(path)) {
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
const normalized = normalizeSegments(path);
|
|
317
|
+
return normalized !== void 0 && (allowCurrentDirectory || normalized !== "");
|
|
318
|
+
}
|
|
319
|
+
function normalizeSegments(path) {
|
|
320
|
+
const parts = [];
|
|
321
|
+
for (const part of path.split("/")) {
|
|
322
|
+
if (!part || part === ".") continue;
|
|
323
|
+
if (part === "..") {
|
|
324
|
+
if (!parts.length) return void 0;
|
|
325
|
+
parts.pop();
|
|
326
|
+
} else {
|
|
327
|
+
parts.push(part);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return parts.join("/");
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// src/config/planner.ts
|
|
334
|
+
var GLOB_OPTIONS = {
|
|
335
|
+
dot: false,
|
|
336
|
+
nobrace: false,
|
|
337
|
+
nocase: false,
|
|
338
|
+
noextglob: false,
|
|
339
|
+
noglobstar: false,
|
|
340
|
+
posix: true
|
|
341
|
+
};
|
|
342
|
+
function createConfigAssetPlanner(config) {
|
|
343
|
+
let prepared;
|
|
344
|
+
const route = (input) => {
|
|
345
|
+
prepared ??= prepareRules(config);
|
|
346
|
+
if (!prepared.ok) return prepared;
|
|
347
|
+
const normalizedInput = normalizeProjectPath(input);
|
|
348
|
+
if (!normalizedInput) {
|
|
349
|
+
return failure("invalid_recipe", `Asset input must be a project-relative POSIX path: ${input}`);
|
|
350
|
+
}
|
|
351
|
+
if (prepared.data.outputDirectory && isGeneratedOutput(normalizedInput, prepared.data.outputDirectory)) {
|
|
352
|
+
return success({ kind: "output", input: normalizedInput });
|
|
353
|
+
}
|
|
354
|
+
const planned = /* @__PURE__ */ new Map();
|
|
355
|
+
for (const { rule, matches } of prepared.data.rules) {
|
|
356
|
+
if (!matches(normalizedInput)) continue;
|
|
357
|
+
const output = resolveOutput(rule, normalizedInput, prepared.data.outputDirectory);
|
|
358
|
+
if (!output.ok) return output;
|
|
359
|
+
planned.set(output.data, { input: normalizedInput, output: output.data, rule });
|
|
360
|
+
}
|
|
361
|
+
const outputs = [...planned.values()];
|
|
362
|
+
return outputs.length ? success({ kind: "source", input: normalizedInput, outputs }) : success({ kind: "unmatched", input: normalizedInput });
|
|
363
|
+
};
|
|
364
|
+
const resolve = (input) => {
|
|
365
|
+
const routed = route(input);
|
|
366
|
+
if (!routed.ok) return routed;
|
|
367
|
+
return success(routed.data.kind === "source" ? routed.data.outputs : []);
|
|
368
|
+
};
|
|
369
|
+
return { route, resolve };
|
|
370
|
+
}
|
|
371
|
+
function prepareRules(config) {
|
|
372
|
+
const validated = validateConfig(config);
|
|
373
|
+
if (!validated.ok) return validated;
|
|
374
|
+
try {
|
|
375
|
+
return success({
|
|
376
|
+
outputDirectory: validated.data.outputDirectory,
|
|
377
|
+
rules: validated.data.rules.map((rule) => ({ rule, matches: compileRuleMatcher(rule) }))
|
|
378
|
+
});
|
|
379
|
+
} catch (cause) {
|
|
380
|
+
return failure("invalid_recipe", "Config contains an invalid glob pattern", cause);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
function compileRuleMatcher(rule) {
|
|
384
|
+
const includePatterns = [];
|
|
385
|
+
const ignorePatterns = (rule.exclude ?? []).filter((pattern) => !pattern.startsWith("!") || pattern.startsWith("!("));
|
|
386
|
+
for (const pattern of rule.include) {
|
|
387
|
+
if (pattern.startsWith("!") && !pattern.startsWith("!(")) {
|
|
388
|
+
if (!pattern.startsWith("!!") || pattern.startsWith("!!(")) ignorePatterns.push(pattern.slice(1));
|
|
389
|
+
} else {
|
|
390
|
+
includePatterns.push(pattern);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
const include = picomatch(includePatterns.map(expandDirectoryPattern), GLOB_OPTIONS);
|
|
394
|
+
const ignore = picomatch(ignorePatterns.map(expandDirectoryPattern), GLOB_OPTIONS);
|
|
395
|
+
return (input) => include(input) && !ignore(input);
|
|
396
|
+
}
|
|
397
|
+
function expandDirectoryPattern(pattern) {
|
|
398
|
+
const normalized = pattern.split("/").filter((segment) => segment && segment !== ".").join("/") || "**";
|
|
399
|
+
const withoutTrailingSlash = normalized.endsWith("/") ? normalized.slice(0, -1) : normalized;
|
|
400
|
+
return withoutTrailingSlash.endsWith("*") ? withoutTrailingSlash : `${withoutTrailingSlash}/**`;
|
|
401
|
+
}
|
|
402
|
+
function resolveOutput(rule, input, outputDirectory) {
|
|
403
|
+
const slash = input.lastIndexOf("/");
|
|
404
|
+
const directory = slash < 0 ? "" : input.slice(0, slash);
|
|
405
|
+
const base = slash < 0 ? input : input.slice(slash + 1);
|
|
406
|
+
const dot = base.lastIndexOf(".");
|
|
407
|
+
const name = dot <= 0 ? base : base.slice(0, dot);
|
|
408
|
+
const extension = rule.recipe.kind === "glb" ? ".glb" : outputExtension(rule.recipe.encode);
|
|
409
|
+
if (outputDirectory) {
|
|
410
|
+
return success(`${directory ? `${directory}/` : ""}${outputDirectory}/${name}${extension}`);
|
|
411
|
+
}
|
|
412
|
+
const template = rule.path ?? "{dir}/{name}.{ext}";
|
|
413
|
+
if (template.includes("\\") || template.startsWith("/") || /^[A-Za-z]:\//.test(template)) {
|
|
414
|
+
return failure("invalid_recipe", `Output path must stay inside the configured output directory: ${template}`);
|
|
415
|
+
}
|
|
416
|
+
const rendered = template.replaceAll("{dir}/", directory ? `${directory}/` : "").replaceAll("{dir}", directory).replaceAll("{name}", name).replaceAll("{ext}", extension.slice(1));
|
|
417
|
+
if (/\{[^{}]+\}/.test(rendered)) {
|
|
418
|
+
return failure("invalid_recipe", `Output path contains an unsupported placeholder: ${template}`);
|
|
419
|
+
}
|
|
420
|
+
const output = normalizeProjectPath(rendered);
|
|
421
|
+
return output ? success(output) : failure("invalid_recipe", `Output path must stay inside the configured output directory: ${template}`);
|
|
422
|
+
}
|
|
423
|
+
function isGeneratedOutput(input, outputDirectory) {
|
|
424
|
+
return input.split("/").slice(0, -1).includes(outputDirectory);
|
|
425
|
+
}
|
|
22
426
|
|
|
23
427
|
// src/config/index.ts
|
|
24
|
-
|
|
428
|
+
function createChopper(config) {
|
|
429
|
+
const { output = DEFAULT_OUTPUT, ...rest } = config;
|
|
430
|
+
const defined = { output, ...rest };
|
|
431
|
+
const planner = createConfigAssetPlanner(defined);
|
|
432
|
+
Object.defineProperties(defined, {
|
|
433
|
+
route: { enumerable: false, value: planner.route },
|
|
434
|
+
resolve: { enumerable: false, value: planner.resolve }
|
|
435
|
+
});
|
|
436
|
+
return defined;
|
|
437
|
+
}
|
|
438
|
+
var defineConfig = createChopper;
|
|
25
439
|
|
|
26
|
-
export { COLOR_TEXTURE_SLOTS, DATA_TEXTURE_SLOTS, DIRECTIONAL_TEXTURE_SLOTS, defineConfig, defineRecipe };
|
|
440
|
+
export { COLOR_TEXTURE_SLOTS, DATA_TEXTURE_SLOTS, DIRECTIONAL_TEXTURE_SLOTS, createChopper, defineConfig, defineRecipe };
|
|
27
441
|
//# sourceMappingURL=index.js.map
|
|
28
442
|
//# sourceMappingURL=index.js.map
|