Skip to content

Commit

Permalink
fix #190: ensure re-exported default aliases aren't renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jun 20, 2020
1 parent 0be23de commit 21244e7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

* Fix re-export statements ([#190](https://github.com/evanw/esbuild/issues/190))

The previous release caused a regression due to some behind-the-scenes work for the upcoming code splitting feature. The re-export alias in statements of the form `export { foo as bar } from 'path'` could sometimes incorrectly be renamed to something else, such as `foo` becoming `foo2`. This release fixes the bug.

## 0.5.5

* Implement logical assignment operator transforms
Expand Down
63 changes: 63 additions & 0 deletions internal/bundler/bundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5310,3 +5310,66 @@ export {
},
})
}

func TestReExportDefault(t *testing.T) {
expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
export {default as foo} from './foo'
export {default as bar} from './bar'
`,
"/foo.js": `
export default 'foo'
`,
"/bar.js": `
export default 'bar'
`,
},
entryPaths: []string{"/entry.js"},
parseOptions: parser.ParseOptions{
IsBundling: true,
},
bundleOptions: BundleOptions{
IsBundling: true,
AbsOutputFile: "/out.js",
},
expected: map[string]string{
"/out.js": `// /foo.js
const foo_default = "foo";
// /bar.js
const bar_default = "bar";
// /entry.js
export {
bar_default as bar,
foo_default as foo
};
`,
},
})
}

func TestReExportDefaultNoBundle(t *testing.T) {
expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
export {default as foo} from './foo'
export {default as bar} from './bar'
`,
},
entryPaths: []string{"/entry.js"},
parseOptions: parser.ParseOptions{
IsBundling: false,
},
bundleOptions: BundleOptions{
IsBundling: false,
AbsOutputFile: "/out.js",
},
expected: map[string]string{
"/out.js": `export {default as foo} from "./foo";
export {default as bar} from "./bar";
`,
},
})
}
9 changes: 6 additions & 3 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6965,7 +6965,7 @@ func (p *parser) visitAndAppendStmt(stmts []ast.Stmt, stmt ast.Stmt) []ast.Stmt
}

case *ast.SExportClause:
// "export {foo}
// "export {foo}"
for i, item := range s.Items {
name := p.loadNameFromRef(item.Name.Ref)
ref := p.findSymbol(name).ref
Expand All @@ -6980,10 +6980,13 @@ func (p *parser) visitAndAppendStmt(stmts []ast.Stmt, stmt ast.Stmt) []ast.Stmt
p.currentScope.Generated = append(p.currentScope.Generated, s.NamespaceRef)
p.recordDeclaredSymbol(s.NamespaceRef)

// This is a re-export and the names are symbols in another file
// This is a re-export and the symbols created here are used to reference
// names in another file. This means the symbols are really aliases. The
// symbols are marked as "unbound" so that they aren't accidentally renamed
// by the code that avoids symbol name collisions.
for i, item := range s.Items {
name := p.loadNameFromRef(item.Name.Ref)
ref := p.newSymbol(ast.SymbolOther, name)
ref := p.newSymbol(ast.SymbolUnbound, name)
p.currentScope.Generated = append(p.currentScope.Generated, ref)
p.recordDeclaredSymbol(ref)
s.Items[i].Name.Ref = ref
Expand Down

0 comments on commit 21244e7

Please sign in to comment.