From 86f5fc787bc58c09c2dc31e7c33f9145d7d62e62 Mon Sep 17 00:00:00 2001 From: Miguel Castillo Date: Sun, 14 Oct 2018 13:29:48 -0400 Subject: [PATCH] cleaned up bundle named exports to make it more clear where we set the module ids These changes to make it more clear that we are setting the module id when we are exporting modules from a bundle by name. --- src/bundler/chunkedBundle.js | 16 ++++++++++++---- src/bundler/exportNames.js | 24 ------------------------ src/bundler/namedExports.js | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 28 deletions(-) delete mode 100644 src/bundler/exportNames.js create mode 100644 src/bundler/namedExports.js diff --git a/src/bundler/chunkedBundle.js b/src/bundler/chunkedBundle.js index c232ff1..e479f9b 100644 --- a/src/bundler/chunkedBundle.js +++ b/src/bundler/chunkedBundle.js @@ -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 @@ -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; @@ -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); diff --git a/src/bundler/exportNames.js b/src/bundler/exportNames.js deleted file mode 100644 index a56f1ed..0000000 --- a/src/bundler/exportNames.js +++ /dev/null @@ -1,24 +0,0 @@ -function configureExportName(bundler, exportNames) { - if (!exportNames) { - return () => {}; - } - - var exportedNames = ( - exportNames === true ? true : - Array.isArray(exportNames) ? exportNames.reduce((acc, item) => (acc[item] = item, acc), {}) : - exportNames - ); - - var processed = {}; - - return function(mod) { - var name = exportedNames === true && /^\w/.test(mod.name) ? mod.name : exportedNames[mod.name]; - - if (name && !processed[mod.id]) { - processed[mod.id] = true; - bundler.setId(mod.id, JSON.stringify(name)); - } - }; -} - -module.exports = configureExportName; diff --git a/src/bundler/namedExports.js b/src/bundler/namedExports.js new file mode 100644 index 0000000..739f831 --- /dev/null +++ b/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;