Skip to content
This repository was archived by the owner on Mar 17, 2024. It is now read-only.

Commit 4c28064

Browse files
committed
fix module api
1 parent 450218e commit 4c28064

File tree

16 files changed

+247
-290
lines changed

16 files changed

+247
-290
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
lib

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ Temporary Items
131131
# End of https://www.gitignore.io/api/macos
132132

133133
# Output Modules
134-
core
135-
web
134+
lib
136135
docs
137136
!src/**/*
138137
*.d.ts

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
lib

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"publishConfig": {
2424
"access": "public"
2525
},
26-
"main": "core/index.js",
27-
"module": "core/index.mjs",
28-
"types": "core/index.d.ts",
26+
"main": "lib/index.js",
27+
"module": "lib/index.mjs",
28+
"types": "lib/index.d.ts",
2929
"scripts": {
3030
"example": "ts-node example/example",
3131
"build": "ts-node rollup.build",

rollup.build.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { rollup } from 'rollup';
22
import typescript from 'rollup-plugin-typescript2';
33
import { terser } from 'rollup-plugin-terser';
44
import commonjs from '@rollup/plugin-commonjs';
5-
import { move, remove } from 'fs-extra';
5+
import { remove } from 'fs-extra';
66

77
async function build({
88
input,
@@ -43,21 +43,11 @@ async function build({
4343
}
4444

4545
(async (): Promise<void> => {
46-
await Promise.all([remove('core'), remove('web')]);
47-
await Promise.all([
48-
build({
49-
input: 'src/index.ts',
50-
output: 'core',
51-
declaration: true,
52-
external: ['react', 'react-dom/server', 'ts-graphviz', 'prop-types', 'react-reconciler'],
53-
}),
54-
build({
55-
input: 'src/web/index.ts',
56-
output: 'core/web',
57-
declaration: false,
58-
external: ['react', 'react-dom/server', 'ts-graphviz', 'react-reconciler', '@hpcc-js/wasm'],
59-
}),
60-
]);
61-
62-
await move('core/web', 'web');
46+
await remove('lib');
47+
await build({
48+
input: 'src/index.ts',
49+
output: 'lib',
50+
declaration: true,
51+
external: ['react', 'react-dom/server', 'ts-graphviz', 'prop-types', 'react-reconciler', '@hpcc-js/wasm'],
52+
});
6353
})();

src/components/Graphviz.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* eslint-disable react/no-danger */
2+
/* eslint-disable react/prop-types */
3+
import React, { FC, useMemo, ReactElement } from 'react';
4+
import { renderToDot } from '../renderer/render';
5+
import { useRendered, Engine, Image, File } from '../hooks/use-rendered';
6+
7+
type Props = {
8+
children: ReactElement;
9+
engine?: Engine;
10+
images?: Image[];
11+
files?: File[];
12+
};
13+
14+
/**
15+
* Web only
16+
*/
17+
export const Graphviz: FC<Props> = ({ children, engine, images, files }) => {
18+
const dot = useMemo(() => renderToDot(children), [children]);
19+
const rendered = useRendered(dot, engine, 'svg', { images, files });
20+
const svg = useMemo((): { __html: string } | undefined => {
21+
if (rendered) {
22+
return {
23+
__html: rendered,
24+
};
25+
}
26+
return undefined;
27+
}, [rendered]);
28+
return <div dangerouslySetInnerHTML={svg} />;
29+
};
30+
31+
Graphviz.defaultProps = {
32+
images: [],
33+
files: [],
34+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import { Graphviz } from '../Graphviz';
4+
import { Engine, Format, Image } from '../../hooks/use-rendered';
5+
import { Digraph } from '../Digraph';
6+
import { Node } from '../Node';
7+
import { Edge } from '../Edge';
8+
9+
jest.mock('../../hooks/use-rendered', () => {
10+
return {
11+
useRendered: (
12+
dot: string,
13+
engine?: Engine,
14+
format?: Format,
15+
ext?: {
16+
images?: Image[];
17+
files?: File[];
18+
},
19+
): string => JSON.stringify({ dot, engine, format, ext }),
20+
};
21+
});
22+
23+
describe('Graphviz component', () => {
24+
test('render dot element', () => {
25+
const { container } = render(
26+
<Graphviz>
27+
<Digraph>
28+
<Node id="n1" />
29+
<Node id="n2" />
30+
<Node id="n3" />
31+
<Edge targets={['n1', 'n2', 'n3']} />
32+
<Edge targets={['n1', 'n3']} />
33+
</Digraph>
34+
</Graphviz>,
35+
);
36+
expect(container).toMatchSnapshot();
37+
});
38+
});

src/web/components/__tests__/__snapshots__/Graphviz.spec.tsx.snap renamed to src/components/__tests__/__snapshots__/Graphviz.spec.tsx.snap

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,3 @@ exports[`Graphviz component render dot element 1`] = `
77
</div>
88
</div>
99
`;
10-
11-
exports[`Graphviz component render dot string 1`] = `
12-
<div>
13-
<div>
14-
{"dot":"digraph { a -&gt; b -&gt; c; a-&gt;c; }","format":"svg","ext":{"images":[],"files":[]}}
15-
</div>
16-
</div>
17-
`;

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './renderer/render';
2+
export * from './hooks/use-rendered';
23
export * from './hooks/use-cluster';
34
export * from './hooks/use-graphviz-context';
45
export * from './hooks/use-digraph';
@@ -8,6 +9,7 @@ export * from './hooks/use-edge';
89
export * from './hooks/use-node';
910
export * from './hooks/use-rendered-id';
1011
export * from './hooks/use-root-cluster';
12+
export * from './components/Graphviz';
1113
export * from './components/HtmlLike';
1214
export * from './components/Graph';
1315
export * from './components/Digraph';

0 commit comments

Comments
 (0)