App.js 5.25 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.
 */

10
import React, { Component, PropTypes, createElement } from 'react';
11

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

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

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

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

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

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

    this.state = { feature: null };

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

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

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

  render() {
174
175
176
177
178
    const { feature } = this.state;
    if (feature !== null) {
      return <BuiltEmitter feature={feature} />;
    }
    return null;
179
180
181
182
  }
}

export default App;