Skip to content

Conversation

kunyoungparkk
Copy link
Collaborator

This PR updates lottie-player.ts to adopt the new TypeScript bindings introduced in thorvg#3678.

These updates are related to thorvg.web#16, which requests better TypeScript support for the WASM output.

Copy link

vercel bot commented Aug 2, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
thorvg-perf-test Ready Ready Preview Comment Sep 29, 2025 3:37am

@tinyjin tinyjin self-requested a review August 3, 2025 15:30
@tinyjin tinyjin assigned tinyjin and kunyoungparkk and unassigned tinyjin Aug 3, 2025
@tinyjin tinyjin added the javascript Pull requests that update javascript code label Aug 3, 2025
Copy link
Member

@tinyjin tinyjin left a comment

Choose a reason for hiding this comment

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

Thank you please check comments

Copilot

This comment was marked as outdated.

@kang-hyuck
Copy link

I run this patch and found that there is some error.

test environmts:

  • ubunu 22.04 LTS (wsl2)
  • bash (user shell, echo $SHELL)

error log:


## npm run build

> @thorvg/[email protected] build
> npm run clean && npm run copy:wasm && npm run copy:js && THORVG_VERSION=$(sed -n -e 4p ./thorvg/meson.build | sed 's/..$//' | sed -r 's/.{19}//') rollup -c --bundleConfigAsCjs && rm -rf ./dist/thorvg-wasm.js


> @thorvg/[email protected] clean
> rm -rf dist && mkdir dist && touch dist/index.js


> @thorvg/[email protected] copy:wasm
> cp ./thorvg/build_wasm/src/bindings/wasm/thorvg-wasm.wasm ./dist


> @thorvg/[email protected] copy:js
> cp ./thorvg/build_wasm/src/bindings/wasm/thorvg-wasm.{js,d.ts} ./dist

cp: cannot stat './thorvg/build_wasm/src/bindings/wasm/thorvg-wasm.{js,d.ts}': No such file or directory

comment:
I think ubuntu 22.04 doesn't use bash or user's shell when running cp ./thorvg/build_wasm/src/bindings/wasm/thorvg-wasm.{js,d.ts} ./dist command in npm, and the shell (not bash) doesn't seem to handle brace patterns like {js, d.ts}.

Is there no such error log in Mac environment?

@kunyoungparkk
Copy link
Collaborator Author

kunyoungparkk commented Aug 6, 2025

@kang-hyuck Thanks for pointing that out!
I didn’t encounter this issue on macOS. The build script worked without problems there.

I’ll test it on Ubuntu and update the script to avoid relying on brace expansion.

@kunyoungparkk
Copy link
Collaborator Author

@kang-hyuck I tested it again on my WSL2 and confirmed the same issue. Thanks to your insight, I was able to resolve it. Much appreciated!

Copy link
Member

@tinyjin tinyjin left a comment

Choose a reason for hiding this comment

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

Need tsd generation script here

@kunyoungparkk
Copy link
Collaborator Author

kunyoungparkk commented Aug 15, 2025

@tinyjin Based on the shell script you provided, I modified this PR to create an emit_tsd folder internally and copy only the .d.ts file to the dist folder during the build process.

@kunyoungparkk kunyoungparkk requested a review from tinyjin August 15, 2025 00:34
Copy link
Member

@tinyjin tinyjin left a comment

Choose a reason for hiding this comment

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

Thank you. Please check comments

@tinyjin tinyjin requested a review from Copilot August 17, 2025 13:04
Copilot

This comment was marked as outdated.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates the TypeScript bindings for lottie-player.ts to use proper type definitions from the WASM module, addressing the need for better TypeScript support in the WASM output.

Key changes:

  • Adds proper TypeScript type definitions by importing MainModule and TvgLottieAnimation types
  • Replaces generic any types with specific type annotations throughout the codebase
  • Adds null safety checks for module initialization and operations

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
wasm_emit_tsd.sh Adds script to generate TypeScript definition files during WASM build process
src/lottie-player.ts Updates type annotations and adds null safety checks for WASM module usage
package.json Modifies build script to copy generated TypeScript definition files
Comments suppressed due to low confidence (1)

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@kunyoungparkk
Copy link
Collaborator Author

kunyoungparkk commented Aug 21, 2025

@tinyjin
I made two changes that I think were necessary:
1. Stopped moving thorvg-wasm.d.ts into the dist folder during the build process.
I believe the thorvg-wasm.d.ts file doesn’t need to be included in the build output folder.
2. Reverted the ts-ignore.
I think that import is only accessible during the build process, so ts-ignore should remain.

@kunyoungparkk
Copy link
Collaborator Author

lottie: clean up code
Regarding this change(about overloaded load function), I’ve submitted a PR to thorvg that adds support for overload bindings:
thorvg/thorvg#3760

That PR should be merged first in order to properly keep the current code structure here.

@kunyoungparkk
Copy link
Collaborator Author

@tinyjin
The previously mentioned issue has now been resolved.
I have updated this branch accordingly and pushed the revised changes.

Could you re-review the PR at your convenience? Thank you.

Copy link

vercel bot commented Oct 8, 2025

@kunyoungparkk is attempting to deploy a commit to the thorvg-web Team on Vercel.

A member of the Team first needs to authorize it.

@tinyjin
Copy link
Member

tinyjin commented Oct 14, 2025

This project relies on lottie-player.d.ts as the main type definition, however the WASM generated type definition is separated. These should be unified, and types should be arranged in a single source definition for API consistency and user-side hinting.

We can re-export the WASM type definitions in lottie-player.d.ts, so users won't miss the WASM types.

// lottie-player.d.ts

// re-export WASM types
export type { 
  MainModule, 
  TvgLottieAnimation, 
  ClassHandle,
  EmbindString 
} from './thorvg';

Package main type def

CleanShot 2025-10-15 at 03 15 35@2x

thorvg.d.ts isn't integrated with the main type definition

CleanShot 2025-10-15 at 03 12 36@2x

Copy link
Member

@tinyjin tinyjin left a comment

Choose a reason for hiding this comment

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

Thank you for contribution. Please check comments as well as above suggestion


private _loadBytes(data: Uint8Array): void {
const isLoaded = this.TVG.load(data, this.fileType, this.canvas!.width, this.canvas!.height, '');
const isLoaded = this.TVG!.load(data, this.fileType, this.canvas!.width, this.canvas!.height);
Copy link
Member

Choose a reason for hiding this comment

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

This looks unrelated changes to TypeScript binding support. Removing unused parameter could be separated in another patch.

this.pause();
this.currentFrame = curFrame;
this.TVG.frame(curFrame);
this.TVG!.frame(curFrame);
Copy link
Member

Choose a reason for hiding this comment

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

Overuse of the ! operator may cause unexpected runtime errors.

Please add an if (!this.TVG) check at the beginning of the function and remove the non-null assertion.

alias({
entries: [
{ find: 'thorvg', replacement: path.join('..', presetMap[preset].path, 'thorvg') },
{ find: '../dist/thorvg.js', replacement: path.join('..', presetMap[preset].path, 'thorvg') },
Copy link
Member

Choose a reason for hiding this comment

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

wrong alias...
There is no case ../dist/thorvg.js in import statement.

// @ts-ignore: WASM Glue code doesn't have type & Only available on build progress
import Module from 'thorvg';
import Module from '../dist/thorvg';
import type { MainModule, TvgLottieAnimation } from '../dist/thorvg';
Copy link
Member

Choose a reason for hiding this comment

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

import Module, { type MainModule, type TvgLottieAnimation } from '../dist/thorvg';


type LottieJson = Map<PropertyKey, any>;
type TvgModule = any;
type TvgModule = TvgLottieAnimation;
Copy link
Member

@tinyjin tinyjin Oct 14, 2025

Choose a reason for hiding this comment

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

Now we have type support, we don't actually need this type (No need wrapper type). Instead, let's replace TvgModule with TvgLottieAnimation.

const buffer = this.TVG.render();
const clampedBuffer = new Uint8ClampedArray(buffer.buffer, buffer.byteOffset, buffer.byteLength);
const buffer = this.TVG!.render();
const clampedBuffer = new Uint8ClampedArray(buffer.buffer as ArrayBuffer, buffer.byteOffset, buffer.byteLength);
Copy link
Member

Choose a reason for hiding this comment

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

as is a signal that the data type of a value has been wrongly chosen. There is no point in introducing type safety if we bypass it.

see: thorvg/thorvg#3887

check:

const buffer = this.TVG!.render();
const clampedBuffer = new Uint8ClampedArray(buffer, 0, buffer.byteLength);

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

Labels

javascript Pull requests that update javascript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants