Skip to content

feat(unplugin-typegpu): Support obfuscation of already built sources - #3291

Open
cieplypolar wants to merge 6 commits into
impr/unplugin-ast-ext-parserfrom
feat/re-obf
Open

cieplypolar wants to merge 6 commits into
impr/unplugin-ast-ext-parserfrom
feat/re-obf

Conversation

@cieplypolar

Copy link
Copy Markdown
Collaborator

No description provided.

Copilot AI balanced review requested due to automatic review settings October 29, 2026 13:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@cieplypolar
cieplypolar changed the base branch from main to impr/unplugin-ast-ext-parser October 29, 2026 13:30
@cieplypolar
cieplypolar added this pull request to stack #3292 October 29, 2026 13:30
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

pkg.pr.new

packages
Ready to be installed by your favorite package manager ⬇️

https://pkg.pr.new/software-mansion/TypeGPU/tgpu-gen@c860aaafa1db39544989545ed3935fb94c9cac8c
https://pkg.pr.new/software-mansion/TypeGPU/typegpu@c860aaafa1db39544989545ed3935fb94c9cac8c
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/cli@c860aaafa1db39544989545ed3935fb94c9cac8c
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/color@c860aaafa1db39544989545ed3935fb94c9cac8c
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/gl@c860aaafa1db39544989545ed3935fb94c9cac8c
https://pkg.pr.new/software-mansion/TypeGPU/unplugin-typegpu@c860aaafa1db39544989545ed3935fb94c9cac8c

benchmark
view benchmark

commit
view commit

@github-actions

Copy link
Copy Markdown

Bundle size comparison (import * as ... in PR vs import * as ... in target):

🟢 Decreased ➖ Unchanged 🔴 Increased ❔ Unknown
0 355 0 0

No notable changes.

If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Resolution Time Benchmark

---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Random Branching (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [1.13, 2.22, 4.97, 7.22, 9.36, 13.14, 29.49, 29.08]
  line [1.06, 2.17, 4.76, 7.26, 8.59, 13.34, 27.08, 28.63]
  line [1.06, 2.26, 4.65, 7.99, 8.48, 14.17, 27.69, 28.80]
Loading
---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Linear Recursion (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.34, 0.61, 0.77, 0.92, 1.28, 1.32, 1.58, 1.74]
  line [0.32, 0.52, 0.73, 0.87, 1.20, 1.25, 1.56, 1.71]
  line [0.33, 0.56, 0.72, 0.90, 1.23, 1.31, 1.55, 1.71]
Loading
---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Full Tree (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.90, 2.49, 4.67, 8.15, 15.06, 29.63, 62.99, 128.71]
  line [0.96, 2.20, 4.54, 8.40, 15.22, 28.29, 60.43, 122.03]
  line [0.93, 2.53, 3.85, 8.08, 14.63, 28.42, 61.77, 125.87]
Loading

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — one small documentation gap.

Reviewed changes

Reviewed the complete diff of PR #3291 against impr/unplugin-ast-ext-parser (4 commits, 8 files). The PR lets unplugin-typegpu re-obfuscate metadata embedded by an earlier transform pass.

  • Re-obfuscation of built functions — functionOnExit now runs reobfuscateEmbedded when embedded metadata is present and unstable_obfuscate is on, rewriting the metadata ast and the external getter keys through the new replaceWithObfuscated method (Babel and unplugin implementations).
  • obfuscateAst extraction — obfuscate now delegates to obfuscateAst(fn, externalKeys), returning the obfuscated AST plus a key map so the same routine can be reused on already-built metadata; sourceMap is preserved.
  • Early-pruning regex — earlyPruneRegex becomes getEarlyPruneRegex(obfuscate), adding __TYPEGPU_META__ as a trigger when obfuscation is enabled so metadata-only files are no longer skipped.
  • Tests — re-obfuscation of built shellless/shelled functions, idempotency, prune behavior, and the once-per-file unsupported-version warning. All 284 package tests pass; typecheck passes.

ℹ️ earlyPruning docs no longer list every trigger string

The Options.earlyPruning JSDoc still says files are skipped unless they contain "typegpu", "tgpu" or "use gpu". With this change, when unstable_obfuscate is on the trigger set also includes __TYPEGPU_META__, so an already-built file with no directive and no typegpu reference is intentionally no longer pruned. The minifying-shaders guide is likewise silent on the newly re-obfuscated already-built inputs.

Technical details
# Update early-pruning option docs to mention `__TYPEGPU_META__`

## Affected sites
- `packages/unplugin-typegpu/src/core/common.ts:39-46` — `Options.earlyPruning` JSDoc enumerates the trigger strings but predates the `__TYPEGPU_META__` addition in `getEarlyPruneRegex` (`common.ts:168-174`).
- `apps/typegpu-docs/src/content/docs/advanced/minifying-shaders.mdx` — describes `unstable_obfuscate` without noting that already-built sources are now re-obfuscated.

## Required outcome
- Document that when `unstable_obfuscate` is enabled, files containing `__TYPEGPU_META__` are also transformed (not pruned).

## Open questions for the human
- Whether the user-facing guide should call out the re-obfuscation of already-built sources, or whether the flag remains intentionally undocumented until it stabilizes.

Pullfrog  | Fix it ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

Reviewed the delta since the prior pullfrog review (5c1faa47..386095e6, a docs-only commit) on top of the full PR against impr/unplugin-ast-ext-parser.

  • Re-obfuscation documented — minifying-shaders.mdx gains an :::caution[Experimental] note explaining that already-built sources can be re-processed with { unstable_obfuscate: true, autoNamingEnabled: false }, and that names from the original build are preserved.
  • Early-pruning trigger documented — the earlyPruning JSDoc in core/common.ts and its mirror in tooling/unplugin-typegpu.mdx now both mention that __TYPEGPU_META__ counts as a trigger when unstable_obfuscate is enabled.
  • Trailing newline — minifying-shaders.mdx now ends with a newline.

The documentation gap raised in the prior review is fully addressed. The recommended option pair matches checkOpts (which rejects unstable_obfuscate + autoNamingEnabled together), and the claim that original-build names survive re-obfuscation is consistent with reobfuscateEmbedded, which only rewrites the embedded ast and the externals' keys, leaving metadata.name untouched.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@cieplypolar
cieplypolar force-pushed the feat/re-obf branch 3 times, most recently from e605c53 to 2414b52 Compare October 30, 2026 14:46

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

Reviewed the delta since the prior pullfrog review (e605c533..2414b524). The range also picks up a base-branch advance (c17c69a8 → 99777d94, "lowering arrow functions" on impr/unplugin-ast-ext-parser); that commit is not part of this PR's diff and was not reviewed here. One authored commit landed on the branch.

  • Bun warning context — bun.ts now passes { warn: (message) => console.warn(message) } into the manual rawPlugin.transform.handler.apply call in place of {}. Previously state.warn resolved to this.warn on an empty stub, so the new unsupported-metadata warning thrown during re-obfuscation crashed with this.warn is not a function under Bun.
  • Regression test — test/bun.test.ts downgrades the embedded metadata (v: 2 → v: 1), runs the plugin with unstable_obfuscate, and asserts exactly one warning plus preserved file contents.

Verified locally: vitest run --project=!browser packages/unplugin-typegpu/test/bun.test.ts passes (4 tests) and pnpm --filter unplugin-typegpu test:types is clean. No open pullfrog threads remain on this PR.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants