UNPKG

2.38 kBJavaScriptView Raw
1import * as visitor from './react-dom/block.js'
2import getStyleForProperty from './react-dom/get-style-for-property.js'
3import getStyles from './react-dom/get-styles.js'
4import getValueForProperty from './react-dom/get-value-for-property.js'
5import maybeUsesRouter from './react-dom/maybe-uses-router.js'
6import restrictedNames from './react-dom/restricted-names.js'
7import toComponent from './react/to-component.js'
8import walk from './walk.js'
9
10const imports = {
11 Animated: "import Animated from 'react-dom-animated'",
12 Link: "import { Link } from 'react-router-dom'",
13 Route: "import { Route } from 'react-router-dom'",
14 Router: "import { BrowserRouter as Router } from 'react-router-dom'",
15}
16
17export default ({
18 debug,
19 enableAnimated = true,
20 file,
21 getFont = () => false,
22 getImport,
23 name,
24 track = true,
25 views,
26}) => {
27 const finalName = restrictedNames.includes(name) ? `${name}1` : name
28 if (name !== finalName) {
29 console.warn(
30 `// "${name}" is a Views reserved name.
31 We've renamed it to "${finalName}", so your view should work but this isn't ideal.
32 To fix this, change its file name to something else.`
33 )
34 }
35
36 const state = {
37 captures: [],
38 cssDynamic: false,
39 cssStatic: false,
40 enableAnimated,
41 defaultProps: false,
42 debug,
43 file,
44 getFont,
45 getStyleForProperty,
46 getValueForProperty,
47 images: [],
48 isDynamic: false,
49 isReactNative: false,
50 name: finalName,
51 render: [],
52 styles: {},
53 svgs: [],
54 usedBlockNames: { [finalName]: 1 },
55 uses: [],
56 testIdKey: 'data-test-id',
57 testIds: {},
58 track,
59 use(block) {
60 if (
61 state.uses.includes(block) ||
62 /props/.test(block) ||
63 block === finalName
64 )
65 return
66
67 state.uses.push(block)
68 },
69 withRouter: false,
70 }
71
72 if (name !== finalName) {
73 console.warn(
74 `// ${name} is a Views reserved name. To fix this, change its file name to something else.`
75 )
76 }
77
78 const parsed = views[name]
79 state.fonts = parsed.fonts
80
81 walk(parsed.views[0], visitor, state)
82 maybeUsesRouter(state)
83
84 const finalGetImport = name => imports[name] || getImport(name)
85
86 return {
87 code: toComponent({
88 getImport: finalGetImport,
89 getStyles,
90 name: finalName,
91 state,
92 }),
93 fonts: parsed.fonts,
94 props: parsed.props,
95 svgs: state.svgs,
96 }
97}