App.js 5.75 KB
Newer Older
1
2
3
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
Sophie Alpert's avatar
Sophie Alpert committed
4
5
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
6
7
 */

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

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

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

    // 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
24
25
  }

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

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

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

    this.state = { feature: null };

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

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

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

  render() {
202
203
204
205
206
    const { feature } = this.state;
    if (feature !== null) {
      return <BuiltEmitter feature={feature} />;
    }
    return null;
207
208
209
210
  }
}

export default App;