Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,27 @@ test('exports members of another module directly from an import (as all)', () =>
`);
});

test('exports members of another module directly from an import (as namespace)', () => {
const code = `
export * as AppleIcons from 'apple-icons';
`;

const expected = `
Object.defineProperty(exports, '__esModule', {value: true});

var _AppleIcons = _$$_IMPORT_ALL('apple-icons');
exports.AppleIcons = _AppleIcons;
`;

compare([importExportPlugin], code, expected, opts);

expect(showTransformedDeps(code)).toMatchInlineSnapshot(`
"
> 2 | export * as AppleIcons from 'apple-icons';
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dep #0 (apple-icons)"
`);
});

test('enables module exporting when something is exported', () => {
const code = `
foo();
Expand Down
30 changes: 28 additions & 2 deletions packages/metro-transform-plugins/src/import-export-plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
* Portions Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -9,6 +9,8 @@
* @oncall react_native
*/

// Portions Copyright (c) 2015-present 650 Industries, Inc. (aka Expo), under MIT.

import type {PluginObj} from '@babel/core';
import type {NodePath} from '@babel/traverse';
import type {
Expand Down Expand Up @@ -274,7 +276,6 @@ export default function importExportPlugin({
const specifiers = path.node.specifiers;
if (specifiers) {
specifiers.forEach(s => {
const local = s.local;
const remote = s.exported;

if (remote.type === 'StringLiteral') {
Expand All @@ -284,6 +285,31 @@ export default function importExportPlugin({
);
}

if (s.type === 'ExportNamespaceSpecifier') {
const source = nullthrows(path.node.source);
const temp = path.scope.generateUidIdentifier(remote.name);

path.insertBefore(
withLocation(
importTemplate({
IMPORT: t.cloneNode(state.importAll),
FILE: resolvePath(t.cloneNode(source), state.opts.resolve),
LOCAL: temp,
}),
loc,
),
);

state.exportNamed.push({
local: temp.name,
remote: remote.name,
loc,
});
return;
}

const local = s.local;

if (path.node.source) {
// $FlowFixMe[incompatible-use]
const temp = path.scope.generateUidIdentifier(local.name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
* Portions Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noformat
* @oncall react_native
* @generated SignedSource<<abccab72cbf3143d690c0cea72fcede8>>
* @generated SignedSource<<587cbe7792cfd21ce4abb4bfb5bac2f8>>
*
* This file was translated from Flow by scripts/generateTypeScriptDefinitions.js
* Original file: packages/metro-transform-plugins/src/import-export-plugin.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Object {
"myDefault": "export-1: DEFAULT",
"myFoo": "export-1: FOO",
"myFunction": "export-1: MY_FUNCTION",
"namespaceReExportDefault": "export-2: DEFAULT",
"namespaceReExportFoo": "export-2: FOO",
"primitiveDefault": "export-primitive-default: DEFAULT",
"primitiveFoo": "export-primitive-default: FOO",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ test('builds a simple bundle', async () => {
const object = execBundle(result.code);
const cjs = await object.asyncImportCJS;

expect(object.extraData.namespaceReExportDefault).toBe('export-2: DEFAULT');
expect(object.extraData.namespaceReExportFoo).toBe('export-2: FOO');
expect(object).toMatchSnapshot();
expect(cjs).toEqual(expect.objectContaining(cjs.default));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict
*/

'use strict';

export * as namespaceReExport from './export-2';
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {RequireWithUnstableImportMaybeSync} from './utils';

import {default as myDefault, foo as myFoo, myFunction} from './export-1';
import * as importStar from './export-2';
import {namespaceReExport} from './export-namespace';
import {foo} from './export-null';
import primitiveDefault, {
foo as primitiveFoo,
Expand All @@ -30,6 +31,8 @@ export const extraData = {
myDefault,
myFoo,
myFunction: myFunction() as string,
namespaceReExportDefault: namespaceReExport.default,
namespaceReExportFoo: namespaceReExport.foo,
primitiveDefault,
primitiveFoo,
};
Expand Down
Loading