@joycostudio/chopper 0.8.1 → 0.9.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 +66 -3
- package/codecs/manifest.json +1 -1
- package/dist/browser/{chunk-6PIXH7DA.js → chunk-STDFGSQX.js} +3 -3
- package/dist/browser/{chunk-6PIXH7DA.js.map → chunk-STDFGSQX.js.map} +1 -1
- package/dist/browser/{in-process-FAMTTLSK.js → in-process-MJMPQZDQ.js} +3 -3
- package/dist/browser/{in-process-FAMTTLSK.js.map → in-process-MJMPQZDQ.js.map} +1 -1
- package/dist/browser/index.js +2 -2
- package/dist/browser/worker.js +1 -1
- package/dist/browser/worker.js.map +1 -1
- package/dist/cli/index.js +27 -17
- package/dist/cli/index.js.map +1 -1
- package/dist/config/index.d.ts +28 -4
- package/dist/config/index.js +166 -3
- 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 +27 -3
- package/dist/node/index.js +27 -17
- package/dist/node/index.js.map +1 -1
- package/package.json +1 -1
package/dist/config/index.d.ts
CHANGED
|
@@ -301,6 +301,29 @@ interface ChopperRule {
|
|
|
301
301
|
exclude?: readonly string[];
|
|
302
302
|
/** Optimization recipe applied to every asset matched by this rule. */
|
|
303
303
|
recipe: AssetRecipe;
|
|
304
|
+
/**
|
|
305
|
+
* Builds the output file name, without extension, for each matched source.
|
|
306
|
+
* Chopper still owns the directory (the reserved {@link ChopperConfig.outputDirectory}
|
|
307
|
+
* sibling) and the extension (derived from the recipe codec), so the builder
|
|
308
|
+
* only names the file. The result must be one safe path segment.
|
|
309
|
+
*
|
|
310
|
+
* Use it to let several rules produce distinct outputs from one source, such
|
|
311
|
+
* as a smaller mobile encoding next to the desktop one:
|
|
312
|
+
* `filename: ({ name }) => name + '_mobile'`.
|
|
313
|
+
*
|
|
314
|
+
* @defaultValue `({ name }) => name`
|
|
315
|
+
*/
|
|
316
|
+
filename?: (source: ChopperFilenameSource) => string;
|
|
317
|
+
}
|
|
318
|
+
interface ChopperFilenameSource {
|
|
319
|
+
/** Project-relative POSIX path of the matched source, such as `assets/hero.png`. */
|
|
320
|
+
input: string;
|
|
321
|
+
/** Source directory, such as `assets`, or `''` at the project root. */
|
|
322
|
+
directory: string;
|
|
323
|
+
/** Source file name without its extension, such as `hero`. */
|
|
324
|
+
name: string;
|
|
325
|
+
/** Recipe of the rule being planned. */
|
|
326
|
+
recipe: AssetRecipe;
|
|
304
327
|
}
|
|
305
328
|
interface ChopperConfig {
|
|
306
329
|
/**
|
|
@@ -324,9 +347,10 @@ interface ChopperConfig {
|
|
|
324
347
|
* Given `assets/wall.png` and `outputDirectory: 'compressed'`, a WebP
|
|
325
348
|
* recipe resolves to `assets/compressed/wall.webp`. Inputs already inside
|
|
326
349
|
* this directory are classified as generated outputs and are never
|
|
327
|
-
* processed again. Rules choose which assets and recipes apply
|
|
328
|
-
*
|
|
329
|
-
*
|
|
350
|
+
* processed again. Rules choose which assets and recipes apply, and may
|
|
351
|
+
* name the output file with {@link ChopperRule.filename}; this config-level
|
|
352
|
+
* property is the sole authority for which directory their logical outputs
|
|
353
|
+
* go to. This logical segment is independent of
|
|
330
354
|
* {@link buildDirectory}, the physical filesystem root used by Node and the
|
|
331
355
|
* CLI.
|
|
332
356
|
*
|
|
@@ -388,4 +412,4 @@ declare function createChopper<const T extends ChopperConfig>(config: T & NoUnkn
|
|
|
388
412
|
/** @deprecated Use {@link createChopper} instead. */
|
|
389
413
|
declare const defineConfig: typeof createChopper;
|
|
390
414
|
|
|
391
|
-
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 };
|
|
415
|
+
export { COLOR_TEXTURE_SLOTS, type ChopperConfig, type ChopperFilenameSource, 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
|
@@ -282,7 +282,11 @@ function validateConfig(config) {
|
|
|
282
282
|
return failure("invalid_recipe", `Config rule ${index} must be an object`);
|
|
283
283
|
}
|
|
284
284
|
const entry = rule;
|
|
285
|
-
const ruleUnknownField = unknownFieldIssue(
|
|
285
|
+
const ruleUnknownField = unknownFieldIssue(
|
|
286
|
+
entry,
|
|
287
|
+
["include", "exclude", "recipe", "filename"],
|
|
288
|
+
`config rule ${index}`
|
|
289
|
+
);
|
|
286
290
|
if (ruleUnknownField) return failure("invalid_recipe", ruleUnknownField);
|
|
287
291
|
if (!isStringArray(entry.include)) {
|
|
288
292
|
return failure("invalid_recipe", `Config rule ${index} include must be an array of glob patterns`);
|
|
@@ -296,6 +300,9 @@ function validateConfig(config) {
|
|
|
296
300
|
if (invalidGlob) {
|
|
297
301
|
return failure("invalid_recipe", `Config rule ${index} glob must be project-relative: ${invalidGlob}`);
|
|
298
302
|
}
|
|
303
|
+
if (entry.filename !== void 0 && typeof entry.filename !== "function") {
|
|
304
|
+
return failure("invalid_recipe", `Config rule ${index} filename must be a function`);
|
|
305
|
+
}
|
|
299
306
|
const recipe = validateRecipe(entry.recipe);
|
|
300
307
|
if (!recipe.ok) return recipe;
|
|
301
308
|
}
|
|
@@ -376,7 +383,8 @@ function createConfigAssetPlanner(config) {
|
|
|
376
383
|
for (const { rule, matches } of prepared.data.rules) {
|
|
377
384
|
if (!matches(normalizedInput)) continue;
|
|
378
385
|
const output = resolveOutput(rule, normalizedInput, prepared.data.outputDirectory);
|
|
379
|
-
|
|
386
|
+
if (!output.ok) return output;
|
|
387
|
+
planned.set(output.data, { input: normalizedInput, output: output.data, rule });
|
|
380
388
|
}
|
|
381
389
|
const outputs = [...planned.values()];
|
|
382
390
|
return outputs.length ? success({ kind: "source", input: normalizedInput, outputs }) : success({ kind: "unmatched", input: normalizedInput });
|
|
@@ -388,6 +396,133 @@ function createConfigAssetPlanner(config) {
|
|
|
388
396
|
};
|
|
389
397
|
return { route, resolve };
|
|
390
398
|
}
|
|
399
|
+
var EXPANSION_LIMIT = 64;
|
|
400
|
+
var FILLERS = ["x", "q", "chopper"];
|
|
401
|
+
function findRuleOverrides(config) {
|
|
402
|
+
const prepared = prepareRules(config);
|
|
403
|
+
if (!prepared.ok) return [];
|
|
404
|
+
const { rules, outputDirectory } = prepared.data;
|
|
405
|
+
const patterns = rules.map(({ rule }) => parsedIncludes(rule));
|
|
406
|
+
const overrides = [];
|
|
407
|
+
for (let later = 1; later < rules.length; later++) {
|
|
408
|
+
for (let earlier = 0; earlier < later; earlier++) {
|
|
409
|
+
const witness = FILLERS.flatMap(
|
|
410
|
+
(filler) => patterns[earlier].flatMap((a) => patterns[later].map((b) => intersectPaths(a, b, filler)))
|
|
411
|
+
).sort((a = "", b = "") => a.split("/").length - b.split("/").length || a.length - b.length).map((input) => {
|
|
412
|
+
if (!input || isGeneratedOutput(input, outputDirectory)) return void 0;
|
|
413
|
+
if (!rules[earlier].matches(input) || !rules[later].matches(input)) return void 0;
|
|
414
|
+
const first = resolveOutput(rules[earlier].rule, input, outputDirectory);
|
|
415
|
+
const second = resolveOutput(rules[later].rule, input, outputDirectory);
|
|
416
|
+
return first.ok && second.ok && first.data === second.data ? { input, output: first.data } : void 0;
|
|
417
|
+
}).find(Boolean);
|
|
418
|
+
if (witness) overrides.push({ earlier, later, ...witness });
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return overrides;
|
|
422
|
+
}
|
|
423
|
+
var STAR_SEGMENT = [{ star: true }];
|
|
424
|
+
function parsedIncludes(rule) {
|
|
425
|
+
return rule.include.filter((pattern) => !pattern.startsWith("!") || pattern.startsWith("!(")).flatMap((pattern) => expandBraces(expandDirectoryPattern(pattern))).map((pattern) => pattern.split("/").map((segment) => segment === "**" ? "globstar" : parseSegment(segment)));
|
|
426
|
+
}
|
|
427
|
+
function parseSegment(segment) {
|
|
428
|
+
const literal = (char) => ({ star: false, accepts: (c) => c === char, hints: char });
|
|
429
|
+
const tokens = [];
|
|
430
|
+
for (let index = 0; index < segment.length; index++) {
|
|
431
|
+
const char = segment[index];
|
|
432
|
+
const end = char === "[" ? segment.indexOf("]", index + 2) : -1;
|
|
433
|
+
if (char === "*") {
|
|
434
|
+
if (!tokens.at(-1)?.star) tokens.push({ star: true });
|
|
435
|
+
} else if (char === "?") {
|
|
436
|
+
tokens.push({ star: false, accepts: () => true, hints: "" });
|
|
437
|
+
} else if (end > 0) {
|
|
438
|
+
const body = segment.slice(index + 1, end);
|
|
439
|
+
try {
|
|
440
|
+
const set = new RegExp(`^[${body.replace(/^!/, "^")}]$`);
|
|
441
|
+
tokens.push({ star: false, accepts: (c) => set.test(c), hints: body });
|
|
442
|
+
index = end;
|
|
443
|
+
} catch {
|
|
444
|
+
tokens.push(literal(char));
|
|
445
|
+
}
|
|
446
|
+
} else if (char === "\\" && index + 1 < segment.length) {
|
|
447
|
+
tokens.push(literal(segment[++index]));
|
|
448
|
+
} else {
|
|
449
|
+
tokens.push(literal(char));
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return tokens;
|
|
453
|
+
}
|
|
454
|
+
function intersectPaths(a, b, filler) {
|
|
455
|
+
const memo = /* @__PURE__ */ new Map();
|
|
456
|
+
const cons = (head, rest) => head && rest ? [head, ...rest] : null;
|
|
457
|
+
const visit = (i, j) => {
|
|
458
|
+
const key = i * (b.length + 1) + j;
|
|
459
|
+
if (memo.has(key)) return memo.get(key);
|
|
460
|
+
const x = a[i];
|
|
461
|
+
const y = b[j];
|
|
462
|
+
let result = null;
|
|
463
|
+
if (!x && !y) result = [];
|
|
464
|
+
else if (x === "globstar" && y === "globstar") {
|
|
465
|
+
result = visit(i + 1, j) ?? visit(i, j + 1);
|
|
466
|
+
} else if (x === "globstar") {
|
|
467
|
+
result = visit(i + 1, j) ?? (y && y !== "globstar" ? cons(intersectSegments(y, STAR_SEGMENT, filler), visit(i, j + 1)) : null);
|
|
468
|
+
} else if (y === "globstar") {
|
|
469
|
+
result = visit(i, j + 1) ?? (x ? cons(intersectSegments(x, STAR_SEGMENT, filler), visit(i + 1, j)) : null);
|
|
470
|
+
} else if (x && y) {
|
|
471
|
+
result = cons(intersectSegments(x, y, filler), visit(i + 1, j + 1));
|
|
472
|
+
}
|
|
473
|
+
memo.set(key, result);
|
|
474
|
+
return result;
|
|
475
|
+
};
|
|
476
|
+
const path = visit(0, 0);
|
|
477
|
+
return path ? path.join("/") || filler : void 0;
|
|
478
|
+
}
|
|
479
|
+
function intersectSegments(a, b, filler) {
|
|
480
|
+
const memo = /* @__PURE__ */ new Map();
|
|
481
|
+
const prefix = (char, rest) => char !== void 0 && rest !== null ? char + rest : null;
|
|
482
|
+
const visit = (i, j) => {
|
|
483
|
+
const key = i * (b.length + 1) + j;
|
|
484
|
+
if (memo.has(key)) return memo.get(key);
|
|
485
|
+
const x = a[i];
|
|
486
|
+
const y = b[j];
|
|
487
|
+
let result = null;
|
|
488
|
+
if (!x && !y) result = "";
|
|
489
|
+
else if (x?.star && y?.star) {
|
|
490
|
+
result = prefix(filler, visit(i + 1, j)) ?? prefix(filler, visit(i, j + 1)) ?? visit(i + 1, j) ?? visit(i, j + 1);
|
|
491
|
+
} else if (x?.star) result = visit(i + 1, j) ?? (y && !y.star ? prefix(pickChar(filler, y), visit(i, j + 1)) : null);
|
|
492
|
+
else if (y?.star) result = visit(i, j + 1) ?? (x && !x.star ? prefix(pickChar(filler, x), visit(i + 1, j)) : null);
|
|
493
|
+
else if (x && y && !x.star && !y.star) result = prefix(pickChar(filler, x, y), visit(i + 1, j + 1));
|
|
494
|
+
memo.set(key, result);
|
|
495
|
+
return result;
|
|
496
|
+
};
|
|
497
|
+
return visit(0, 0) || void 0;
|
|
498
|
+
}
|
|
499
|
+
function pickChar(filler, ...tokens) {
|
|
500
|
+
const candidates = `${tokens.map(({ hints }) => hints).join("")}${filler}xa0_-`;
|
|
501
|
+
return [...candidates].find((char) => char !== "/" && tokens.every(({ accepts }) => accepts(char)));
|
|
502
|
+
}
|
|
503
|
+
function expandBraces(pattern) {
|
|
504
|
+
const open = pattern.indexOf("{");
|
|
505
|
+
if (open < 0) return [pattern];
|
|
506
|
+
let depth = 0;
|
|
507
|
+
const commas = [];
|
|
508
|
+
for (let index = open; index < pattern.length; index++) {
|
|
509
|
+
const char = pattern[index];
|
|
510
|
+
if (char === "{") depth++;
|
|
511
|
+
else if (char === "," && depth === 1) commas.push(index);
|
|
512
|
+
else if (char === "}" && --depth === 0) {
|
|
513
|
+
if (!commas.length) return [pattern];
|
|
514
|
+
const bounds = [open, ...commas, index];
|
|
515
|
+
const head = pattern.slice(0, open);
|
|
516
|
+
const tail = expandBraces(pattern.slice(index + 1));
|
|
517
|
+
return bounds.slice(1).flatMap(
|
|
518
|
+
(end, alternative) => expandBraces(pattern.slice(bounds[alternative] + 1, end)).flatMap(
|
|
519
|
+
(middle) => tail.map((rest) => `${head}${middle}${rest}`)
|
|
520
|
+
)
|
|
521
|
+
).slice(0, EXPANSION_LIMIT);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return [pattern];
|
|
525
|
+
}
|
|
391
526
|
function prepareRules(config) {
|
|
392
527
|
const validated = validateConfig(config);
|
|
393
528
|
if (!validated.ok) return validated;
|
|
@@ -426,7 +561,21 @@ function resolveOutput(rule, input, outputDirectory) {
|
|
|
426
561
|
const dot = base.lastIndexOf(".");
|
|
427
562
|
const name = dot <= 0 ? base : base.slice(0, dot);
|
|
428
563
|
const extension = rule.recipe.kind === "glb" ? ".glb" : outputExtension(rule.recipe.encode);
|
|
429
|
-
|
|
564
|
+
let filename = name;
|
|
565
|
+
if (rule.filename) {
|
|
566
|
+
try {
|
|
567
|
+
filename = rule.filename({ input, directory, name, recipe: rule.recipe });
|
|
568
|
+
} catch (cause) {
|
|
569
|
+
return failure("invalid_recipe", `Config rule filename threw for ${input}`, cause);
|
|
570
|
+
}
|
|
571
|
+
if (!isSafePathSegment(filename)) {
|
|
572
|
+
return failure(
|
|
573
|
+
"invalid_recipe",
|
|
574
|
+
`Config rule filename must return exactly one safe path segment for ${input}: ${String(filename)}`
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return success(`${directory ? `${directory}/` : ""}${outputDirectory}/${filename}${extension}`);
|
|
430
579
|
}
|
|
431
580
|
function isGeneratedOutput(input, outputDirectory) {
|
|
432
581
|
return input.split("/").slice(0, -1).includes(outputDirectory);
|
|
@@ -437,12 +586,26 @@ function createChopper(config) {
|
|
|
437
586
|
const { buildDirectory = DEFAULT_BUILD_DIRECTORY, outputDirectory = DEFAULT_OUTPUT_DIRECTORY, ...rest } = config;
|
|
438
587
|
const defined = { buildDirectory, outputDirectory, ...rest };
|
|
439
588
|
const planner = createConfigAssetPlanner(defined);
|
|
589
|
+
if (isDevelopment()) {
|
|
590
|
+
for (const { earlier, later, input, output } of findRuleOverrides(defined)) {
|
|
591
|
+
console.warn(
|
|
592
|
+
`[chopper] Rule ${later} overrides rule ${earlier} wherever both match, e.g. ${input} \u2192 ${output}. The later rule wins.`
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
440
596
|
Object.defineProperties(defined, {
|
|
441
597
|
route: { enumerable: false, value: planner.route },
|
|
442
598
|
resolve: { enumerable: false, value: planner.resolve }
|
|
443
599
|
});
|
|
444
600
|
return defined;
|
|
445
601
|
}
|
|
602
|
+
function isDevelopment() {
|
|
603
|
+
try {
|
|
604
|
+
return process.env.NODE_ENV !== "production";
|
|
605
|
+
} catch {
|
|
606
|
+
return false;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
446
609
|
var defineConfig = createChopper;
|
|
447
610
|
|
|
448
611
|
export { COLOR_TEXTURE_SLOTS, DATA_TEXTURE_SLOTS, DIRECTIONAL_TEXTURE_SLOTS, createChopper, defineConfig, defineRecipe };
|