-
-
Notifications
You must be signed in to change notification settings - Fork 76
feat: Obfuscate variables in plugin #2755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
aleksanderkatan
wants to merge
29
commits into
main
Choose a base branch
from
feat/minify-variables-in-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
2c1d700
Add `minify` option to plugin options
aleksanderkatan e134114
Add minifier to parsers
aleksanderkatan 1b51628
Add some tests
aleksanderkatan 8ad03d1
Make basic examples work
aleksanderkatan 7ac210d
Support struct props
aleksanderkatan dc8e0ca
Support function params
aleksanderkatan 4ab2e80
Add more parameter tests
aleksanderkatan dd2fc0d
Generate minified identifiers properly
aleksanderkatan 6125147
CHange externals set to map, handle externals
aleksanderkatan cde0469
Add more tests
aleksanderkatan 626992f
Fix object expression
aleksanderkatan 29b4116
Add more tests
aleksanderkatan ab8e565
Allow plugins to minify
aleksanderkatan e2e428b
Fix undefined handling
aleksanderkatan 071c869
Add obfuscation stub to unplugin
aleksanderkatan 8e2b3b0
Implement obfuscate
aleksanderkatan 76cebf2
Revert parsers to original state
aleksanderkatan 623e3f1
Make externalNames a map again
aleksanderkatan 0c874fe
Fix types
aleksanderkatan 6c5db9d
Use stringifyNode in obfuscation tests
aleksanderkatan bbca2ae
Add remaining obfuscation tests
aleksanderkatan 0534ae8
Remove unused minifier functionality
aleksanderkatan f57d387
Minify -> obfuscate
aleksanderkatan 837884e
Merge test files
aleksanderkatan 4b5551d
Rename obfuscate to EXPERIMENTAL_obfuscate
aleksanderkatan aa3c7c5
Add options check
aleksanderkatan b8bed32
Add docs
aleksanderkatan c9ad048
nr fix
aleksanderkatan a590a52
Remove outdated test
aleksanderkatan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
apps/typegpu-docs/src/content/docs/advanced/minifying-shaders.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| --- | ||
| title: Minifying & Obfuscating Shaders | ||
| description: How to setup TypeGPU so that resulting shaders will be smaller or obfuscated. | ||
| --- | ||
|
|
||
| :::caution[Experimental] | ||
| This feature is under development and is yet to reach stability. | ||
| ::: | ||
|
|
||
| Regular JS minification & obfuscation does not result in changes to the shader code generated by TypeGPU. | ||
| This is why we provide specific options for transforming shaders. | ||
|
|
||
| ## Plugin obfuscation | ||
|
|
||
| :::note | ||
| To reduce both the shader size and readability, it is advised to disable the plugin auto-naming by setting `{ autoNamingEnabled: false }`. | ||
| This way, only resources given name via `.$name()` will be named in the resulting shader. | ||
| ::: | ||
|
|
||
| `unplugin-typegpu` collects function metadata, which is later used for WGSL code generation. | ||
| With the `{ EXPERIMENTAL_obfuscate: true }` option, all saved identifiers will be obfuscated: | ||
| - In the AST, all parameters and variables will have their names changed to `a`, `b`, `c`, ... | ||
| - Externals (the captured scope used by the function) will also have their respective identifiers changed. | ||
|
|
||
| :::caution | ||
| Enabling this will obfuscate not only the resulting code, but also error messages appearing during resolution. | ||
|
|
||
| It is not advised to minify shaders during development. | ||
| ::: | ||
|
|
||
| ## Runtime minification | ||
|
|
||
| Coming soon. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import babel from '@babel/parser'; | ||
| import type { Node } from '@babel/types'; | ||
| import * as acorn from 'acorn'; | ||
|
|
||
| export const parseRollup = (code: string) => acorn.parse(code, { ecmaVersion: 'latest' }); | ||
| export const parseBabel = (code: string) => | ||
| babel.parse(code, { sourceType: 'module', plugins: ['typescript'] }).program.body[0] as Node; | ||
|
|
||
| export function dualTest(test: (p: (code: string) => Node | acorn.AnyNode) => void) { | ||
| return () => { | ||
| test(parseBabel); | ||
| test(parseRollup); | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Less work swapping it for a map here than in unplugin, and it needs to become a map eventually