Skip to content

Commit

Permalink
Fix import {default as LocalName} in Metro
Browse files Browse the repository at this point in the history
Reviewed By: rubennorte

Differential Revision: D15621553

fbshipit-source-id: 124d22fab3624ad47fa5f02fb9b6a3f10415e610
  • Loading branch information
cpojer authored and facebook-github-bot committed Jun 5, 2019
1 parent 2e6652d commit 2f15a05
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,19 @@ it('enables module exporting when something is exported', () => {

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

it('supports `import {default as LocalName}`', () => {
const code = `
import {
Platform,
default as ReactNative,
} from 'react-native';
`;

const expected = `
var Platform = require('react-native').Platform;
var ReactNative = _$$_IMPORT_DEFAULT('react-native');
`;

compare([importExportPlugin], code, expected, opts);
});
24 changes: 17 additions & 7 deletions packages/metro/src/JSTransformer/worker/import-export-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,23 @@ function importExportPlugin({types: t}: $FlowFixMe) {
break;
case 'ImportSpecifier':
anchor.insertBefore(
importNamedTemplate({
FILE: resolvePath(file, state.opts.resolve),
LOCAL: local,
REMOTE: imported,
}),
);
if (imported.name === 'default') {
anchor.insertBefore(
importTemplate({
IMPORT: state.importDefault,
FILE: resolvePath(file, state.opts.resolve),
LOCAL: local,
}),
);
} else {
anchor.insertBefore(
importNamedTemplate({
FILE: resolvePath(file, state.opts.resolve),
LOCAL: local,
REMOTE: imported,
}),
);
}
break;

default:
Expand Down

0 comments on commit 2f15a05

Please sign in to comment.