-
-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WebPack + CodeMirror + LoadMode #4838
Comments
loadMode doesn't really work with static bundlers -- it dynamically loads files on demand using |
Hi @marijnh! Thanks, anyway. If I manage to make it work, I'll just leave a post here and explain how. |
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 |
@hacdias I'm interested in seeing what the entirety (to the extent possible) of what your webpack looks like when it loads codemirror / dependencies. Is that something you're willing to share? I've even looked through GH search, Having quite the struggle finding an up to date webpack config example with CodeMirror. Best |
Hey @tony! I'm using it on this project. It's all open source. |
Way to go posting that 👍 @hacdias |
@hacdias Have you solved this problem? i also run into this issue. |
Hey @Jocs! I used the function above ^^ |
@hacdias 3q |
Thanks @hacdias 👍 |
@hacdias Thank you very much! You saved a little time of my life! |
Hello!
I'm trying to use Codemirror with LoadMode alongside with WebPack. I thought about having something like:
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:
This is my import section of the file:
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!
The text was updated successfully, but these errors were encountered: