@@ -96,17 +96,18 @@ cd "$PACKAGE_DIR"
9696
9797echo " Building $PACKAGE_NAME ..."
9898
99- # Create pkg directory if it doesn't exist
99+ # Create a new clean directory package directory
100+ rm -rf pkg
100101mkdir -p pkg
101102
102103if [ " $USE_WASM_PACK " = true ]; then
103104 # Build using wasm-pack
104105 echo " Building with wasm-pack..."
105-
106+
106107 # Disable LTO for wasm-pack builds to avoid conflicts
107108 export CARGO_PROFILE_RELEASE_LTO=false
108109 export RUSTFLAGS=" -C lto=off"
109-
110+
110111 # Add features if specified
111112 FEATURES_ARG=" "
112113 if [ -n " ${CARGO_BUILD_FEATURES:- } " ]; then
@@ -117,19 +118,19 @@ if [ "$USE_WASM_PACK" = true ]; then
117118 # Explicitly pass default features to ensure they're used
118119 FEATURES_ARG=" --features default"
119120 fi
120-
121+
121122 echo " Running: wasm-pack build --target $TARGET_TYPE --release --no-opt $FEATURES_ARG "
122123 wasm-pack build --target " $TARGET_TYPE " --release --no-opt $FEATURES_ARG
123124else
124125 # Build using cargo directly
125126 echo " Building with cargo..."
126-
127+
127128 # Add features if specified
128129 FEATURES_ARG=" "
129130 if [ -n " ${CARGO_BUILD_FEATURES:- } " ]; then
130131 FEATURES_ARG=" --features $CARGO_BUILD_FEATURES "
131132 fi
132-
133+
133134 cargo build --target wasm32-unknown-unknown --release $FEATURES_ARG \
134135 --config ' profile.release.panic="abort"' \
135136 --config ' profile.release.strip=true' \
@@ -138,22 +139,22 @@ else
138139 --config ' profile.release.lto=true' \
139140 --config ' profile.release.opt-level="z"' \
140141 --config ' profile.release.codegen-units=1'
141-
142+
142143 # Run wasm-snip if available
143144 if command -v wasm-snip & > /dev/null; then
144145 wasm-snip " ../../target/wasm32-unknown-unknown/release/${PACKAGE_NAME// -/ _} .wasm" \
145146 -o " ../../target/wasm32-unknown-unknown/release/${PACKAGE_NAME// -/ _} .wasm" \
146147 --snip-rust-fmt-code \
147148 --snip-rust-panicking-code
148149 fi
149-
150+
150151 # Run wasm-bindgen
151152 echo " Running wasm-bindgen..."
152153 if ! command -v wasm-bindgen & > /dev/null; then
153154 echo " Error: 'wasm-bindgen' not found. Install via 'cargo install wasm-bindgen-cli'." >&2
154155 exit 1
155156 fi
156-
157+
157158 wasm-bindgen \
158159 --typescript \
159160 --out-dir=pkg \
165166# Optimize the WASM file
166167if [ " $OPT_LEVEL " != " none" ] && command -v wasm-opt & > /dev/null; then
167168 echo " Optimizing wasm using Binaryen (level: $OPT_LEVEL )..."
168-
169+
169170 WASM_PATH=" pkg/$WASM_FILE "
170-
171+
171172 if [ " $OPT_LEVEL " = " full" ]; then
172173 # Check wasm-opt version to determine available options
173174 WASM_OPT_VERSION=$( wasm-opt --version 2> /dev/null || echo " " )
174-
175+
175176 # Core optimization flags that should work with most versions
176177 CORE_FLAGS=(
177178 --strip-producers
@@ -191,7 +192,7 @@ if [ "$OPT_LEVEL" != "none" ] && command -v wasm-opt &> /dev/null; then
191192 -Oz
192193 -Oz
193194 )
194-
195+
195196 # Additional flags to test for compatibility
196197 OPTIONAL_FLAGS=(
197198 " --code-folding"
@@ -209,7 +210,7 @@ if [ "$OPT_LEVEL" != "none" ] && command -v wasm-opt &> /dev/null; then
209210 " --generate-global-effects"
210211 " --abstract-type-refining"
211212 )
212-
213+
213214 # Test which optional flags are supported
214215 SUPPORTED_FLAGS=()
215216 for flag in " ${OPTIONAL_FLAGS[@]} " ; do
@@ -219,15 +220,15 @@ if [ "$OPT_LEVEL" != "none" ] && command -v wasm-opt &> /dev/null; then
219220 echo " Note: $flag not supported by this wasm-opt version, skipping..."
220221 fi
221222 done
222-
223+
223224 # Run optimization with core flags and any supported optional flags
224225 wasm-opt \
225226 " ${CORE_FLAGS[@]} " \
226227 " ${SUPPORTED_FLAGS[@]} " \
227228 " $WASM_PATH " \
228229 -o \
229230 " $WASM_PATH "
230-
231+
231232 # Create optimized version for wasm-sdk
232233 if [ " $PACKAGE_NAME " = " wasm-sdk" ]; then
233234 cp " $WASM_PATH " " pkg/optimized.wasm"
0 commit comments