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

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

  • Verified V 0

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

Issues 1

Files 452

Show 432 more files