feat(client): lazy load Monaco editor

pull/36736/head
Valeriy 2019-08-31 02:26:43 +03:00 committed by Mrugesh Mohapatra
parent e7c82713e4
commit 7d52d9a3bb
3 changed files with 13 additions and 42 deletions

View File

@ -137,7 +137,7 @@ exports.createPages = function createPages({ graphql, actions }) {
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin'); const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
exports.onCreateWebpackConfig = ({ stage, plugins, actions }) => { exports.onCreateWebpackConfig = ({ plugins, actions }) => {
actions.setWebpackConfig({ actions.setWebpackConfig({
node: { node: {
fs: 'empty' fs: 'empty'
@ -153,24 +153,10 @@ exports.onCreateWebpackConfig = ({ stage, plugins, actions }) => {
process.env.FREECODECAMP_NODE_ENV || 'development' process.env.FREECODECAMP_NODE_ENV || 'development'
), ),
PAYPAL_SUPPORTERS: JSON.stringify(process.env.PAYPAL_SUPPORTERS || 404) PAYPAL_SUPPORTERS: JSON.stringify(process.env.PAYPAL_SUPPORTERS || 404)
}) }),
new MonacoWebpackPlugin()
] ]
}); });
if (stage !== 'build-html') {
actions.setWebpackConfig({
plugins: [new MonacoWebpackPlugin()]
});
}
if (stage === 'build-html') {
actions.setWebpackConfig({
plugins: [
plugins.normalModuleReplacement(
/react-monaco-editor/,
require.resolve('./src/__mocks__/monacoEditorMock.js')
)
]
});
}
}; };
exports.onCreateBabelConfig = ({ actions }) => { exports.onCreateBabelConfig = ({ actions }) => {

View File

@ -1,15 +0,0 @@
/* eslint-disable */
import React from 'react';
export default props => {
const { width = '100%', height = '100%' } = props;
const fixedWidth =
width.toString().indexOf('%') !== -1 ? width : `${width}px`;
const fixedHeight =
height.toString().indexOf('%') !== -1 ? height : `${height}px`;
const style = {
width: fixedWidth,
height: fixedHeight
};
return <div style={style} className="react-monaco-editor-container" />;
};

View File

@ -1,12 +1,13 @@
import React, { Component, Fragment } from 'react'; import React, { Component, Suspense } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { bindActionCreators } from 'redux'; import { bindActionCreators } from 'redux';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import MonacoEditor from 'react-monaco-editor'; import { createSelector } from 'reselect';
import { executeChallenge, updateFile } from '../redux'; import { executeChallenge, updateFile } from '../redux';
import { userSelector } from '../../../redux'; import { userSelector } from '../../../redux';
import { createSelector } from 'reselect';
const MonacoEditor = React.lazy(() => import('react-monaco-editor'));
const propTypes = { const propTypes = {
contents: PropTypes.string, contents: PropTypes.string,
@ -45,7 +46,7 @@ const defineMonacoThemes = monaco => {
return; return;
} }
monacoThemesDefined = true; monacoThemesDefined = true;
const yellowCollor = 'FFFF00'; const yellowColor = 'FFFF00';
const lightBlueColor = '9CDCFE'; const lightBlueColor = '9CDCFE';
const darkBlueColor = '00107E'; const darkBlueColor = '00107E';
monaco.editor.defineTheme('vs-dark-custom', { monaco.editor.defineTheme('vs-dark-custom', {
@ -56,9 +57,9 @@ const defineMonacoThemes = monaco => {
}, },
rules: [ rules: [
{ token: 'delimiter.js', foreground: lightBlueColor }, { token: 'delimiter.js', foreground: lightBlueColor },
{ token: 'delimiter.parenthesis.js', foreground: yellowCollor }, { token: 'delimiter.parenthesis.js', foreground: yellowColor },
{ token: 'delimiter.array.js', foreground: yellowCollor }, { token: 'delimiter.array.js', foreground: yellowColor },
{ token: 'delimiter.bracket.js', foreground: yellowCollor } { token: 'delimiter.bracket.js', foreground: yellowColor }
] ]
}); });
monaco.editor.defineTheme('vs-custom', { monaco.editor.defineTheme('vs-custom', {
@ -129,8 +130,7 @@ class Editor extends Component {
const { contents, ext, theme, fileKey } = this.props; const { contents, ext, theme, fileKey } = this.props;
const editorTheme = theme === 'night' ? 'vs-dark-custom' : 'vs-custom'; const editorTheme = theme === 'night' ? 'vs-dark-custom' : 'vs-custom';
return ( return (
<Fragment> <Suspense fallback={<div />}>
<base href='/' />
<MonacoEditor <MonacoEditor
editorDidMount={this.editorDidMount} editorDidMount={this.editorDidMount}
editorWillMount={this.editorWillMount} editorWillMount={this.editorWillMount}
@ -141,7 +141,7 @@ class Editor extends Component {
theme={editorTheme} theme={editorTheme}
value={contents} value={contents}
/> />
</Fragment> </Suspense>
); );
} }
} }