App.js 5.43 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 window.Event('ReactFeatureDidMount'));
30
  }
Fabrizio Castellarin's avatar
Fabrizio Castellarin committed
31

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

47
class App extends Component {
48
49
50
51
52
53
54
55
56
  constructor(props) {
    super(props);

    this.state = { feature: null };

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

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

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

  render() {
179
180
181
182
183
    const { feature } = this.state;
    if (feature !== null) {
      return <BuiltEmitter feature={feature} />;
    }
    return null;
184
185
186
187
  }
}

export default App;