Skip to content

Commit

Permalink
fix(shaker): use the last export statement instead of the 1st (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber committed Jul 24, 2021
1 parent 60f8fc3 commit b79584c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ color.green = '#0f0';
exports.__linariaPreval = [color];"
`;

exports[`keeps only the last assignment of each exported variable 1`] = `
"\\"use strict\\";
var bar = function bar() {
return 'hello world';
};
exports.bar = bar;
var foo = exports.bar();
exports.__linariaPreval = [foo];"
`;

exports[`keeps reused exports 1`] = `
"\\"use strict\\";
Expand Down
16 changes: 16 additions & 0 deletions packages/babel/__tests__/evaluators/shaker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,19 @@ it('keeps reused exports', () => {

expect(shaken).toMatchSnapshot();
});

it('keeps only the last assignment of each exported variable', () => {
const [shaken] = _shake()`
const bar = function() {
return 'hello world';
};
exports.bar = "bar";
exports.bar = bar;
const foo = exports.bar();
exports.__linariaPreval = [foo];
`;

expect(shaken).toMatchSnapshot();
});
3 changes: 2 additions & 1 deletion packages/shaker/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ export default class ScopeManager {
const name = getExportName(memberExp);
if (!this.global.has(name)) {
this.global.set(name, new Set());
this.declarations.set(getId(this.global, name), memberExp);
}

// There can be a few `export.foo = …` statements, but we need only the last one
this.declarations.set(getId(this.global, name), memberExp);
this.global.get(name)!.add(memberExp);
return;
}
Expand Down

0 comments on commit b79584c

Please sign in to comment.