Review main Abandoned

65407[POC][WIP][TASK] TypeScript: Do only use ES6 exports, no pseudo imports

Benjamin Franzke CR 0 V 0 Expert +1087 −979 updated 01/14/22, 19:29

Commit message

Advantages:
 * Preparation for ES6 future by configuring
   TypeScript to produce ES6 builds, that means
   we won't (and can't) use ES6 incompatible `export =` export syntax
   any longer – while keeping backwards compatbility
   in the conversion to AMD.
 * Actually less generated code, as @buxlabs/es6-to-amd uses
   AMD return syntax (which is actually recommended), instead of the
   `exports` object that TypeScript's native AMD exporter uses
 * Dynamic runtime imports (previously using require()), now with
   ES2020 import() method (Note import() is mapped to a Promise + require()
   by @buxlabs/es6-to-amd wrapper, so we do not use the browsers import()
   method, as that would only work with ES6 modules not with AMD).

Note: It was due to TypeScript's decision which maps the default export
to the `default` property, that TYPO3 had to use the non standard
pseudo export syntax (`export =` and `import = require()`) to model
the traditional AMD module in order to preserve backwards compatbility
when converting javascript modules to typescript.

With @buxlabs/es6-to-amd we now use a different ES6 to AMD conversion
strategy that maps default exports to the traditional AMD default export,
which allows to use native ES6 export syntax within typescript.

That means the AMD module signature doesn't change, because we do
convert ES6 *default* exports to *direct* AMD module return values,
instead of returning an object with a default property (which
is what TypeScript would have done for ES6 default exports).
As we already used `export default` for some modules (and TypeScript
therefore added default properties), we provide backwards
compatibility for these modules by adding a default property getter
function that returns a reference to the module itself and triggers a
deprecation warning.

This change actually has one slight limitation (which could also be seen
as a feature):
Mapping the default export to the return value (in AMD) means a
TypeScript/JavaScript module can not mix and match default and named
exports. That means we have to choose for modules between two options:

 * Exporting exactly *one* default export
 * Exporting one or multiple named exports

(Note, this has been the case with `export =` expression as well,
it's just that we keep this limitation).

No manual changes have been applied in Sources/TypeScript, besides for
  Sources/TypeScript/backend/Resources/Public/TypeScript/Element/ImmediateActionElement.ts
  Sources/TypeScript/t3editor/Resources/Public/TypeScript/Element/CodeMirrorElement.ts
All changes have been performed with the following find/sed command.
This automatic migration will ease a possible backport to v10.4 (which
isn't strictly needed, but will ease upcoming backports).

Used Commands:

cd Build
find Sources/TypeScript/ -name '*.ts' -exec sed -i \
        -e "s/export default class \\(.*\\) {/&\\n  public static get default(): any {\\n    console.warn('The property .default of module \\1 has been deprecated, use \\1 directly.');\\n    return this;\\n  }\\n/" \
        -e "s/class DragDrop {/&\\n  public static get default(): any {\\n    console.warn('The property .default of module DragDrop has been deprecated, use DragDrop directly.');\\n    return this;\\n  }\\n/" \
	-e 's/export =/export default/' \
	-e 's/ = require(\(.*\))/ from \1/g' \
        -e '/top.require/!s/require(\[\([^]]*\)\], /import(\1).then(/' \
	-e 's/\(import(.*).then(\(function\|\)(\)\([^:]*\): \([^)]*\))/\1{default: \3}: {default: \4})/' \
	-e "s/\\(import(\\('[^']*'\\)).then(\\(function\\|\\)(\\)\\({[^}]*}\\): {default: [^}]*}/\\1\\4: typeof import(\\2)/" \
	-e '/top.require/!s/require(\[\([^]]*\)\])/import(\1)/' \
	-e '/top.require/!s/require(\([^,]*\), /Promise.all(\1.map((module: string) => import(module))).then(/' \
        -e 's/require\.specified(/window.&/' \
	'{}' +
yarn add --dev @buxlabs/es6-to-amd@^1.1.2
yarn add --dev "https://github.com/bnf/es6-to-amd#mapping-imports-v1"
yarn remove @types/requirejs
grunt build

Out of scope for this patch:
 * We still use require.specified() for conditionally loading the
   deprecated RsaAuthenticationModule (but we call the global window.require module now,
   as `require` is no longer avaiable in TypeScript ES6 mode).
   Note: This functionality can not be implemented as-is with dynamic imports.
   With dyanmic import we would need to use a try-to-load-and-fail strategy.
   We therefore stick with window.require.specified for this special usecase.
 * We still use top.require() in some places (used to load dependencies for modals
   that are rendered into the outer/top frame).
   This could be solved in a later commit by sending a signal to the top frame.

We disable "esModuleInterop" again (which was enabled in #91954) as we now
only need allowSyntheticDefaultImports for type definitions from the
@types/* packages where `export =` is still used for non ES6 modules like
jquery. Explanation: We don't need `esModuleInterop` anymore, as we're *not*
using the default property, which esModuleInterop tries to workaround/fix.
We are therefore automatically compatible to 3rd party libraries, without
the need for typescript to emit __importDefault() wrappers to all imports.
That means all the __importDefault() wrappers added in #91954 are now removed
from the generated javascript sources again. (=> less conditions during runtime)

Review

  • Code review CR 0
  • Verified V 0

Issues 1

Files 452

Show 432 more files

Review log 1531

Show 36 earlier messages
  1. Benjamin Franzke uploaded patch set 1 08/21/20, 14:54

  2. TYPO3com voted on patch set 1 V -1 08/21/20, 15:10 Completed build in 16m 16s on Fri, 21 Aug, 03:10 PM Test Summary: 72210 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-4809

  3. TYPO3com commented on patch set 1 08/21/20, 17:37 Completed build in 20s on Fri, 21 Aug, 05:37 PM Test Summary: No tests found Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-4824

  4. Benjamin Franzke uploaded patch set 2 08/21/20, 22:21

  5. TYPO3com voted on patch set 2 V -1 08/21/20, 22:33 Completed build in 12m 06s on Fri, 21 Aug, 10:33 PM Test Summary: 76 of 73481 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-4829

  6. Benjamin Franzke uploaded patch set 3 08/21/20, 22:46

  1. TYPO3com voted on patch set 3 V -1 08/21/20, 22:57 Completed build in 10m 49s on Fri, 21 Aug, 10:57 PM Test Summary: 72996 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-4830

  2. Benjamin Franzke uploaded patch set 4 08/21/20, 23:21

  3. TYPO3com voted on patch set 4 V -1 08/21/20, 23:34 Completed build in 12m 50s on Fri, 21 Aug, 11:34 PM Test Summary: 1 of 73481 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-4831

  4. TYPO3com voted on patch set 4 V +1 08/21/20, 23:55 Completed build in 11m 56s on Fri, 21 Aug, 11:55 PM Test Summary: 72996 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-4832

  5. Benjamin Franzke uploaded patch set 5 08/24/20, 14:19

  6. TYPO3com voted on patch set 5 V +1 08/24/20, 16:09 Completed build in 11m 16s on Mon, 24 Aug, 04:09 PM Test Summary: 73074 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-4909

  7. Benjamin Franzke uploaded patch set 6 09/01/20, 08:18

  8. TYPO3com voted on patch set 6 V +1 09/01/20, 08:30 Completed build in 11m 46s on Tue, 1 Sep, 08:30 AM Test Summary: 73164 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-5094

  9. Benjamin Franzke uploaded patch set 7 09/01/20, 15:13

  10. TYPO3com voted on patch set 7 V +1 09/01/20, 15:24 Completed build in 10m 55s on Tue, 1 Sep, 03:24 PM Test Summary: 73164 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-5112

  11. Benjamin Franzke uploaded patch set 8 11/12/20, 10:42

  12. TYPO3com voted on patch set 8 V +1 11/12/20, 11:00 Completed build in 14m 24s on Thu, 12 Nov, 11:00 AM Test Summary: 73623 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7268

  13. Benjamin Franzke uploaded patch set 9 12/07/20, 20:01

  14. TYPO3com voted on patch set 9 V +1 12/07/20, 20:11 Completed build in 10m 00s on Mon, 7 Dec, 08:11 PM Test Summary: 32411 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7941

  15. Benjamin Franzke uploaded patch set 10 12/28/20, 23:59

  16. TYPO3com voted on patch set 10 V -1 12/29/20, 00:08 Completed build in 08m 57s on Tue, 29 Dec, 12:08 AM Test Summary: 54 of 32781 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-8485

  17. Benjamin Franzke uploaded patch set 11 12/29/20, 08:28

  18. TYPO3com voted on patch set 11 V +1 12/29/20, 08:38 Completed build in 10m 35s on Tue, 29 Dec, 08:38 AM Test Summary: 32540 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-8486

  19. Benjamin Franzke uploaded patch set 12 01/05/21, 17:29

  20. TYPO3com voted on patch set 12 V -1 01/05/21, 17:46 Completed build in 16m 19s on Tue, 5 Jan, 05:46 PM Test Summary: 30505 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-8637

  21. TYPO3com commented on patch set 12 01/05/21, 18:55 Completed build in 13m 21s on Tue, 5 Jan, 06:55 PM Test Summary: 1 of 30087 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-8644

  22. TYPO3com voted on patch set 12 V +1 01/05/21, 19:13 Completed build in 09m 42s on Tue, 5 Jan, 07:13 PM Test Summary: 32545 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-8648

  23. Benni Mack wrote 11/29/21, 10:03

    Change destination moved from master to main
  24. Benni Mack updated the commit message in patch set 13 11/29/21, 13:14

  25. Benjamin Franzke abandoned the change 01/14/22, 19:29

    Abandon in favor of emitting ES6 directly: https://review.typo3.org/c/Packages/TYPO3.CMS/+/72637