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