import React from 'react';
import highlight from './highlight';

const languages = [];
export const registerLanguage = (_, language) => languages.push(language);

export default class ReactHighlightAsync extends React.PureComponent {
  static refractor = null;
  static highlightInstance = null;

  state = {
    refractor: ReactHighlightAsync.refractor
  };

  componentWillMount() {
    if (!ReactHighlightAsync.highlightInstance) {
      const refractorPromise = import(/* webpackChunkName:"react-syntax-highlighter/refractor-import" */ 'refractor/core').then(({ default: refractor }) => {
        languages.forEach((language) => refractor.register(language));
        this.setState({ refractor })
      });
      ReactHighlightAsync.highlightInstance = (highlight(null, {}));
    }
  }
  render() {
    return (<ReactHighlightAsync.highlightInstance {...this.props} astGenerator={this.state.refractor} />);
  }
};

@@ -128,16 +128,112 @@
 uage) =%3E
+ %7B%0A  if(ReactHighlightAsync.refractor) %7B%0A    return refractor.register(language);%0A  %7D else %7B%0A   
  languag
@@ -250,16 +250,23 @@
 nguage);
+%0A  %7D%0A%7D;
 %0A%0Aexport
@@ -390,98 +390,84 @@
 e = 
-null;%0A%0A  state = %7B%0A    refractor: ReactHighlightAsync.refractor%0A  %7D;%0A%0A  componentWillMount
+(highlight(null, %7B%7D));%0A  static refractorPromise = null;%0A  %0A  _loadRefractor
 () %7B
@@ -475,13 +475,8 @@
     
-if (!
 Reac
@@ -495,41 +495,8 @@
 ync.
-highlightInstance) %7B%0A      const 
 refr
@@ -643,18 +643,16 @@
 %7B%0A      
-  
 language
@@ -716,25 +716,39 @@
     
-  this.setState(%7B
+ReactHighlightAsync.refractor =
  ref
@@ -757,14 +757,10 @@
 ctor
- %7D)%0A  
+;%0A
     
@@ -769,80 +769,206 @@
 ;%0A  
-    ReactHighlightAsync.highlightInstance = (highlight(null, %7B%7D)
+%7D%0A%0A  componentWillMount() %7B%0A    if (!ReactHighlightAsync.refractorPromise) %7B%0A      this._loadRefractor();%0A    %7D%0A%0A    ReactHighlightAsync.refractorPromise.then(() =%3E %7B%0A      this.forceUpdate(
 );%0A    %7D
 %0A  %7D
@@ -963,16 +963,18 @@
 );%0A    %7D
+);
 %0A  %7D%0A  r
@@ -1068,18 +1068,27 @@
 or=%7B
-this.state
+ReactHighlightAsync
 .ref

Clean up promises