Skip to content

Commit 65622df

Browse files
author
Tobias Lengsholz
committed
Add documentation
1 parent 53e4090 commit 65622df

File tree

1 file changed

+90
-2
lines changed

1 file changed

+90
-2
lines changed

docs/rules/no-cross-imports.md

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ This rule takes one argument:
88

99
```
1010
...
11-
"workspaces/no-cross-imports": ["error", { allow: ["@project/A", "@project/B"] }]
11+
"workspaces/no-cross-imports": ["error", { allow: ["@project/A", "@project/B"], scopes: { enable: true, folderName: 'shared' } }]
1212
...
1313
```
1414

1515
### allow
1616

1717
Takes a single or a list of package names to exclude from this rule.
1818

19-
## Example
19+
#### Example
2020

2121
These examples have the following project structure:
2222

@@ -46,3 +46,91 @@ import bar from '../B/bar';
4646
// inside "project/index.js"
4747
import foo from './packages/B/foo';
4848
```
49+
50+
### scopes
51+
52+
Takes either a boolean or an options object. Defaults to `false`.
53+
54+
Scopes are a way to partially allow imports across workspace boundaries.
55+
In larger monorepos, you might run into a situation where you want to group code
56+
across _some_ packages, but not all of them. A natural way to do this would be
57+
to create folder structure that visualizes this. So your structure might look
58+
like this:
59+
60+
```
61+
project
62+
└─── packages
63+
└─── shared-components/
64+
└─── package.json
65+
└─── welcome-page/
66+
└─── package.json
67+
└─── user-management/
68+
└─── registration/
69+
└─── package.json
70+
└─── login/
71+
└─── package.json
72+
```
73+
74+
Now, we may want to share code across the packages in the `user-management`
75+
section (e.g. fetching the user object, user form components etc.). With scopes,
76+
i am always allowed to import from a package with a **special folder name**
77+
(see below) given that it shares a common folder parent. So for
78+
the above case, I would be able to do this:
79+
80+
```
81+
project
82+
└─── packages
83+
└─── shared-components/
84+
└─── package.json
85+
└─── welcome-page/
86+
└─── package.json
87+
└─── user-management/
88+
└─── shared/
89+
└─── package.json
90+
└─── registration/
91+
└─── package.json
92+
└─── login/
93+
└─── package.json
94+
```
95+
96+
When passing a boolean, the default folder name `shared` will be used. If you
97+
want to configure this, pass another string via the `folderName` key.
98+
99+
#### Example
100+
101+
These examples have the following project structure:
102+
103+
```
104+
project
105+
└─── packages
106+
└─── shared-components/
107+
└─── package.json
108+
└─── welcome-page/
109+
└─── package.json
110+
└─── user-management/
111+
└─── shared/
112+
└─── package.json
113+
└─── registration/
114+
└─── package.json
115+
└─── login/
116+
└─── package.json
117+
```
118+
119+
Examples of **incorrect** code for this rule:
120+
121+
```js
122+
// inside "project/packages/welcome-page/index.js"
123+
// configuration: [{ allow: "@project/user-management-shared", scopes: true }]
124+
import foo from '@project/user-management-shared';
125+
```
126+
127+
Examples of **correct** code for this rule:
128+
129+
```js
130+
// inside "project/packages/user-management/registration/index.js"
131+
// configuration: [{ allow: "@project/user-management-shared", scopes: true }]
132+
import foo from '@project/user-management-shared';
133+
134+
// inside "project/index.js"
135+
import foo from './packages/user-management/registration';
136+
```

0 commit comments

Comments
 (0)