Skip to content
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

Fix/#594 #988

Merged
merged 2 commits into from
Jun 9, 2023
Merged

Fix/#594 #988

merged 2 commits into from
Jun 9, 2023

Conversation

HHongSeungWoo
Copy link
Contributor

First of all, I want to thank you for making such a great library.

I'm using ngl with React.
When I install ngl with npm and build with swc, I get the error
SyntaxError: function statement requires a name

The problem is that the result of the getAllowedContours function is inlined and the name removed.
After this, in the makeWorkerString function, the error occurs as a result of (function () { return [...] }).toString()

So I removed getAllowedContours from __deps and inlined it.

@fredludlow
Copy link
Collaborator

Hi @HHongSeungWoo - thanks for this. Is the underlying problem that makeWorkerString and swc have some different assumptions about what attributes a function needs to have (i.e. a name) and when it's a candidate for in-lining? I wonder if putting an export in front of the function definition would prevent inlining?

Anyway - will merge this as it fixes a problem and it doesn't seem to cause any problems, thanks again!

@fredludlow fredludlow merged commit b68ddd1 into nglviewer:master Jun 9, 2023
@fredludlow
Copy link
Collaborator

Available in 2.1.1 https://www.npmjs.com/package/ngl

@HHongSeungWoo
Copy link
Contributor Author

@fredludlow

webpack and swc both use or reference terser for minification and optimization. The getAllowedContours function is a pure function that returns a constant value without any arguments, and it is called only once in the code. Therefore, the single invocation gets inlined by directly assigning the return value, and getAllowedContours is no longer used.

Since the function is being passed in the __deps without being called anymore, during the process of minification and mangling, its name is removed and it gets inlined.

There are a few possible solutions to this issue. One option is to refactor the code. Another option is to configure the pure_funcs or keepfnname options in SWC or webpack. However, the simplest solution in this case is to assign the value directly.

@HHongSeungWoo HHongSeungWoo deleted the fix/#594 branch June 28, 2023 08:19
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