Merge pull request #1790 from alixander/amend

amend expose imports
This commit is contained in:
Alexander Wang 2023-12-24 22:41:19 -08:00 committed by GitHub
commit 9f12d50e67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View file

@ -27,7 +27,8 @@ type globContext struct {
type compiler struct { type compiler struct {
err *d2parser.ParseError err *d2parser.ParseError
fs fs.FS fs fs.FS
imports []string
// importStack is used to detect cyclic imports. // importStack is used to detect cyclic imports.
importStack []string importStack []string
// importCache enables reuse of files imported multiple times. // importCache enables reuse of files imported multiple times.
@ -80,7 +81,7 @@ func Compile(ast *d2ast.Map, opts *CompileOptions) (*Map, []string, error) {
if !c.err.Empty() { if !c.err.Empty() {
return nil, nil, c.err return nil, nil, c.err
} }
return m, c.importStack, nil return m, c.imports, nil
} }
func (c *compiler) overlayClasses(m *Map) { func (c *compiler) overlayClasses(m *Map) {

View file

@ -80,6 +80,13 @@ func (c *compiler) __import(imp *d2ast.Import) (*Map, bool) {
} }
defer c.popImportStack() defer c.popImportStack()
// Only get immediate imports.
if len(c.importStack) == 2 {
if _, ok := c.importCache[impPath]; !ok {
c.imports = append(c.imports, imp.PathWithPre())
}
}
ir, ok := c.importCache[impPath] ir, ok := c.importCache[impPath]
if ok { if ok {
return ir, true return ir, true