question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebPack + CodeMirror + LoadMode

See original GitHub issue

Hello!

I’m trying to use Codemirror with LoadMode alongside with WebPack. I thought about having something like:

new CopyWebpackPlugin([
{
    from: path.resolve(__dirname, '../node_modules/codemirror/mode/*/*'),
    to: path.join(config.build.assetsSubDirectory, 'js/codemirror/mode/[name]/[name].js')
}

and then have the CodeMirror to auto load the files but I can’t get it to work. When I import ‘loadmodule’ I get this warning and it doesn’t seem to be working:

warning  in ./~/codemirror/addon/mode/loadmode.js
51:6-19 Critical dependency: the request of a dependency is an expression

This is my import section of the file:

import 'codemirror/addons/mode/loadmodule'
import CodeMirror from 'codemirror'

I can use Codemirror and the themes. I’m not just being able to load the modes dynamically and I didn’t want to import them at once.

How do I plug loadmode into CodeMirror?

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
hacdiascommented, Jul 5, 2017

So I created something like a wrapper around CodeMirror and instead of importing CodeMirror directly, I import this file:

// Most of the code from this file comes from:
// https://github.com/codemirror/CodeMirror/blob/master/addon/mode/loadmode.js
import * as CodeMirror from 'codemirror'

// Make CodeMirror available globally so the modes' can register themselves.
window.CodeMirror = CodeMirror

if (!CodeMirror.modeURL) CodeMirror.modeURL = '../mode/%N/%N.js'

var loading = {}

function splitCallback (cont, n) {
  var countDown = n
  return function () {
    if (--countDown === 0) cont()
  }
}

function ensureDeps (mode, cont) {
  var deps = CodeMirror.modes[mode].dependencies
  if (!deps) return cont()
  var missing = []
  for (var i = 0; i < deps.length; ++i) {
    if (!CodeMirror.modes.hasOwnProperty(deps[i])) missing.push(deps[i])
  }
  if (!missing.length) return cont()
  var split = splitCallback(cont, missing.length)
  for (i = 0; i < missing.length; ++i) CodeMirror.requireMode(missing[i], split)
}

CodeMirror.requireMode = function (mode, cont) {
  if (typeof mode !== 'string') mode = mode.name
  if (CodeMirror.modes.hasOwnProperty(mode)) return ensureDeps(mode, cont)
  if (loading.hasOwnProperty(mode)) return loading[mode].push(cont)

  var file = CodeMirror.modeURL.replace(/%N/g, mode)

  var script = document.createElement('script')
  script.src = file
  var others = document.getElementsByTagName('script')[0]
  var list = loading[mode] = [cont]

  CodeMirror.on(script, 'load', function () {
    ensureDeps(mode, function () {
      for (var i = 0; i < list.length; ++i) list[i]()
    })
  })

  others.parentNode.insertBefore(script, others)
}

CodeMirror.autoLoadMode = function (instance, mode) {
  if (CodeMirror.modes.hasOwnProperty(mode)) return

  CodeMirror.requireMode(mode, function () {
    instance.setOption('mode', instance.getOption('mode'))
  })
}

export default CodeMirror
7reactions
hacdiascommented, Aug 1, 2017

Hey @tony! I’m using it on this project. It’s all open source.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebPack + CodeMirror + LoadMode · Issue #4838 - GitHub
Hello! I'm trying to use Codemirror with LoadMode alongside with WebPack. I thought about having something like: new CopyWebpackPlugin([ ...
Read more >
[Help] Running into loader-related issue when trying to use ...
My project is using React, so I am using the codemirror and ... See https://webpack.js.org/concepts#loaders | /* BASICS */ | > .
Read more >
Vue-codemirror-component NPM | npm.io
Check Vue-codemirror-component 1.1.1 package - Last release 1.1.1 at our NPM packages aggregator and ... Install webpack, css-loader and style-loader.
Read more >
Make codemirror-mode-elixir work : UP-8614 - YouTrack
Issue was created from an Upsource discussion in file loadmode-webpack.js in review UP-CR-1062, revision a816d77. Leonid Khachaturov wrote on 10 Mar 2017 ...
Read more >
Add grunt task for building new CodeMirror bundles from ...
Use webpack to build codemirror.js from manifest based on ... and the issue prevents being able to add Modes, even via the included...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found