Documentation
¶
Index ¶
- Constants
- func Build(path string, useGarble bool, garbleCfg GarbleConfig, useTinygo bool, ...) ([]byte, error)
- func FindConfig(startDir string) (string, error)
- func Minify(b []byte) ([]byte, error)
- func Obfuscate(js []byte, args string) ([]byte, error)
- func ObfuscateWithOptions(js []byte, options map[string]any) ([]byte, error)
- func Pack(wasm []byte) ([]byte, error)
- func ParseObfuscateOptions(args string) (map[string]any, error)
- func WasmOpt(path string, flags []string) error
- func Wrap(name string, wasm []byte) ([]byte, error)
- func WrapCJS(packed []byte) ([]byte, error)
- func WrapESM(packed []byte) ([]byte, error)
- func WrapHTML(packed []byte) ([]byte, error)
- func WrapIIFE(packed []byte) ([]byte, error)
- func WriteDefaultConfig(path string) error
- type Config
- type GarbleConfig
- type ObfuscateConfig
- type TinygoConfig
- type WasmOptConfig
Constants ¶
const ConfigFileName = "wasmpack.yml"
ConfigFileName is the name of the wasmpack configuration file.
Variables ¶
This section is empty.
Functions ¶
func Build ¶
func Build(path string, useGarble bool, garbleCfg GarbleConfig, useTinygo bool, tinygoCfg TinygoConfig, useWasmOpt bool, wasmOptCfg WasmOptConfig) ([]byte, error)
Build compiles the Go package at path into a WebAssembly binary and returns the raw bytes. The binary is written to a temp file, optionally optimised with wasm-opt, read back, and then removed.
When useGarble is true the build is performed via garble using garbleCfg. When useTinygo is true the build is performed via tinygo using tinygoCfg. useGarble and useTinygo are mutually exclusive; useGarble takes precedence. When useWasmOpt is true the wasm binary is optimised via wasm-opt using wasmOptCfg.
func FindConfig ¶ added in v1.0.0
FindConfig searches for a wasmpack.yml file starting from startDir and walking up to the filesystem root, like go.mod discovery. Returns an empty string if no config file is found.
func Minify ¶
Minify takes a JavaScript source code as a byte slice and returns a minified version of it. It removes unnecessary whitespace and comments while preserving the functionality of the code.
func Obfuscate ¶ added in v1.0.0
Obfuscate obfuscates js using options parsed from a flag-style args string. Example args: "-controlFlowFlattening -compact=false"
func ObfuscateWithOptions ¶ added in v1.0.0
ObfuscateWithOptions obfuscates js using the provided options map. Use ParseObfuscateOptions or ObfuscateConfig.ToOptions to build the map.
func Pack ¶
Pack takes the wasm bytes, compresses them using the deflate algorithm, and returns a JavaScript snippet that inflates the compressed bytes and returns them as an Uint8Array.
func ParseObfuscateOptions ¶ added in v1.0.0
ParseObfuscateOptions parses a flag-style argument string into a javascript-obfuscator options map. Example: "-controlFlowFlattening -compact=false"
func WasmOpt ¶ added in v1.0.0
WasmOpt runs wasm-opt on the wasm file at path with the given flag slice, writing the result back to the same path. Example flags: []string{"-O2"}, []string{"-O4", "--enable-simd"}.
func Wrap ¶
Wrap takes the wasm_exec.js file from the go installation and wraps the packed wasm bytes in it, returning the resulting JavaScript code. If name is not empty, instead of running the wasm immediately it will be assigned to a global function with the provided name that can be called with an optional env object and args array.
func WrapCJS ¶ added in v1.0.0
WrapCJS wraps the packed wasm bytes in wasm_exec.js as a CommonJS module. The module exports a single `run(env, args)` function that instantiates and starts the WebAssembly module, returning a Promise. packed is the JavaScript expression returned by Pack.
func WrapESM ¶ added in v1.0.0
WrapESM wraps the packed wasm bytes in wasm_exec.js as an ES module. The module exports a single `run(env, args)` function that instantiates and starts the WebAssembly module, returning a Promise. packed is the JavaScript expression returned by Pack.
func WrapHTML ¶ added in v1.0.0
WrapHTML wraps the packed wasm IIFE inside a minimal HTML page. The <script> tag uses defer so it executes after HTML is parsed. packed is the JavaScript expression returned by Pack.
func WrapIIFE ¶ added in v1.0.0
WrapIIFE wraps the packed wasm bytes in wasm_exec.js as a self-executing IIFE. The WebAssembly module starts running as soon as the script is loaded. packed is the JavaScript expression returned by Pack.
func WriteDefaultConfig ¶ added in v1.0.0
WriteDefaultConfig writes a commented default wasmpack.yml to path.
Types ¶
type Config ¶ added in v1.0.0
type Config struct {
Pre string `yaml:"pre"`
Post string `yaml:"post"`
Garble GarbleConfig `yaml:"garble"`
Tinygo TinygoConfig `yaml:"tinygo"`
WasmOpt WasmOptConfig `yaml:"wasm-opt"`
Obfuscate ObfuscateConfig `yaml:"obfuscate"`
}
Config holds all wasmpack project configuration.
func DefaultConfig ¶ added in v1.0.0
func DefaultConfig() *Config
DefaultConfig returns a Config populated with sensible defaults.
func LoadConfig ¶ added in v1.0.0
LoadConfig reads and parses a wasmpack.yml file. Fields not present in the file keep their DefaultConfig values.
type GarbleConfig ¶ added in v1.0.0
type GarbleConfig struct {
Seed string `yaml:"seed"` // randomness seed (-seed)
Literals bool `yaml:"literals"` // obfuscate string literals (-literals)
Tiny bool `yaml:"tiny"` // smaller output (-tiny)
Flags string `yaml:"flags"` // extra space-separated garble flags
}
GarbleConfig holds options passed to the garble build tool.
func (GarbleConfig) ToArgs ¶ added in v1.0.0
func (g GarbleConfig) ToArgs() []string
ToArgs converts the GarbleConfig into a slice of garble flag strings.
type ObfuscateConfig ¶ added in v1.0.0
type ObfuscateConfig struct {
Compact bool `yaml:"compact"`
ControlFlowFlattening bool `yaml:"controlFlowFlattening"`
ControlFlowFlatteningThreshold float64 `yaml:"controlFlowFlatteningThreshold"`
DeadCodeInjection bool `yaml:"deadCodeInjection"`
DeadCodeInjectionThreshold float64 `yaml:"deadCodeInjectionThreshold"`
DebugProtection bool `yaml:"debugProtection"`
DebugProtectionInterval int `yaml:"debugProtectionInterval"`
DisableConsoleOutput bool `yaml:"disableConsoleOutput"`
DomainLock []string `yaml:"domainLock"`
DomainLockRedirectURL string `yaml:"domainLockRedirectURL"`
Exclude []string `yaml:"exclude"`
ForceTransformStrings []string `yaml:"forceTransformStrings"`
IdentifierNamesCache map[string]string `yaml:"identifierNamesCache"`
IdentifierNamesGenerator string `yaml:"identifierNamesGenerator"`
IdentifiersDictionary []string `yaml:"identifiersDictionary"`
IdentifiersPrefix string `yaml:"identifiersPrefix"`
IgnoreImports bool `yaml:"ignoreImports"`
InputFileName string `yaml:"inputFileName"`
Log bool `yaml:"log"`
NumbersToExpressions bool `yaml:"numbersToExpressions"`
OptionsPreset string `yaml:"optionsPreset"`
RenameGlobals bool `yaml:"renameGlobals"`
RenameProperties bool `yaml:"renameProperties"`
RenamePropertiesMode string `yaml:"renamePropertiesMode"`
ReservedNames []string `yaml:"reservedNames"`
ReservedStrings []string `yaml:"reservedStrings"`
Seed string `yaml:"seed"`
SelfDefending bool `yaml:"selfDefending"`
Simplify bool `yaml:"simplify"`
SourceMap bool `yaml:"sourceMap"`
SourceMapBaseURL string `yaml:"sourceMapBaseURL"`
SourceMapFileName string `yaml:"sourceMapFileName"`
SourceMapMode string `yaml:"sourceMapMode"`
SplitStrings bool `yaml:"splitStrings"`
SplitStringsChunkLength int `yaml:"splitStringsChunkLength"`
StringArray bool `yaml:"stringArray"`
StringArrayCallsTransform bool `yaml:"stringArrayCallsTransform"`
StringArrayCallsTransformThreshold float64 `yaml:"stringArrayCallsTransformThreshold"`
StringArrayEncoding []string `yaml:"stringArrayEncoding"`
StringArrayIndexesType []string `yaml:"stringArrayIndexesType"`
StringArrayIndexShift bool `yaml:"stringArrayIndexShift"`
StringArrayRotate bool `yaml:"stringArrayRotate"`
StringArrayShuffle bool `yaml:"stringArrayShuffle"`
StringArrayWrappersCount int `yaml:"stringArrayWrappersCount"`
StringArrayWrappersChainedCalls bool `yaml:"stringArrayWrappersChainedCalls"`
StringArrayWrappersParametersMaxCount int `yaml:"stringArrayWrappersParametersMaxCount"`
StringArrayWrappersType string `yaml:"stringArrayWrappersType"`
StringArrayThreshold float64 `yaml:"stringArrayThreshold"`
Target string `yaml:"target"`
TransformObjectKeys bool `yaml:"transformObjectKeys"`
UnicodeEscapeSequence bool `yaml:"unicodeEscapeSequence"`
}
ObfuscateConfig holds all javascript-obfuscator options.
func (*ObfuscateConfig) ToOptions ¶ added in v1.0.0
func (c *ObfuscateConfig) ToOptions() map[string]any
ToOptions converts the ObfuscateConfig into the options map consumed by the javascript-obfuscator engine.
type TinygoConfig ¶ added in v1.0.0
type TinygoConfig struct {
Target string `yaml:"target"` // compile target (e.g. "wasm", "wasi")
Opt string `yaml:"opt"` // optimization level (none, 0, 1, 2, s, z)
Flags string `yaml:"flags"` // extra space-separated tinygo flags
}
TinygoConfig holds options passed to the tinygo compiler.
func (TinygoConfig) ToArgs ¶ added in v1.0.0
func (t TinygoConfig) ToArgs() []string
ToArgs converts the TinygoConfig into a slice of tinygo flag strings.
type WasmOptConfig ¶ added in v1.0.0
type WasmOptConfig struct {
Level string `yaml:"level"` // optimization level (O1, O2, O3, O4, Os)
Flags string `yaml:"flags"` // extra space-separated wasm-opt flags
}
WasmOptConfig holds options passed to the wasm-opt tool.
func (WasmOptConfig) ToArgs ¶ added in v1.0.0
func (o WasmOptConfig) ToArgs() []string
ToArgs converts the WasmOptConfig into a slice of wasm-opt flag strings.