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: mangle with destructuring #18319

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ahabhgk
Copy link
Contributor

@ahabhgk ahabhgk commented Apr 12, 2024

fix #18278

What kind of change does this PR introduce?

introduce DestructuringAssignmentPropertyKeyDependency to replace the key in destructuring instead of bailout the mangleExports in some condition.

for example in #18095, we will bailout the mangleExports (canMangle: false) when the module is a reexport of another module, but in this PR, we will no longer bailout:

// module1.js
export * as module2 from "./module2"

// index.js
import { module2 } from "./module1"
const { aaa } = module2

index.js will generate:

/* harmony import */ var _module1__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./module1 */ "./module1.js");
const { /* aaa */ "q": aaa } = _module1__WEBPACK_IMPORTED_MODULE_0__/* .module2 */ .h
//      ------------------
//      replaced by DestructuringAssignmentPropertyKeyDependency, aaa -> /* aaa */ "q": aaa

and since the reexport no longer bailout the mangleExports, this should make the output size smaller in some cases.

Did you add tests for your changes?

added

Does this PR introduce a breaking change?

no

What needs to be documented once your changes are merged?

none

@webpack-bot
Copy link
Contributor

For maintainers only:

  • This needs to be documented (issue in webpack/webpack.js.org will be filed when merged)
  • This needs to be backported to webpack 4 (issue will be created when merged)

@ahabhgk ahabhgk marked this pull request as ready for review April 13, 2024 10:14
Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

/cc @vankop

@vankop
Copy link
Member

vankop commented Apr 16, 2024

here is better to use "webpack" design. we should introduce new hook for destructuring assignment in JavascriptParser
e.g. this.hooks.destructuringAssignment, this will allow to use "webpack" power in any plugin =)

then just add handler in HarmonyParser like:

parser.hooks.destructuringAssignment
  .for(harmonySpecifierTag)
  .tap("HarmonyImportDependencyParserPlugin", handler);

also we should not touch dependencies here.. just mark usage ( not sure here )

@vankop
Copy link
Member

vankop commented Apr 16, 2024

btw we dont need to bail out mangling ( only when we can't compute properties ), in dependencies we can pass range that we will change to something like "mangledExportHere": variable ( this will be next improvement with new dependency )

@ahabhgk
Copy link
Contributor Author

ahabhgk commented Apr 18, 2024

@vankop I tried to refactor this but failed, I'm not very familiar with the code related to the JavascriptParser, can you give me more suggestions (add comments to specific lines under the changed files would be better :P), or you can just edit on this PR, feel free to take over this.

@webpack-bot
Copy link
Contributor

@ahabhgk Thanks for your update.

I labeled the Pull Request so reviewers will review it again.

@alexander-akait Please review the new changes.

@ahabhgk
Copy link
Contributor Author

ahabhgk commented Apr 18, 2024

Figured out why before failed, made a refactor, now the logic of replacing the destructuring assignment an import specifier is handled by HarmonyDestructuredImportSpecifierDependency:

import * as module from "./module"
const { a, b } = module
//--------------------- will be replaced by HarmonyDestructuredImportSpecifierDependency
//                      and `"mangledExportHere": a` will also be done by this dependency

But I am still confused about why we need to introduce the new this.hooks.destructuringAssignment hook, is it used to replace parser.destructuringAssignmentPropertiesFor? so plugins can use this hook to get some data and create dependencies from these data, instead of using parser.destructuringAssignmentPropertiesFor to get these data?

@alexander-akait
Copy link
Member

/cc @vankop

@vankop
Copy link
Member

vankop commented May 1, 2024

so.. based on #16941 pull request. what we need to do here:

  1. improve parser part ( as I see it is already there )
  2. adjust all DestructuringAssignmentProperty references with new data structure ( also done as I see )
  3. improve current dependencies ( only lib/dependencies/HarmonyImportSpecifierDependency.js I guess )
    • in this improvement we should I guess revert this part or fix this to allow mangling
    • then fix render part with rendering mangled version

@vankop
Copy link
Member

vankop commented May 1, 2024

in render part we just need to add ( dont change existing code ) something like:

if (dep. referencedPropertiesInDestructuring)
  for (const property of dep. referencedPropertiesInDestructuring) source.replace(...)

@vankop
Copy link
Member

vankop commented May 1, 2024

if you want I can finish this PR @ahabhgk

@ahabhgk
Copy link
Contributor Author

ahabhgk commented May 1, 2024

@vankop Yes, feel free to change on this PR

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

Successfully merging this pull request may close these issues.

mangled exports breaks with destructuring assignment of JSON imports
4 participants