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

[Feature request] nameCache and __MANGLE_PROP__ overlap #1527

Open
gabriel-calin-lazar opened this issue Apr 30, 2024 · 5 comments
Open

[Feature request] nameCache and __MANGLE_PROP__ overlap #1527

gabriel-calin-lazar opened this issue Apr 30, 2024 · 5 comments
Labels

Comments

@gabriel-calin-lazar
Copy link

Bug report or Feature request?

Feature request

Information

nameCache option can be used to fix protected problem across async chunk and main bundle - same mangling should be in place

when MANGLE_PROP is in use in main bundle - make use of nameCache to also cover the mangled property name(s) so they can be safely used in async chunk - replaced accordingly

@fabiosantoscode
Copy link
Collaborator

I notice you're talking about chunks, so I assume you're using webpack. Webpack doesn't use the nameCache option, which is why mangling properties doesn't work across chunks. But it may be possible if you disable parallelism in terser-webpack-plugin.

@fabiosantoscode
Copy link
Collaborator

There's something that Terser and the bundler plugins can do together, which is to provide a pre-step to gather all properties, and create a name cache, and then use it in parallel. However, this will take some time to perform and most likely it's not worth it. Simply disabling parallelism would be faster.

@gabriel-calin-lazar
Copy link
Author

@fabiosantoscode I have parallel turned off, but it still does not work for MANGLE_PROP usage - only for protected members across main bundle and async chunk
Screenshot 2024-04-30 at 13 07 59

@fabiosantoscode
Copy link
Collaborator

Ok, I understand now. Terser finds all annotated properties, but it doesn't store them in nameCache for subsequent runs.

This might not be possible to fix completely. Consider this example:

In chunk 1, you have a property prop which doesn't have a /*@__MANGLE_PROP__*/ annotation so it doesn't get mangled or cached. However, in chunk 2 we find prop with a /*@__MANGLE_PROP__*/ annotation. It is, of course, too late to mangle its chunk 1 sibling.

@gabriel-calin-lazar
Copy link
Author

There is no second chunk. Just these 2
1 main bundle where
/@MANGLE_PROP/ is in use on an object prop
a dynamic import is present
1 async chunk (caused by the dynamic import in the main bundle) where
the object created in the main bundle is in use
The problem is that the object from the main bundle has the specific property mangled but the code from the chunk is not aware of the property mangling as it refers to the specific property by name - not mangled.

Also - it seems that @MANGLE_PROP usage would mangle all occurrences of the specific prop - not just on the object where it is in use. This is not obvious from the docs.

const x = {
/@MANGLE_PROP/
y: 0
}

const x = {
y: 0
}

Both occurrences of y - not just the first one - get mangled. This is not just the first one.
It also cover All occurrences of y in the specific bundle/chunk.
So a single badly used annotation can break the output - erroring at runtime - if your code is relying on the specific annotated property. See the following example

console.log({
/@MANGLE_PROP/ y: 100
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants