Skip to content

Commit

Permalink
Improve handling of conflicting namespace exports (#2893)
Browse files Browse the repository at this point in the history
* test: "export *" before named export

* Prioritize local exports over external exports when resolving export
all modules
  • Loading branch information
aleclarson authored and lukastaegert committed Jun 16, 2019
1 parent 7d1bd82 commit f4b19ab
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Module.ts
Expand Up @@ -429,8 +429,7 @@ export default class Module {
}

if (name !== 'default') {
for (let i = 0; i < this.exportAllModules.length; i += 1) {
const module = this.exportAllModules[i];
for (const module of this.exportAllModules) {
const declaration = module.getVariableForExportName(name, true);

if (declaration) return declaration;
Expand Down Expand Up @@ -504,10 +503,16 @@ export default class Module {
this.addModulesToSpecifiers(this.importDescriptions);
this.addModulesToSpecifiers(this.reexports);

this.exportAllModules = this.exportAllSources.map(source => {
const id = this.resolvedIds[source].id;
return this.graph.moduleById.get(id) as any;
});
this.exportAllModules = this.exportAllSources
.map(source => {
const id = this.resolvedIds[source].id;
return this.graph.moduleById.get(id) as Module | ExternalModule;
})
.sort((moduleA, moduleB) => {
const aExternal = moduleA instanceof ExternalModule;
const bExternal = moduleB instanceof ExternalModule;
return aExternal === bExternal ? 0 : aExternal ? 1 : -1;
});
}

render(options: RenderOptions): MagicString {
Expand Down
7 changes: 7 additions & 0 deletions test/form/samples/export-all-before-named/_config.js
@@ -0,0 +1,7 @@
module.exports = {
description: 'external `export *` must not interfere with internal exports',
options: {
output: { name: 'exposedInternals' },
external: ['external']
}
};
19 changes: 19 additions & 0 deletions test/form/samples/export-all-before-named/_expected/amd.js
@@ -0,0 +1,19 @@
define(['exports', 'external'], function (exports, external) { 'use strict';

function internalFn(path) {
return path[0] === '.';
}

Object.keys(external).forEach(function (key) {
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return external[key];
}
});
});
exports.internalFn = internalFn;

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

});
19 changes: 19 additions & 0 deletions test/form/samples/export-all-before-named/_expected/cjs.js
@@ -0,0 +1,19 @@
'use strict';

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

var external = require('external');

function internalFn(path) {
return path[0] === '.';
}

Object.keys(external).forEach(function (key) {
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return external[key];
}
});
});
exports.internalFn = internalFn;
7 changes: 7 additions & 0 deletions test/form/samples/export-all-before-named/_expected/es.js
@@ -0,0 +1,7 @@
export * from 'external';

function internalFn(path) {
return path[0] === '.';
}

export { internalFn };
20 changes: 20 additions & 0 deletions test/form/samples/export-all-before-named/_expected/iife.js
@@ -0,0 +1,20 @@
var exposedInternals = (function (exports, external) {
'use strict';

function internalFn(path) {
return path[0] === '.';
}

Object.keys(external).forEach(function (key) {
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return external[key];
}
});
});
exports.internalFn = internalFn;

return exports;

}({}, external));
22 changes: 22 additions & 0 deletions test/form/samples/export-all-before-named/_expected/system.js
@@ -0,0 +1,22 @@
System.register('exposedInternals', ['external'], function (exports) {
'use strict';
var _starExcludes = { internalFn: 1, default: 1 };
return {
setters: [function (module) {
var _setter = {};
for (var _$p in module) {
if (!_starExcludes[_$p]) _setter[_$p] = module[_$p];
}
exports(_setter);
}],
execute: function () {

exports('internalFn', internalFn);

function internalFn(path) {
return path[0] === '.';
}

}
};
});
23 changes: 23 additions & 0 deletions test/form/samples/export-all-before-named/_expected/umd.js
@@ -0,0 +1,23 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('external')) :
typeof define === 'function' && define.amd ? define(['exports', 'external'], factory) :
(global = global || self, factory(global.exposedInternals = {}, global.external));
}(this, function (exports, external) { 'use strict';

function internalFn(path) {
return path[0] === '.';
}

Object.keys(external).forEach(function (key) {
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return external[key];
}
});
});
exports.internalFn = internalFn;

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

}));
3 changes: 3 additions & 0 deletions test/form/samples/export-all-before-named/internal.js
@@ -0,0 +1,3 @@
export function internalFn(path) {
return path[0] === '.';
}
2 changes: 2 additions & 0 deletions test/form/samples/export-all-before-named/main.js
@@ -0,0 +1,2 @@
export * from 'external';
export * from './internal.js';

0 comments on commit f4b19ab

Please sign in to comment.