use fixedExportMap
MODIFY
sketch-out-module.js
sketch-out-module.js
@@ -91,7 +91,7 @@ export {g as h} from 'foo';
// [fixedExportName*]
// exportNames of variables that are only initialized and used, but
// never assigned to. The exportName 'default' has no localName.
- fixedExports: ['co', 'default', 'xx'],
+ fixedExportMap: { co: ['co'], default: [], xx: ['xx'] },
functorSource: `(${
function($h_imports, $h_once, $h_live) {

MODIFY
src/babelPlugin.js
src/babelPlugin.js
@@ -3,7 +3,7 @@ import * as h from './hidden';
export default options =>
function rewriteModules({ types: t }) {
const {
- fixedExports,
+ fixedExportMap,
imports,
importDecls,
importSources,
@@ -41,9 +41,12 @@ export default options =>
const moduleVisitor = {
ImportDeclaration(path) {
const specs = path.node.specifiers;
- const myImportSources = [];
const specifier = path.node.source.value;
+ let myImportSources = importSources[specifier];
+ if (!myImportSources) {
+ myImportSources = [];
importSources[specifier] = myImportSources;
+ }
let myImports = imports[specifier];
if (!myImports) {
myImports = [];
@@ -94,7 +97,7 @@ export default options =>
},
ExportDefaultDeclaration(path) {
// export default FOO -> $h_once.default(FOO)
- fixedExports.push('default');
+ fixedExportMap.default = [];
const callee = t.memberExpression(
hiddenIdentifier(h.HIDDEN_ONCE),
t.identifier('default'),
@@ -102,8 +105,7 @@ export default options =>
path.replaceWith(t.callExpression(callee, [path.node.declaration]));
},
ExportNamedDeclaration(path) {
- const decl = path.node.declaration;
- const specs = path.node.specifiers;
+ const { declaration: decl, specifiers: specs, source } = path.node;
const replace = [];
if (decl) {
// export const pattern = ...;
@@ -153,7 +155,7 @@ export default options =>
vnames.forEach(vname => (liveExportMap[vname] = [vname]));
} else {
// Fixed exports (i.e. const or non-mutated)
- fixedExports.push(...vnames);
+ vnames.forEach(vname => (fixedExportMap[vname] = [vname]));
}
// Create the export calls.
@@ -233,8 +235,8 @@ export default options =>
}
}
replace.push(
- ...specs.map(spec => {
- const { local, exported, source } = spec;
+ ...specs.reduce((prior, spec) => {
+ const { local, exported } = spec;
// If local.name is reexported we omit it.
const myUpdaterSources = updaterSources[local.name];
if (myUpdaterSources) {
@@ -242,16 +244,22 @@ export default options =>
// name, so update it with this export.
myUpdaterSources.push(`${h.HIDDEN_LIVE}.${local.name}`);
}
+
// If it was imported directly (i.e. has a source or updaters)
// then don't put the local name in the liveExportMap.
liveExportMap[exported.name] =
source || myUpdaterSources ? [] : [local.name];
+ if (!source && !myUpdaterSources) {
const callee = t.memberExpression(
hiddenIdentifier(h.HIDDEN_LIVE),
- exported,
+ local,
);
- return t.expressionStatement(t.callExpression(callee, []));
- }),
+ prior.push(
+ t.expressionStatement(t.callExpression(callee, [local])),
+ );
+ }
+ return prior;
+ }, []),
// and don't evaluate to anything.
t.expressionStatement(t.identifier('undefined')),
);

MODIFY
src/index.js
src/index.js
import * as h from './hidden';
import makeModulePlugin from './babelPlugin';
-const makeModuleTransformer = (babelCore, makeImporter) => {
+const makeModuleTransformer = (babelCore, importer) => {
function transformSource(source, sourceOptions = {}) {
// Transform the script/expression source for import expressions.
const parserPlugins = ['dynamicImport'];
@@ -30,8 +30,8 @@ const makeModuleTransformer = (babelCore, makeImporter) => {
const sourceOptions = {
sourceType: 'module',
// exportNames of variables that are only initialized and used, but
- // never assigned to. The exportName 'default' has no localName.
- fixedExports: [],
+ // never assigned to.
+ fixedExportMap: {},
// Record of imported module specifier names to list of importNames.
// The importName '*' is that module's module namespace object.
imports: {},
@@ -84,7 +84,7 @@ const makeModuleTransformer = (babelCore, makeImporter) => {
moduleSource,
imports: sourceOptions.imports,
liveExportMap: sourceOptions.liveExportMap,
- fixedExports: sourceOptions.fixedExports,
+ fixedExportMap: sourceOptions.fixedExportMap,
functorSource,
};
return moduleStaticRecord;
@@ -97,7 +97,7 @@ const makeModuleTransformer = (babelCore, makeImporter) => {
// Make an importer that uses our transform for its submodules.
function curryImporter(srcSpec) {
- return makeImporter(srcSpec, endowments);
+ return importer(srcSpec, endowments);
}
// Create an import expression for the given URL.
@@ -116,16 +116,16 @@ const makeModuleTransformer = (babelCore, makeImporter) => {
if (ss.sourceType === 'module') {
// Do the rewrite of our own sources.
- const linkageRecord = createStaticRecord(source);
+ const staticRecord = createStaticRecord(source);
Object.assign(endowments, {
// Import our own source directly, returning a promise.
- [h.HIDDEN_IMPORT_SELF]: curryImporter({ url, linkageRecord }),
+ [h.HIDDEN_IMPORT_SELF]: curryImporter({ url, staticRecord }),
});
return {
...ss,
endowments,
allowHidden: true,
- linkageRecord,
+ staticRecord,
sourceType: 'script',
src: `${h.HIDDEN_IMPORT_SELF}();`,
};

use fixedExportMap