Skip to content

Commit 24b2004

Browse files
authored
Use mono-packages for docs examples (#3327)
1 parent bb28936 commit 24b2004

File tree

6 files changed

+118
-98
lines changed

6 files changed

+118
-98
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
"react-axe": "^3.0.2",
160160
"react-dom": "^18.0.0",
161161
"react-test-renderer": "^16.9.0",
162+
"recast": "^0.20",
162163
"recursive-readdir": "^2.2.2",
163164
"regenerator-runtime": "0.13.3",
164165
"rimraf": "^2.6.3",

packages/dev/docs/pages/react-aria/getting-started.mdx

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,32 @@ screen readers.
2828

2929
## Installation
3030

31-
React Aria is **incrementally adoptable**. Each component is published as a separate package, so you can try it
32-
out in a single component and gradually add more over time. All of these packages are published under the
33-
[@react-aria](https://www.npmjs.com/org/react-aria) scope on npm.
34-
35-
You can install any React Aria package using a package manager like [npm](https://docs.npmjs.com/cli/npm) or
31+
React Aria can be installed using a package manager like [npm](https://docs.npmjs.com/cli/npm) or
3632
[yarn](https://classic.yarnpkg.com/lang/en/).
3733

34+
```
35+
yarn add react-aria
36+
```
37+
38+
If you prefer, you can also use our hooks from individually versioned packages. This allows you to only
39+
install the hooks you use, or more granularly manage their versions. The individual packages are published
40+
under the [@react-aria](https://www.npmjs.com/org/react-aria) scope on npm. For example:
41+
3842
```
3943
yarn add @react-aria/button
4044
```
4145

42-
We also offer a single package containing all React Aria hooks. See [below](#mono-package) for details.
46+
Once installed, hooks can be used from the monopackage or individual packages the same way.
47+
48+
```tsx
49+
// Monopackage
50+
import {useButton} from 'react-aria';
51+
```
52+
53+
```tsx keepIndividualImports
54+
// Individual packages
55+
import {useButton} from '@react-aria/button';
56+
```
4357

4458
## Building a component
4559

@@ -123,31 +137,6 @@ function Checkbox(props) {
123137

124138
See the [useCheckbox](useCheckbox.html) docs for a full example of a custom styled checkbox.
125139

126-
## Mono-package
127-
128-
In addition to individual packages, we also offer a mono-package containing all of the React Aria hooks in a single
129-
package. This ensures that all packages use compatible versions and are upgraded together. If you release all components
130-
in your component library in a single package, or just want to get started easily without installing a lot of packages,
131-
the mono-package may be the way to go. It is available as the [react-aria](https://npmjs.com/package/react-aria) package
132-
on npm.
133-
134-
```
135-
yarn add react-aria
136-
```
137-
138-
Once installed, all hooks work the same way as the individual packages, but are imported from the `react-aria` package
139-
instead of individual packages.
140-
141-
```tsx
142-
// Individual packages
143-
import {useButton} from '@react-aria/button';
144-
```
145-
146-
```tsx
147-
// Monopackage
148-
import {useButton} from 'react-aria';
149-
```
150-
151140
## Next steps
152141

153142
Now that you understand how to use the hooks in React Aria to build your own components, you can read the documentation

packages/dev/docs/pages/react-stately/getting-started.mdx

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,32 @@ and user interactions for many components, while React Stately provides state ma
3131

3232
## Installation
3333

34-
React Stately is **incrementally adoptable**. Each component is published as a separate package, so you can try it
35-
out in a single component and gradually add more over time. All of these packages are published under the
36-
[@react-stately](https://www.npmjs.com/org/react-stately) scope on npm.
37-
38-
You can install any React Stately package using a package manager like [npm](https://docs.npmjs.com/cli/npm) or
34+
React Stately can be installed using a package manager like [npm](https://docs.npmjs.com/cli/npm) or
3935
[yarn](https://classic.yarnpkg.com/lang/en/).
4036

37+
```
38+
yarn add react-stately
39+
```
40+
41+
If you prefer, you can also use our hooks from individually versioned packages. This allows you to only
42+
install the hooks you use, or more granularly manage their versions. The individual packages are published
43+
under the [@react-stately](https://www.npmjs.com/org/react-stately) scope on npm. For example:
44+
4145
```
4246
yarn add @react-stately/radio
4347
```
4448

45-
We also offer a single package containing all React Stately hooks. See [below](#mono-package) for details.
49+
Once installed, hooks can be used from the monopackage or individual packages the same way.
50+
51+
```tsx
52+
// Monopackage
53+
import {useRadioGroupState} from 'react-stately';
54+
```
55+
56+
```tsx keepIndividualImports
57+
// Individual packages
58+
import {useRadioGroupState} from '@react-stately/radio';
59+
```
4660

4761
## Building a component
4862

@@ -88,31 +102,6 @@ If you're using React Stately on the web, see [useRadioGroup](../react-aria/useR
88102
handle more of this behavior and accessibility for you. To see an example of how React Stately manages more complex state,
89103
see [useSelectState](useSelectState.html).
90104

91-
## Mono-package
92-
93-
In addition to individual packages, we also offer a mono-package containing all of the React Stately hooks in a single
94-
package. This ensures that all packages use compatible versions and are upgraded together. If you release all components
95-
in your component library in a single package, or just want to get started easily without installing a lot of packages,
96-
the mono-package may be the way to go. It is available as the [react-stately](https://npmjs.com/package/react-stately) package
97-
on npm.
98-
99-
```
100-
yarn add react-stately
101-
```
102-
103-
Once installed, all hooks work the same way as the individual packages, but are imported from the `react-stately` package
104-
instead of individual packages.
105-
106-
```tsx
107-
// Individual packages
108-
import {useRadioGroupState} from '@react-stately/radio';
109-
```
110-
111-
```tsx
112-
// Monopackage
113-
import {useRadioGroupState} from 'react-stately';
114-
```
115-
116105
## Next steps
117106

118107
Now that you understand how to use the hooks in React Stately to build your own components, you can read the documentation

packages/dev/docs/src/HeaderInfo.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,34 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13+
import ariaMonopackage from 'react-aria/package.json';
1314
import {Flex} from '@react-spectrum/layout';
1415
import js from 'highlight.js/lib/languages/javascript';
1516
import Lowlight from 'react-lowlight';
1617
import React from 'react';
1718
import {ResourceCard} from './ResourceCard';
19+
import rspMonopackage from '@adobe/react-spectrum/package.json';
20+
import statelyMonopackage from 'react-stately/package.json';
1821
import styles from './headerInfo.css';
1922
import typographyStyles from '@adobe/spectrum-css-temp/components/typography/vars.css';
2023

2124
Lowlight.registerLanguage('js', js);
2225

26+
const monopackages = {
27+
'@react-spectrum': {
28+
importName: '@adobe/react-spectrum',
29+
version: rspMonopackage.version
30+
},
31+
'@react-aria': {
32+
importName: 'react-aria',
33+
version: ariaMonopackage.version
34+
},
35+
'@react-stately': {
36+
importName: 'react-stately',
37+
version: statelyMonopackage.version
38+
}
39+
};
40+
2341
export function HeaderInfo(props) {
2442
let {
2543
packageData,
@@ -29,8 +47,12 @@ export function HeaderInfo(props) {
2947

3048
let preRelease = packageData.version.match(/(alpha)|(beta)|(rc)/);
3149
let importName = packageData.name;
32-
if (importName.startsWith('@react-spectrum') && process.env.DOCS_ENV === 'production' && !preRelease) {
33-
importName = '@adobe/react-spectrum';
50+
let version = packageData.version;
51+
if (!preRelease) {
52+
let scope = importName.split('/')[0];
53+
if (monopackages[scope]) {
54+
({importName, version} = monopackages[scope]);
55+
}
3456
}
3557

3658
return (
@@ -43,7 +65,7 @@ export function HeaderInfo(props) {
4365
</tr>
4466
<tr>
4567
<th className={typographyStyles['spectrum-Body--secondary']}>version</th>
46-
<td className={typographyStyles['spectrum-Body4']}>{packageData.version}</td>
68+
<td className={typographyStyles['spectrum-Body4']}>{version}</td>
4769
</tr>
4870
{componentNames &&
4971
<tr>

packages/dev/parcel-transformer-mdx-docs/MDXTransformer.js

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -407,32 +407,42 @@ function transformExample(node, preRelease) {
407407
/* Replace individual package imports in the code
408408
* with monorepo imports if building for production and not a pre-release
409409
*/
410-
if (process.env.DOCS_ENV === 'production' && !preRelease && node.value.includes('@react-spectrum')) {
411-
let specifiers = [];
412-
let last;
410+
if (!preRelease && /@react-spectrum|@react-aria|@react-stately/.test(node.value)) {
411+
let specifiers = {};
412+
const recast = require('recast');
413413
const traverse = require('@babel/traverse').default;
414414
const {parse} = require('@babel/parser');
415-
const generate = require('@babel/generator').default;
416-
let ast = parse(node.value, {
417-
sourceType: 'module',
418-
plugins: ['jsx', 'typescript']
415+
let ast = recast.parse(node.value, {
416+
parser: {
417+
parse() {
418+
return parse(node.value, {
419+
sourceType: 'module',
420+
plugins: ['jsx', 'typescript'],
421+
tokens: true
422+
});
423+
}
424+
}
419425
});
420426

421427
traverse(ast, {
422428
ImportDeclaration(path) {
423-
if (path.node.source.value.startsWith('@react-spectrum') && !(node.meta && node.meta.split(' ').includes('keepIndividualImports'))) {
429+
if (/^(@react-spectrum|@react-aria|@react-stately)/.test(path.node.source.value) && !(node.meta && node.meta.split(' ').includes('keepIndividualImports'))) {
430+
let lib = path.node.source.value.split('/')[0];
431+
if (!specifiers[lib]) {
432+
specifiers[lib] = [];
433+
}
434+
424435
let mapping = IMPORT_MAPPINGS[path.node.source.value];
425436
for (let specifier of path.node.specifiers) {
426437
let mapped = mapping && mapping[specifier.imported.name];
427438
if (mapped && specifier.local.name === specifier.imported.name) {
428439
path.scope.rename(specifier.local.name, mapped);
429-
specifiers.push(mapped);
440+
specifiers[lib].push(mapped);
430441
} else {
431-
specifiers.push(specifier.imported.name);
442+
specifiers[lib].push(specifier.imported.name);
432443
}
433444
}
434445

435-
last = path.node;
436446
path.remove();
437447
}
438448
},
@@ -441,26 +451,25 @@ function transformExample(node, preRelease) {
441451
},
442452
Program: {
443453
exit(path) {
444-
if (specifiers.length > 0) {
445-
let literal = t.stringLiteral('@adobe/react-spectrum');
446-
literal.raw = "'@adobe/react-spectrum'";
447-
448-
let decl = t.importDeclaration(
449-
specifiers.map(s => t.importSpecifier(t.identifier(s), t.identifier(s))),
450-
literal
451-
);
452-
453-
decl.loc = last.loc;
454-
decl.start = last.start;
455-
decl.end = last.end;
456-
457-
path.unshiftContainer('body', [decl]);
454+
for (let lib in specifiers) {
455+
let names = specifiers[lib];
456+
if (names.length > 0) {
457+
let monopackage = lib === '@react-spectrum' ? '@adobe/react-spectrum' : lib.slice(1);
458+
let literal = t.stringLiteral(monopackage);
459+
460+
let decl = t.importDeclaration(
461+
names.map(s => t.importSpecifier(t.identifier(s), t.identifier(s))),
462+
literal
463+
);
464+
465+
path.unshiftContainer('body', [decl]);
466+
}
458467
}
459468
}
460469
}
461470
});
462471

463-
node.value = generate(ast).code.replace(/(<WRAPPER>(?:.|\n)*<\/WRAPPER>)/g, '\n($1)');
472+
node.value = recast.print(ast, {objectCurlySpacing: false, quote: 'single'}).code.replace(/(<WRAPPER>(?:.|\n)*<\/WRAPPER>)/g, '\n($1)');
464473
force = true;
465474
}
466475

yarn.lock

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6272,18 +6272,18 @@ ast-types-flow@^0.0.7:
62726272
resolved "https://registry.yarnpkg.com/ast-types-flow/-/ast-types-flow-0.0.7.tgz#f70b735c6bca1a5c9c22d982c3e39e7feba3bdad"
62736273
integrity sha1-9wtzXGvKGlycItmCw+Oef+ujva0=
62746274

6275-
ast-types@^0.13.2:
6276-
version "0.13.2"
6277-
resolved "https://registry.yarnpkg.com/ast-types/-/ast-types-0.13.2.tgz#df39b677a911a83f3a049644fb74fdded23cea48"
6278-
integrity sha512-uWMHxJxtfj/1oZClOxDEV1sQ1HCDkA4MG8Gr69KKeBjEVH0R84WlejZ0y2DcwyBlpAEMltmVYkVgqfLFb2oyiA==
6279-
6280-
ast-types@^0.14.2:
6275+
[email protected], ast-types@^0.14.2:
62816276
version "0.14.2"
62826277
resolved "https://registry.yarnpkg.com/ast-types/-/ast-types-0.14.2.tgz#600b882df8583e3cd4f2df5fa20fa83759d4bdfd"
62836278
integrity sha512-O0yuUDnZeQDL+ncNGlJ78BiO4jnYI3bvMsD5prT0/nsgijG/LpNBIr63gTjVTNsiGkgQhiyCShTgxt8oXOrklA==
62846279
dependencies:
62856280
tslib "^2.0.1"
62866281

6282+
ast-types@^0.13.2:
6283+
version "0.13.2"
6284+
resolved "https://registry.yarnpkg.com/ast-types/-/ast-types-0.13.2.tgz#df39b677a911a83f3a049644fb74fdded23cea48"
6285+
integrity sha512-uWMHxJxtfj/1oZClOxDEV1sQ1HCDkA4MG8Gr69KKeBjEVH0R84WlejZ0y2DcwyBlpAEMltmVYkVgqfLFb2oyiA==
6286+
62876287
astral-regex@^1.0.0:
62886288
version "1.0.0"
62896289
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9"
@@ -10234,7 +10234,7 @@ espree@^7.3.0:
1023410234
acorn-jsx "^5.2.0"
1023510235
eslint-visitor-keys "^1.3.0"
1023610236

10237-
esprima@^4.0.0, esprima@^4.0.1:
10237+
esprima@^4.0.0, esprima@^4.0.1, esprima@~4.0.0:
1023810238
version "4.0.1"
1023910239
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
1024010240
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==
@@ -19651,6 +19651,16 @@ readdirp@~3.6.0:
1965119651
dependencies:
1965219652
picomatch "^2.2.1"
1965319653

19654+
recast@^0.20:
19655+
version "0.20.5"
19656+
resolved "https://registry.yarnpkg.com/recast/-/recast-0.20.5.tgz#8e2c6c96827a1b339c634dd232957d230553ceae"
19657+
integrity sha512-E5qICoPoNL4yU0H0NoBDntNB0Q5oMSNh9usFctYniLBluTthi3RsQVBXIJNbApOlvSwW/RGxIuokPcAc59J5fQ==
19658+
dependencies:
19659+
ast-types "0.14.2"
19660+
esprima "~4.0.0"
19661+
source-map "~0.6.1"
19662+
tslib "^2.0.1"
19663+
1965419664
rechoir@^0.6.2:
1965519665
version "0.6.2"
1965619666
resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384"

0 commit comments

Comments
 (0)