Skip to content

Commit

Permalink
Add new flag that indicates if a module has exports (#9171)
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Dec 13, 2018
1 parent b60adce commit f4eec5c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
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) {
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
});

0 comments on commit f4eec5c

Please sign in to comment.