-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranspile.js
More file actions
57 lines (51 loc) · 928 Bytes
/
transpile.js
File metadata and controls
57 lines (51 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import React from "react";
import { transform } from "buble";
import {
Page,
Text,
Link,
Font,
View,
Canvas,
Note,
Image,
StyleSheet
} from "@react-pdf/renderer";
import styled from "@react-pdf/styled-components";
const Document = "DOCUMENT";
const primitives = {
Document,
Page,
Text,
Link,
Font,
View,
Note,
Image,
Canvas,
StyleSheet,
styled
};
const transpile = (code, callback, onError) => {
try {
const result = transform(code, {
objectAssign: "Object.assign",
transforms: {
dangerousForOf: true,
dangerousTaggedTemplateString: true
}
});
const res = new Function(
"React",
"ReactPDF",
...Object.keys(primitives),
result.code
);
res(React, { render: doc => callback(doc) }, ...Object.values(primitives));
} catch (e) {
if (onError) {
onError(e);
}
}
};
export default transpile;