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

    Imported before Forger kept the single votes, the next import adds them.

  • Verified V +2

    Imported before Forger kept the single votes, the next import adds them.

Issues 3

Files 31

Show 11 more files