Review main Merged

73004[FEATURE] Add infrastructure for ES6 modules and importmaps

Benjamin Franzke CR +2 V +2 Expert +1107 −31 updated 01/18/22, 10:12

Commit message

JavaScript ES6 modules may now be used instead of AMD modules, both in
backend and frontend context. JavaScript node-js style path resolutions
are managed by importmaps, which allow web pages to control the
behavior of JavaScript imports.

By the time of writing importmaps are supported natively by
Google Chrome, a polyfill is available for Firefox and Safari and
included by TYPO3 core and applied whenever an importmap is emitted.

RequireJs is shimmed to prefer ES6 modules if available, allowing any
extension to ship ES6 modules by providing an importmap configuration
in Configuration/JavaScriptModules.php while providing full backwards
compatibility support for extensions that load modules via RequireJS.

For security reasons importmap configuration is only emitted when the
modules are actually used, that means when a module has been added to
the current page response via via `PageRenderer->loadJavaScriptModule()`
or `JavaScriptRenderer->addJavaScriptModuleInstruction()`.

Further Notes:

1) importmaps serve multiple purposes:
 * They are used to map away cache busting hashes in filenames:
   https://github.com/WICG/import-maps#mapping-away-hashes-in-script-filenames
 * They allow to avoid relative paths between extensions,
   which wouldn't work with typo3/cms-composer-installers v4
 * They allow for extension less imports, like: `import 'jquery'`
   https://github.com/WICG/import-maps#extension-less-imports
2) The importmap becomes very large if all modules are included, but
   preparation is made to allow to optimize this in future:
 * We map all bare module imports to use .js suffix, allowing prefix
   definitions. To use that feature we'll need cache-invalidation for
   extensions paths and that is out of scope for this patch, but become
   possible with typo3/cms-composer-installers v4
 * Configuration/JavaScriptModules.php contains dependency information
   in order to only load importmap configuration for modules that are
   actually needed (loaded modules are tracked by `ImportMap` and their
   respective configuration and dependencies are loaded). This is
   also important for security reasons in order to avoid disclosing all
   installed extensions.
3) Usage in TYPO3 Backend will follow in #96511

Commands executed:

  yarn add es-module-shims

Review

  • Code review CR +2 Done
    • Benjamin Franzke +2
    • Andreas Kienast +1
    • Benni Mack +1
  • Verified V +2 Done
    • Benjamin Franzke +2
    • Andreas Kienast +1
    • Benni Mack +1
    • core-ci +1 CI

Issues 3

Files 31

Show 11 more files

Review log 1320

  1. Benjamin Franzke uploaded patch set 1 01/13/22, 08:47

  2. Benjamin Franzke wrote 01/13/22, 08:55

    Topic set to esm
  3. core-ci voted on patch set 1 V -1 01/13/22, 09:04 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/22006

  4. Benjamin Franzke uploaded patch set 2 01/13/22, 09:15

  5. core-ci voted on patch set 2 V -1 01/13/22, 09:30 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/22007

  6. core-ci commented on patch set 2 01/13/22, 12:17 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/22007

  7. core-ci voted on patch set 2 V +1 01/13/22, 12:22 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/22007

  8. Benjamin Franzke uploaded patch set 3 01/14/22, 17:14

  9. core-ci voted on patch set 3 V +1 01/14/22, 17:29 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/22076

  10. Benjamin Franzke uploaded patch set 4 01/14/22, 18:26

  11. core-ci voted on patch set 4 V +1 01/14/22, 18:42 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/22080

  12. Andreas Kienast commented on patch set 4 01/15/22, 15:12

    ImportMap.php:88

    [EA] 'count(...)' is used in a loop and is a low performing construction.

    ImportMap.php:113

    Would it make sense to move this into the `if ($includePolyfill)` branch?

    ImportMap.php:121

    Should we add JSON_THROW_ON_ERROR as well?

    ImportMap.php:173

    Declaring the variable is redundant

    ImportMap.php:208

    [EA] This closure can be declared as static (better scoping; in some cases can improve performance).

    ImportMap.php:217

    Constant from base class '\RegexIterator' referenced via child class

    ImportMap.php:246

    Declaration is redundant

    ImportMap.php:255

    [EA] 'array_merge(...)' is used in a loop and is a resources greedy construction. See https://dev.to/klnjmm/never-use-arraymerge-in-a-for-loop-in-php-5go1 as well

    Feature-96510-InfrastructureForJavaScriptModulesAndImportmaps.rst:36

    without
  13. Benjamin Franzke uploaded patch set 5 01/15/22, 16:50

  14. Benjamin Franzke commented on patch set 5 01/15/22, 16:50

    ImportMap.php:88 Reply

    Done

    ImportMap.php:113 Reply

    Full ACK. Done.

    ImportMap.php:121 Reply

    Done

    ImportMap.php:173 Reply

    Done

    ImportMap.php:208 Reply

    Done

    ImportMap.php:217 Reply

    Done

    ImportMap.php:246 Reply

    Done

    ImportMap.php:255 Reply

    good catch, done

    Feature-96510-InfrastructureForJavaScriptModulesAndImportmaps.rst:36 Reply

    Done
  15. core-ci voted on patch set 5 V +1 01/15/22, 17:05 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/22122

  16. Benni Mack voted on patch set 5 CR +1 01/17/22, 07:40

  17. Andreas Kienast voted on patch set 5 V +1 CR +1 01/17/22, 10:47

  18. Benni Mack commented on patch set 5 V +1 01/18/22, 09:15

    Although I'm not 100% sure of the registration format ("dependencies" in the array), I think we should proceed here and see how we can improve this until 12.0.
  19. Benjamin Franzke voted on patch set 5 V +2 CR +2 01/18/22, 10:11

  20. Benjamin Franzke merged patch set 6 01/18/22, 10:12