Skip to content

Commit

Permalink
Deduplicate imports referencing default exports and their original va…
Browse files Browse the repository at this point in the history
…riables (#2719)

* Deduplicate imports referencing default exports and their original variables

* Update changelog
  • Loading branch information
lukastaegert committed Feb 26, 2019
1 parent 5d6e01c commit 297cc8c
Show file tree
Hide file tree
Showing 20 changed files with 133 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,14 @@
# rollup changelog

## 1.2.4
*2019-02-25*

### Bug Fixes
* Fix an issue where a variable was imported twice under the same name (#2715)

### Pull Requests
* [#2715](https://github.com/rollup/rollup/pull/2715): Deduplicate imports referencing default exports and their original variables (@lukastaegert)

## 1.2.3
*2019-02-23*

Expand Down
31 changes: 19 additions & 12 deletions src/Chunk.ts
Expand Up @@ -2,7 +2,9 @@ import sha256 from 'hash.js/lib/hash/sha/256';
import MagicString, { Bundle as MagicStringBundle, SourceMap } from 'magic-string';
import * as NodeType from './ast/nodes/NodeType';
import { UNDEFINED_EXPRESSION } from './ast/values';
import { isExportDefaultVariable } from './ast/variables/ExportDefaultVariable';
import ExportDefaultVariable, {
isExportDefaultVariable
} from './ast/variables/ExportDefaultVariable';
import ExportShimVariable from './ast/variables/ExportShimVariable';
import GlobalVariable from './ast/variables/GlobalVariable';
import LocalVariable from './ast/variables/LocalVariable';
Expand Down Expand Up @@ -478,17 +480,22 @@ export default class Chunk {
exportDeclaration.push({ imported: importName, reexported: exportName });
}

const importsAsArray = Array.from(this.imports);
const dependencies: ChunkDependencies = [];

this.dependencies.forEach(dep => {
const importsFromDependency = Array.from(this.imports).filter(variable =>
variable.module instanceof Module ? variable.module.chunk === dep : variable.module === dep
);

let imports: ImportSpecifier[];
if (importsFromDependency.length) {
imports = [];
for (const variable of importsFromDependency) {
for (const dep of this.dependencies) {
const imports: ImportSpecifier[] = [];
for (const variable of importsAsArray) {
if (
(variable.module instanceof Module
? variable.module.chunk === dep
: variable.module === dep) &&
!(
variable instanceof ExportDefaultVariable &&
variable.referencesOriginal() &&
this.imports.has(variable.getOriginalVariable())
)
) {
const local = variable.getName();
const imported =
variable.module instanceof ExternalModule
Expand Down Expand Up @@ -534,9 +541,9 @@ export default class Chunk {
exportsNames,
exportsDefault,
reexports,
imports
imports: imports.length > 0 ? imports : null
});
});
}

return dependencies;
}
Expand Down
4 changes: 4 additions & 0 deletions src/ast/variables/ExportDefaultVariable.ts
Expand Up @@ -47,6 +47,10 @@ export default class ExportDefaultVariable extends LocalVariable {
return this.referencesOriginal() ? this.originalId.variable.getName() : super.getName();
}

getOriginalVariable(): Variable | null {
return (this.originalId && this.originalId.variable) || null;
}

getOriginalVariableName(): string | null {
return (this.originalId && this.originalId.name) || null;
}
Expand Down
@@ -0,0 +1,7 @@
module.exports = {
description:
'do not import variables that reference an original if the original is already imported',
options: {
input: ['main1', 'main2']
}
};
@@ -0,0 +1,8 @@
define(['exports'], function (exports) { 'use strict';

const foo = {};

exports.foo = foo;
exports.bar = foo;

});
@@ -0,0 +1,5 @@
define(['./generated-chunk.js'], function (__chunk_1) { 'use strict';

console.log(__chunk_1.bar, __chunk_1.bar);

});
@@ -0,0 +1,5 @@
define(['./generated-chunk.js'], function (__chunk_1) { 'use strict';

console.log(__chunk_1.bar, __chunk_1.bar);

});
@@ -0,0 +1,6 @@
'use strict';

const foo = {};

exports.foo = foo;
exports.bar = foo;
@@ -0,0 +1,5 @@
'use strict';

var __chunk_1 = require('./generated-chunk.js');

console.log(__chunk_1.bar, __chunk_1.bar);
@@ -0,0 +1,5 @@
'use strict';

var __chunk_1 = require('./generated-chunk.js');

console.log(__chunk_1.bar, __chunk_1.bar);
@@ -0,0 +1,3 @@
const foo = {};

export { foo as a, foo as b };
@@ -0,0 +1,3 @@
import { a as bar } from './generated-chunk.js';

console.log(bar, bar);
@@ -0,0 +1,3 @@
import { a as bar } from './generated-chunk.js';

console.log(bar, bar);
@@ -0,0 +1,12 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

const foo = exports('a', {});

exports('b', foo);

}
};
});
@@ -0,0 +1,14 @@
System.register(['./generated-chunk.js'], function (exports, module) {
'use strict';
var bar;
return {
setters: [function (module) {
bar = module.a;
}],
execute: function () {

console.log(bar, bar);

}
};
});
@@ -0,0 +1,14 @@
System.register(['./generated-chunk.js'], function (exports, module) {
'use strict';
var bar;
return {
setters: [function (module) {
bar = module.a;
}],
execute: function () {

console.log(bar, bar);

}
};
});
@@ -0,0 +1 @@
export const foo = {};
@@ -0,0 +1,4 @@
import { foo } from './foo';
import bar from './proxy';

console.log(foo, bar);
@@ -0,0 +1,4 @@
import { foo } from './foo';
import bar from './proxy';

console.log(foo, bar);
@@ -0,0 +1,2 @@
import { foo } from './foo';
export default foo;

0 comments on commit 297cc8c

Please sign in to comment.