CodeMirror不适用于React / Webpack

时间:2015-10-21 10:56:20

标签: javascript reactjs webpack codemirror

我正在为我的反应项目开发一些降价编辑器。 我想使用CodeMirror作为代码编辑器,但是当我使用webpack构建它时它似乎无法工作。

如果说实话,CodeMirror在DOM树中,textArea是隐藏的,但我看到的是:

enter image description here

enter image description here

UPD:相同的代码在codepen上完美运行。我想这是webpack的一个问题。

一些代码:

index.js

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

import {Editor} from './components';

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

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

组件/ 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);
});

和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')
    }]
  }
};

1 个答案:

答案 0 :(得分:8)

webpack gitter chat @bebraw回答了我的问题:

  

Codemirror与webpack一起使用,但需要一些额外的设置。你需要   为它渲染一些css等。 example