Skip to content

Commit

Permalink
fix #251: code splitting bug with re-export cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 12, 2020
1 parent 1366de3 commit 6d4cae0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 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 code splitting bug with re-export cycles ([#251](https://github.com/evanw/esbuild/issues/251))

Two files that both re-export each other could cause invalid code to be generated when code splitting is enabled. The specific failure was an `export` statement without a matching `import` statement from the shared code chunk. This bug has been fixed.

## 0.6.1

* Allow bundling with stdin as input ([#212](https://github.com/evanw/esbuild/issues/212))
Expand Down
57 changes: 57 additions & 0 deletions internal/bundler/bundler_splitting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,60 @@ export {
},
})
}

func TestSplittingCircularReferenceIssue251(t *testing.T) {
expectBundled(t, bundled{
files: map[string]string{
"/a.js": `
export * from './b.js';
export var p = 5;
`,
"/b.js": `
export * from './a.js';
export var q = 6;
`,
},
entryPaths: []string{"/a.js", "/b.js"},
options: config.Options{
IsBundling: true,
CodeSplitting: true,
OutputFormat: config.FormatESModule,
AbsOutputDir: "/out",
},
expected: map[string]string{
"/out/a.js": `import {
p,
q
} from "./chunk.xL6KqlYO.js";
// /a.js
export {
p,
q
};
`,
"/out/b.js": `import {
p,
q
} from "./chunk.xL6KqlYO.js";
// /b.js
export {
p,
q
};
`,
"/out/chunk.xL6KqlYO.js": `// /b.js
var q = 6;
// /a.js
var p = 5;
export {
p,
q
};
`,
},
})
}
8 changes: 8 additions & 0 deletions internal/bundler/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,7 @@ func (c *linkerContext) addExportsForExportStar(
}
visited[sourceIndex] = true
file := &c.files[sourceIndex]
fileMeta := &c.fileMeta[sourceIndex]

for _, importRecordIndex := range file.ast.ExportStarImportRecords {
record := &file.ast.ImportRecords[importRecordIndex]
Expand Down Expand Up @@ -1623,6 +1624,13 @@ func (c *linkerContext) addExportsForExportStar(
pathLoc: &pathLoc,
isFromExportStar: true,
}

// Make sure the symbol is marked as imported so that code splitting
// imports it correctly if it ends up being shared with another chunk
fileMeta.importsToBind[ref] = importToBind{
ref: ref,
sourceIndex: otherSourceIndex,
}
} else if existing.sourceIndex != otherSourceIndex {
// Two different re-exports colliding makes it ambiguous
existing.isAmbiguous = true
Expand Down

0 comments on commit 6d4cae0

Please sign in to comment.