Skip to content

Commit

Permalink
Merge pull request #399 from MiguelCastillo/bundle-named-export
Browse files Browse the repository at this point in the history
cleaned up bundle named exports to make it more clear where we set th…
  • Loading branch information
MiguelCastillo committed Oct 14, 2018
2 parents 0b14c78 + 86f5fc7 commit e03a4b2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
16 changes: 12 additions & 4 deletions src/bundler/chunkedBundle.js
Expand Up @@ -3,7 +3,7 @@
const utils = require("belty");
const uniqueId = require("@bit/bundler-utils/uniqueId");
const chunkedBundleBuilder = require("./chunkedBundleBuilder");
const configureExportName = require("./exportNames");
const configureNamedExports = require("./namedExports");

const defaults = {
"umd": false
Expand Down Expand Up @@ -44,12 +44,20 @@ function buildBundle(bundler, bundle, context, options) {
Object.assign({}, bundler._options, options) :
Object.assign({}, utils.omit(bundler._options, ["umd"]), options);

const exportName = configureExportName(bundler, bundle.isMain ? bundler._options.exportNames : options.exportNames);
const getNamedExport = configureNamedExports(bundle.isMain ? bundler._options.exportNames : options.exportNames);

const getId = (mod) => {
return bundler.getId(mod.id);
};

const setId = (mod) => {
const moduleName = getNamedExport(mod);

if (moduleName) {
bundler.setId(mod.id, moduleName);
}
};

const configureDependency = (dependency) => {
const id = getId(dependency);
const name = dependency.name || id;
Expand All @@ -63,12 +71,12 @@ function buildBundle(bundler, bundle, context, options) {
// Map entries first to give them lower IDs than the rest to make
// bundle ID generation more predictable.
var entries = context.getModules(bundle.entries);
entries.forEach(exportName);
entries.forEach(setId);
entries = entries.map(getId);

// Configured exported names for the rest of modules.
var modules = context.getModules(bundle.modules);
modules.forEach(exportName);
modules.forEach(setId);

const moduleMap = modules.reduce((acc, mod) => {
const moduleId = getId(mod);
Expand Down
24 changes: 0 additions & 24 deletions src/bundler/exportNames.js

This file was deleted.

22 changes: 22 additions & 0 deletions src/bundler/namedExports.js
@@ -0,0 +1,22 @@
function configureNamedExports(exportNames) {
if (!exportNames) {
return () => {};
}

var exportedNames = (
exportNames === true ? true :
Array.isArray(exportNames) ?
exportNames.reduce((acc, item) => (acc[item] = item, acc), {}) :
exportNames
);

return function(mod) {
var name = exportedNames === true && /^\w/.test(mod.name) ? mod.name : exportedNames[mod.name];

if (name !== undefined) {
return JSON.stringify(name);
}
};
}

module.exports = configureNamedExports;

0 comments on commit e03a4b2

Please sign in to comment.