Skip to content

Commit 768469e

Browse files
committed
Allow dynamically loaded example files
1 parent 9f2f6ed commit 768469e

File tree

12 files changed

+424
-114
lines changed

12 files changed

+424
-114
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ RUN rm -r /usr/share/nginx/html && rm /etc/nginx/conf.d/default.conf
1313
COPY --from=builder /frontend-shared/build /usr/share/nginx/html
1414
COPY ./templates/index.html /usr/share/nginx/html/index.html
1515
COPY ./images /usr/share/nginx/html/images
16+
COPY ./src/pattern-library/examples /usr/share/nginx/html/examples
1617
COPY conf/nginx.conf /etc/nginx/conf.d/default.conf
1718

1819
EXPOSE 5001

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ import { Link } from '@hypothesis/frontend-shared';
4949
5050
- [Development guide](docs/developing.md)
5151
- [Release guide](docs/releases.md)
52+
- [Adding examples](docs/examples.md)

docs/examples.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Adding code examples
2+
3+
Component library documentation frequently needs to show interactive examples, along with the code for them.
4+
5+
Manually writing those code snippets has some issues: they are not covered by your type checking and linting tasks, and they can accidentally get outdated.
6+
7+
The web-based documentation included with this library allows to create example files which are both used as regular modules that can be imported for interactive examples, but also read as plain text to generate their corresponding code snippet.
8+
9+
These files are type-checked, formatted and linted like any other source files, and the code snippet will always be in sync with the interactive example.
10+
11+
## Using example files
12+
13+
When you want to create a code example, add a file with the name of your choice inside `src/pattern-library/examples` directory.
14+
15+
You can then reference this file from the web-based pattern library, passing the `exampleFile` prop to the `<Library.Demo />` component.
16+
17+
```tsx
18+
<Library.Demo exampleFile="some-example-file-name" />
19+
```
20+
21+
## Considerations
22+
23+
There are some considerations and limitations when working with example files.
24+
25+
- They all need to have the `tsx` extension.
26+
- Nested directories are not supported, so it's a good idea to add contextual prefixes to example files. For example, all files related with `SelectNext` can be prefixed with `select-next` to make sure they are all grouped together.
27+
- Example files need to have a Preact component as their default export.
28+
- When generating the source code snippet, import statements are stripped out, to avoid internal module references which are not relevant for the library consumers.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@hypothesis/frontend-testing": "^1.2.2",
1515
"@rollup/plugin-babel": "^6.0.0",
1616
"@rollup/plugin-commonjs": "^25.0.0",
17+
"@rollup/plugin-dynamic-import-vars": "^2.1.2",
1718
"@rollup/plugin-node-resolve": "^15.0.0",
1819
"@rollup/plugin-virtual": "^3.0.0",
1920
"@trivago/prettier-plugin-sort-imports": "^4.1.1",

rollup.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { babel } from '@rollup/plugin-babel';
22
import commonjs from '@rollup/plugin-commonjs';
3+
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
34
import { nodeResolve } from '@rollup/plugin-node-resolve';
45
import { string } from 'rollup-plugin-string';
56

@@ -27,6 +28,7 @@ function bundleConfig(name, entryFile) {
2728
exclude: 'node_modules/**',
2829
extensions: ['.js', '.ts', '.tsx'],
2930
}),
31+
dynamicImportVars(),
3032
nodeResolve({ extensions: ['.js', '.ts', '.tsx'] }),
3133
commonjs({ include: 'node_modules/**' }),
3234
string({

scripts/serve-pattern-library.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export function servePatternLibrary(port = 4001) {
1111
app.use('/scripts', express.static(path.join(dirname, '../build/scripts')));
1212
app.use('/styles', express.static(path.join(dirname, '../build/styles')));
1313
app.use('/images', express.static(path.join(dirname, '../images')));
14+
app.use(
15+
'/examples',
16+
express.static(path.join(dirname, '../src/pattern-library/examples')),
17+
);
1418

1519
// For any other path, serve the index.html file to allow client-side routing
1620
app.get('/:path?', (req, res) => {

0 commit comments

Comments
 (0)