App.js 5.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

Joe Haddad's avatar
Joe Haddad committed
10
11
import React, { Component, createElement } from 'react';
import PropTypes from 'prop-types';
12

13
14
class BuiltEmitter extends Component {
  static propTypes = {
15
16
    feature: PropTypes.func.isRequired,
  };
Fabrizio Castellarin's avatar
Fabrizio Castellarin committed
17
18

  componentDidMount() {
19
    const { feature } = this.props;
20
21
22
23
24
25

    // Class components must call this.props.onReady when they're ready for the test.
    // We will assume functional components are ready immediately after mounting.
    if (!Component.isPrototypeOf(feature)) {
      this.handleReady();
    }
Fabrizio Castellarin's avatar
Fabrizio Castellarin committed
26
27
  }

28
  handleReady() {
29
    document.dispatchEvent(new Event('ReactFeatureDidMount'));
30
  }
Fabrizio Castellarin's avatar
Fabrizio Castellarin committed
31

32
  render() {
33
    const { props: { feature }, handleReady } = this;
34
35
36
    return (
      <div>
        {createElement(feature, {
37
          onReady: handleReady,
38
39
40
        })}
      </div>
    );
Fabrizio Castellarin's avatar
Fabrizio Castellarin committed
41
42
43
  }
}

44
class App extends Component {
45
46
47
48
49
50
51
52
53
  constructor(props) {
    super(props);

    this.state = { feature: null };

    this.setFeature = this.setFeature.bind(this);
  }

  componentDidMount() {
54
    const feature = window.location.hash.slice(1);
55
    switch (feature) {
56
      case 'array-destructuring':
57
58
59
        import('./features/syntax/ArrayDestructuring').then(f =>
          this.setFeature(f.default)
        );
60
61
        break;
      case 'array-spread':
62
        import('./features/syntax/ArraySpread').then(f =>
63
64
          this.setFeature(f.default)
        );
65
66
        break;
      case 'async-await':
67
        import('./features/syntax/AsyncAwait').then(f =>
68
69
          this.setFeature(f.default)
        );
70
71
        break;
      case 'class-properties':
72
        import('./features/syntax/ClassProperties').then(f =>
73
74
          this.setFeature(f.default)
        );
75
76
        break;
      case 'computed-properties':
77
78
79
        import('./features/syntax/ComputedProperties').then(f =>
          this.setFeature(f.default)
        );
80
81
        break;
      case 'css-inclusion':
82
        import('./features/webpack/CssInclusion').then(f =>
83
84
          this.setFeature(f.default)
        );
85
86
        break;
      case 'custom-interpolation':
87
88
89
        import('./features/syntax/CustomInterpolation').then(f =>
          this.setFeature(f.default)
        );
90
91
        break;
      case 'default-parameters':
92
        import('./features/syntax/DefaultParameters').then(f =>
93
94
          this.setFeature(f.default)
        );
95
96
        break;
      case 'destructuring-and-await':
97
98
99
        import('./features/syntax/DestructuringAndAwait').then(f =>
          this.setFeature(f.default)
        );
100
101
        break;
      case 'file-env-variables':
102
        import('./features/env/FileEnvVariables').then(f =>
103
104
          this.setFeature(f.default)
        );
105
106
        break;
      case 'generators':
107
        import('./features/syntax/Generators').then(f =>
108
109
          this.setFeature(f.default)
        );
110
111
        break;
      case 'image-inclusion':
112
        import('./features/webpack/ImageInclusion').then(f =>
113
114
          this.setFeature(f.default)
        );
115
116
        break;
      case 'json-inclusion':
117
        import('./features/webpack/JsonInclusion').then(f =>
118
119
          this.setFeature(f.default)
        );
120
        break;
Joe Haddad's avatar
Joe Haddad committed
121
122
      case 'linked-modules':
        import('./features/webpack/LinkedModules').then(f =>
123
124
          this.setFeature(f.default)
        );
Joe Haddad's avatar
Joe Haddad committed
125
        break;
126
      case 'node-path':
127
        import('./features/env/NodePath').then(f => this.setFeature(f.default));
128
129
        break;
      case 'no-ext-inclusion':
130
        import('./features/webpack/NoExtInclusion').then(f =>
131
132
          this.setFeature(f.default)
        );
133
134
        break;
      case 'object-destructuring':
135
136
137
        import('./features/syntax/ObjectDestructuring').then(f =>
          this.setFeature(f.default)
        );
138
139
        break;
      case 'object-spread':
140
        import('./features/syntax/ObjectSpread').then(f =>
141
142
          this.setFeature(f.default)
        );
143
144
        break;
      case 'promises':
145
        import('./features/syntax/Promises').then(f =>
146
147
          this.setFeature(f.default)
        );
148
        break;
149
      case 'public-url':
150
        import('./features/env/PublicUrl').then(f =>
151
152
          this.setFeature(f.default)
        );
153
        break;
154
      case 'rest-and-default':
155
        import('./features/syntax/RestAndDefault').then(f =>
156
157
          this.setFeature(f.default)
        );
158
159
        break;
      case 'rest-parameters':
160
        import('./features/syntax/RestParameters').then(f =>
161
162
          this.setFeature(f.default)
        );
163
164
        break;
      case 'shell-env-variables':
165
        import('./features/env/ShellEnvVariables').then(f =>
166
167
          this.setFeature(f.default)
        );
168
169
        break;
      case 'svg-inclusion':
170
        import('./features/webpack/SvgInclusion').then(f =>
171
172
          this.setFeature(f.default)
        );
173
174
        break;
      case 'template-interpolation':
175
176
177
        import('./features/syntax/TemplateInterpolation').then(f =>
          this.setFeature(f.default)
        );
178
179
        break;
      case 'unknown-ext-inclusion':
180
181
182
        import('./features/webpack/UnknownExtInclusion').then(f =>
          this.setFeature(f.default)
        );
183
        break;
184
185
      default:
        throw new Error(`Missing feature "${feature}"`);
186
187
188
189
190
191
192
193
    }
  }

  setFeature(feature) {
    this.setState({ feature });
  }

  render() {
194
195
196
197
198
    const { feature } = this.state;
    if (feature !== null) {
      return <BuiltEmitter feature={feature} />;
    }
    return null;
199
200
201
202
  }
}

export default App;