CodeMirror does not work with React/Webpack

Terry Sahaidak picture Terry Sahaidak · Oct 21, 2015 · Viewed 11.3k times · Source

I'm working on some markdown editor for my react project. I wanna use CodeMirror as the code editor, but it seems it does not working when I build it with webpack.

If be honest, CodeMirror are in the DOM-tree, textArea is hidden, but everything I see is:

enter image description here

and

enter image description here

UPD: The same code works perfect on codepen. I guess it's a problem with webpack.

some code:

index.js

import React from 'react';
import ReactDOM from 'react-dom';

import {Editor} from './components';

const rootElement = document.getElementById('root');

ReactDOM.render(<Editor />, rootElement);

components/editor.js

import React, { Component } from 'react';
import cm from 'codemirror';
require('codemirror/mode/markdown/markdown');

export class App extends Component {
  componentDidMount() {
    this.codeMirror = cm.fromTextArea(this.refs.editor, {mode: 'markdown'})
  }
  render() {
    return (
      <div>
        <textarea ref='editor' autoComplete='off' defaultValue='default value' />
      </div>
    );
  }
}

server.js

var path = require('path');
var express = require('express');
var webpack = require('webpack');
var config = require('./webpack.config.dev');

var HOST = 'localhost';
var PORT = 3000;

var app = express();
var compiler = webpack(config);

app.use(require('webpack-dev-middleware')(compiler, {
  noInfo: true,
  publicPath: config.output.publicPath
}));


app.get('*', function(req, res) {
  res.sendFile(path.join(__dirname, '/app/index.html'));
});

app.listen(PORT, HOST, function(err) {
  if (err) {
    console.log(err);
    return;
  }

  console.log('Listening at http://' + HOST + ':' + PORT);
});

and webpack.config.js

var path = require('path');
var webpack = require('webpack');

module.exports = {
  devtool: 'eval',
  entry: [
    'webpack-hot-middleware/client',
    './app/index'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static/'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ],
  module: {
    loaders: [{
      test: /\.js$/,
      loaders: ['babel'],
      include: path.join(__dirname, 'app')
    }]
  }
};

Answer

Terry Sahaidak picture Terry Sahaidak · Oct 21, 2015

In webpack gitter chat @bebraw answered to my question:

Codemirror works with webpack but it takes some extra setup. you need to bring some css etc. for it to render. example