Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transforming empty export statement #9171

Merged
merged 1 commit into from Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -8,6 +8,8 @@ export type ModuleMetadata = {
// The name of the variable that will reference an object containing export names.
exportNameListName: null | string,

hasExports: boolean,

// Lookup from local binding to export information.
local: Map<string, LocalExportMetadata>,

Expand Down Expand Up @@ -52,18 +54,7 @@ export type LocalExportMetadata = {
* Check if the module has any exports that need handling.
*/
export function hasExports(metadata: ModuleMetadata) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function still needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but as it is a separate package I cannot remove it without breaking backwards compatibility if a mix between old and new version of Babel packages is used. At first I also wanted to use meta.hasExports directly in the transforms, but I ended up not doing it for the same reason.

const { local, source } = metadata;

return (
local.size > 0 ||
Array.from(source).some(([, meta]) => {
return (
meta.reexports.size > 0 ||
meta.reexportNamespace.size > 0 ||
!!meta.reexportAll
);
})
);
return metadata.hasExports;
}

/**
Expand Down Expand Up @@ -99,7 +90,10 @@ export default function normalizeModuleAndLoadMetadata(

nameAnonymousExports(programPath);

const { local, source } = getModuleMetadata(programPath, { loose, lazy });
const { local, source, hasExports } = getModuleMetadata(programPath, {
loose,
lazy,
});

removeModuleDeclarations(programPath);

Expand Down Expand Up @@ -127,6 +121,7 @@ export default function normalizeModuleAndLoadMetadata(
return {
exportName,
exportNameListName: null,
hasExports,
local,
source,
};
Expand Down Expand Up @@ -171,6 +166,7 @@ function getModuleMetadata(
}
return data;
};
let hasExports = false;
programPath.get("body").forEach(child => {
if (child.isImportDeclaration()) {
const data = getData(child.node.source);
Expand Down Expand Up @@ -219,13 +215,15 @@ function getModuleMetadata(
}
});
} else if (child.isExportAllDeclaration()) {
hasExports = true;
const data = getData(child.node.source);
if (!data.loc) data.loc = child.node.loc;

data.reexportAll = {
loc: child.node.loc,
};
} else if (child.isExportNamedDeclaration() && child.node.source) {
hasExports = true;
const data = getData(child.node.source);
if (!data.loc) data.loc = child.node.loc;

Expand All @@ -242,6 +240,11 @@ function getModuleMetadata(
throw exportName.buildCodeFrameError('Illegal export "__esModule".');
}
});
} else if (
child.isExportNamedDeclaration() ||
child.isExportDefaultDeclaration()
) {
hasExports = true;
}
});

Expand Down Expand Up @@ -295,6 +298,7 @@ function getModuleMetadata(
}

return {
hasExports,
local: localData,
source: sourceData,
};
Expand Down
Expand Up @@ -2,6 +2,8 @@

var _interopRequireDefault3 = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;

var _interopRequireDefault2 = _interopRequireDefault3(require("@babel/runtime/helpers/interopRequireDefault"));

console.log(_interopRequireDefault2.default);
Expand Up @@ -2,6 +2,10 @@

var _interopRequireDefault3 = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
value: true
});

var _interopRequireDefault2 = _interopRequireDefault3(require("@babel/runtime/helpers/interopRequireDefault"));

console.log(_interopRequireDefault2.default);
@@ -0,0 +1 @@
export {};
@@ -0,0 +1,5 @@
{
"plugins": [
"transform-modules-commonjs"
]
}
@@ -0,0 +1,5 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});