본문으로 건너뛰기

Syntax

There are two ways to import. These two examples both have the same result:

abx

Result of running both types of imports below

In the next section, we'll see examples of common import use cases.

Two types of imports

1. Regular import

  • x.d2
  • y.d2

This is the equivalent of giving the entire file of x as a map that a sets as its value.

2. Spread import

  • x.d2
  • y.d2

This tells D2 to take the contents of the file x and insert it into the map.

정보

Spread imports only work within maps. Something like a: ...@x.d2 is an invalid usage.

Omit the extension

Above, we wrote the full file name for clarity, but the correct usage is to just specify the file name without the suffix. If you run D2's autoformatter, it'll change

into

정보

D2 will not open files that don't have .d2 extension, which means an import like @x.txt won't work.

Partial imports

You don't have to import the full file.

For example, if you have a file that defines all the people in your organization, and you just want to show some relations between managers, you can import a specific object.

donut-flowchart.d2

people.d2

정보

Since . is used for targeting, if you want to import from a file with . in its name, use string quotes.

@"schema-v0.1.2"

Render of donut-flowchart.d2

Joe DonutloverdonutsJan Donutbaker lovesbrings

Relative imports

Relative imports are relative to the file, not the executing path.

Consider that your working directory is /Users/You/dev. Your D2 files:

  • /Users/you/dev/d2-stuff/x.d2

The above import will search directory /Users/you/dev/ for y.d2, not /Users/You.

정보

Unnecessary relative imports are removed by autoformat.

@./x will be autoformatted to @x.

Absolute imports

You can also use absolute paths for imports.