|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * This is an interop file allowing for `@babel/core` to be imported in both CommonJS or |
| 11 | + * ES module files. The `@babel/core` package needs some special treatment because: |
| 12 | + * |
| 13 | + * Using a default import does not with CommonJS because the `@babel/core` package does not |
| 14 | + * expose a `default` export at runtime (because it sets the `_esModule` property that causes |
| 15 | + * TS to not create the necessary interop `default` export). On the other side, when loaded |
| 16 | + * as part of an ESM, NodeJS will make all of the exports available as default export. |
| 17 | + * |
| 18 | + * Using named import bindings (i.e. namespace import or actual named bindings) is not |
| 19 | + * working well for ESM because as said before, NodeJS will make all of the exports available |
| 20 | + * as the `default` export. Hence ESM that imports CJS, always should use the default import. |
| 21 | + * |
| 22 | + * There is no solution that would work for both CJS and ESM, so we need to use a custom interop |
| 23 | + * that switches between the named exports or the default exports depending on what is available. |
| 24 | + * This allows the code to run in both ESM (for production) and CJS (for development). |
| 25 | + * |
| 26 | + * TODO(devversion): remove this once devmode uses ESM as well. |
| 27 | + */ |
| 28 | + |
| 29 | +// tslint:disable-next-line |
| 30 | +import * as _babelNamespace from '@babel/core'; |
| 31 | +// tslint:disable-next-line |
| 32 | +import _babelDefault from '@babel/core'; |
| 33 | + |
| 34 | +const babel: typeof _babelNamespace = _babelDefault ?? _babelNamespace; |
| 35 | + |
| 36 | +// We create an alias of the `types` namespace so that we can re-export the |
| 37 | +// types namespace. Preserving the namespace is important so that types |
| 38 | +// can still be referenced using a qualified name. |
| 39 | +import _typesNamespace = _babelNamespace.types; |
| 40 | + |
| 41 | +// If the default export is available, we use its `types` runtime value |
| 42 | +// for the type namespace we re-export. This is a trick we use to preserve |
| 43 | +// the namespace types, while changing the runtime value of the namespace. |
| 44 | +// TS complains about us assigning to a namespace but this is legal at runtime. |
| 45 | +if (_babelDefault !== undefined) { |
| 46 | + // @ts-ignore |
| 47 | + _typesNamespace = _babelDefault.types; |
| 48 | +} |
| 49 | + |
| 50 | +export import types = _typesNamespace; |
| 51 | +export type PluginObj = _babelNamespace.PluginObj; |
| 52 | +export type ConfigAPI = _babelNamespace.ConfigAPI; |
| 53 | +export type NodePath<T = _babelNamespace.Node> = _babelNamespace.NodePath<T>; |
| 54 | + |
| 55 | +export const NodePath: typeof _babelNamespace.NodePath = babel.NodePath; |
| 56 | +export const transformSync: typeof _babelNamespace.transformSync = babel.transformSync; |
| 57 | +export const parse: typeof _babelNamespace.parse = babel.parse; |
0 commit comments