-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Description
Hi all,
I report here an issue that is somehow related to #4838 and to other similar open questions on various forums. I found the problem when using webpack, but I think the issue also comes when simply using import. There is no « bug » to fix in the code distribution, but I think something could be added in the installation page of the (awesome) CodeMirror manual, which covers many importation schemes but not import explicitly.
Here is the story (with suitable webpack & babel configuration):
- Install CodeMirror with
yarn add codemirror(or using npm). - Using
import CodeMirror from 'codemirror';works as expected. Good! - Adds
import 'codemirror/theme/<myTheme>.css';for your favorite theme. Very Good! - Adds
import 'codemirror/mode/javascript/javascript.js';seems to Work (no error in console)... - However, selecting the mode does nothing. Worst:
CodeMirror.modesreveals to be empty! - Change step 2 with:
import CodeMirror from 'codemirror/lib/codemirror.js';then every thing works magically. Yeah!
Remark: this simple fix in step #6 above also works with electron + webpack + react + react-hot-loader, contrarily with negative comments found here and there on the web.
Explanation: after deep debugging, it appears that when using import CodeMirror from 'codemirror', the adds-on and modes register themselves in an instance of CodeMirror that is different from the imported one.
Actually, all CodeMirror adds-on and modes are using relative paths to their parent '../lib/codemirror.js' module. However, it seems that the module cache (whoever is responsible for it) is confused and does not identify '<cm>' with '<cm>/lib/codemirror.js' even if '<cm>/package.json' properly specifies that the main entry is at ./lib/codemirror.js'.