Skip to content

Improve template minifier #33016

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

Open
wants to merge 13 commits into
base: 2.4-develop
Choose a base branch
from
Open

Conversation

blmage
Copy link
Contributor

@blmage blmage commented May 14, 2021

Description (*)

This PR aims to improve the template minifier by :

  • fixing a variety of cases that are currently handled incorrectly,
  • commenting on each step of the minification process to explain what it does,
  • increasing the exhaustiveness of the corresponding test.

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes HTML minification breaks PHP code #32153

Manual testing scenarios (*)

  1. See HTML minification breaks PHP code #32153 or the updated test

Questions or comments

It is very likely that there remains cases that are handled incorrectly, mostly in JS code now, but it also is probably impossible to make the process entirely safe as long as the minification happens on the templates, given the mix of PHP and HTML / CSS / JS code.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented May 14, 2021

Hi @blmage. Thank you for your contribution
Here are some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here

ℹ️ Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review.

For more details, please, review the Magento Contributor Guide documentation.

⚠️ According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@m2-community-project m2-community-project bot added Priority: P3 May be fixed according to the position in the backlog. Severity: S2 Major restrictions or short-term circumventions are required until a fix is available. labels May 14, 2021
@blmage
Copy link
Contributor Author

blmage commented May 14, 2021

Still need to :

  • ensure that // comments are only removed inside <script> tags
  • update the tests

but this looks promising so far, if adding a dependency on nikic/php-parser is not a problem.

@blmage blmage marked this pull request as ready for review May 15, 2021 13:31
@blmage
Copy link
Contributor Author

blmage commented May 15, 2021

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@mrtuvn
Copy link
Contributor

mrtuvn commented May 16, 2021

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@mrtuvn
Copy link
Contributor

mrtuvn commented May 16, 2021

@magento run Functional Tests CE, Functional Tests B2B, Functional Tests EE, WebAPI Tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@mrtuvn mrtuvn added Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Award: bug fix Award: category of expertise labels May 16, 2021
@mrtuvn
Copy link
Contributor

mrtuvn commented May 16, 2021

Fail tests may not related with changes from this pull request

@Den4ik Den4ik self-assigned this May 17, 2021
@Den4ik
Copy link
Contributor

Den4ik commented May 17, 2021

@magento run WebAPI Test, Functional Tests B2B

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@Den4ik
Copy link
Contributor

Den4ik commented May 19, 2021

@magento run WebAPI Test, Functional Tests B2B

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@Den4ik
Copy link
Contributor

Den4ik commented May 20, 2021

@magento run WebAPI Test

@magento-automated-testing
Copy link

Failed to run the builds. Please try to re-run them later.

@Den4ik
Copy link
Contributor

Den4ik commented May 20, 2021

@magento run WebAPI Test

@blmage blmage force-pushed the template-minifier branch from efcb392 to 4e50eff Compare October 16, 2021 12:58
@blmage
Copy link
Contributor Author

blmage commented Oct 16, 2021

@mrtuvn @Den4ik Done!

@blmage
Copy link
Contributor Author

blmage commented Oct 16, 2021

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@Den4ik
Copy link
Contributor

Den4ik commented Oct 16, 2021

Let's wait test results

@blmage
Copy link
Contributor Author

blmage commented Oct 18, 2021

@magento run Database Compare, Functional Tests CE, Functional Tests EE, Integration Tests, Sample Data Tests CE,
Static Tests, Unit Tests, WebAPI Tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@Den4ik
Copy link
Contributor

Den4ik commented Oct 20, 2021

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@DanielRuf
Copy link
Contributor

I'm surprised that Magento wants to minify source code (PHP) instead of resulting html files.

Generally the current approach is not how it should work.

At least view_preprocessed files should ideally not be minified.

At least the current setup leads to many issues and the new proposed solution here still does some black magic / voodoo with regex.

The catch statement probably throws away too complex code, PHP 8 support might have to be added, the regex does not cover the optional trailing semicolon and so on (see #32153 (comment)).

Please see also my comments at #32153

Truely, (complex) regular expressions are a safe way to shoot yourself into the foot, with a double-barrel shotgun.

@DanielRuf
Copy link
Contributor

Generally I would also propose to support some ignore-comment syntax (like eslint-ignore, ts-ignore, htmlmin:ignore by html-minifier and more) to tell the minifier, to not touch code blocks if needed and have some control, so we don't have to patch core files in the future, if some minified code leads to such issues (there are way more cases where newlines and other elements are crucial).

$content = $prettyPrinter->prettyPrintFile($ast);
$heredocs = $prettyPrinter->getDelayedHeredocs();
} catch (\Error $error) {
// Some PHP code is seemingly invalid, or too complex.
Copy link

@maaarghk maaarghk Jul 7, 2023

Choose a reason for hiding this comment

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

In this case doesn't it make more sense to just log a warning and return the unminified code instead of guessing that you could do a better job than the PHP parser library at minifying some mangled script using regex?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Award: advanced Award: bug fix Award: category of expertise Component: View improvement Priority: P3 May be fixed according to the position in the backlog. Progress: review Release Line: 2.4 Severity: S2 Major restrictions or short-term circumventions are required until a fix is available.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HTML minification breaks PHP code
6 participants