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.

Add es6 module support for addons/modes

See original GitHub issue

Edit: I found out about the discussion forum a tad to late, feel free to ignore this here šŸ˜“

Hi, Iā€™ve been trying to use CodeMirror inside a WebComponent made with LitElement (demo here)and I somehow managed to make it work but thereā€™s a couple of things I hoped you could evaluate adding to make life easier for people developing on es6 modules like me.

Since the CodeMirror core src folder is on npm and itā€™s already in modules it can be used just fine like that in other modules, but thereā€™s a bit of a problem when trying to use any of the addons/modes provided here;

As far as I can tell, all addons/modes all asume that if youā€™re not using CommonJS/AMD you must be using a global script, but since es6 modules have local environments trying to import the addons/modes as they are right now in an es6 module ends up with a CodeMirror is not defined error, e.g.:

import CodeMirror from "codemirror/src/codemirror.js"; // works fine
import "codemirror/mode/javascript/javascript.js"; //  throws CodeMirror is not defined

For now, Iā€™ve gotten this to work by using a function which fetches the addon/mode and then evals it in the moduleā€™s context but that is far from ideal thinking of the risks and debugging troubles using eval brings, so I was hoping you could release an es6 modules version of the addons/modes, it could be something as simple as changing:

// from this
(function(mod) {
  if (typeof exports == "object" && typeof module == "object") // CommonJS
    mod(require("../../lib/codemirror"));
  else if (typeof define == "function" && define.amd) // AMD
    define(["../../lib/codemirror"], mod);
  else // Plain browser env
    mod(CodeMirror);
})(function(CodeMirror) {
// addon/mode content
})

// to this
exports function addonOrModeName(CodeMirror){
// addon/mode content
}

// so that you can do this
import CodeMirror from "codemirror/src/codemirror.js"; 
import { addModeJavascript } from "codemirror/mode/javascript/javascript-module.js"; // or any name you like, maybe use .mjs?

addModeJavascript(CodeMirror);

If you like this way of doing things I can make a PR for this kind of behavior.

Now that Firefox joined every other major modern browser in supporting es6 modules I think it could be a great time to add this šŸ‘

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:12
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

23reactions
justinfagnanicommented, May 29, 2018

@alangdm thereā€™s a workaround for some modes at least. You can define a module that imports CodeMirror then writes it to a global, and import that before any mode:

codemirror-global.js:

import CodeMirror from "codemirror/src/codemirror.js";
window.CodeMirror = CodeMirror;

app.js:

import './codemirror-global.js';
import CodeMirror from "codemirror/src/codemirror.js";
import "codemirror/mode/javascript/javascript.js";

@marijnh

further complexity and indirection

Wouldnā€™t this reduce complexity and indirection by directly using the web native module system, rather than relying on user-land loaders?

Weā€™re getting a lot of demand for libraries like CodeMirror to be loadable as modules. What can we do to help?

10reactions
marijnhcommented, May 12, 2018

Weā€™re planning a modernization of the codebase in the near future, but until then Iā€™d prefer to keep things stable and not add further complexity and indirection, so this isnā€™t something thatā€™s welcome right now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add es6 module support for addons/modes - CodeMirror
Hi, I've been trying to use CodeMirror inside a WebComponent made with LitElement (demo here)and I somehow managed to make it work butĀ ......
Read more >
Add es6 module support for addons/modes #5403 - GitHub
Hi, I've been trying to use CodeMirror inside a WebComponent made with LitElement (demo here)and I somehow managed to make it work but...
Read more >
ES6 modules in chrome extensions ā€” An introduction - Medium
Let's see now how to add modules support to background, browser action (a.k.a. popup) and ā€œextension pagesā€. All the code in this article...
Read more >
JavaScript modules - MDN Web Docs
This guide gives you all you need to get started with JavaScript module syntax.
Read more >
How to import ES6 modules in content script for Chrome ...
Chrome has support for modules with an asterisk: Only works when your script is a module script. The import statement is synchronous which...
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