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
use rollup to bundle runmode-standalone and runmode-node #6314
Conversation
Thanks for working on this. There's some issues, though...
Do you want to continue to work on this or would you prefer if I just merge your two other PRs and we leave these duplicated? |
You can merge the other two PRs for now since I don't know how long it'll take to resolve the issues brought up; I can continue to look into using rollup to generate the runmode-standalone and runmode.node.
Ah, I think we can use the
I had this in mind, since I saw that the
Moving it to |
Reverted the change within
I believe this is fixed by taking all the methods attached to runmode-node democonst CodeMirror = require("./addon/runmode/runmode.node.js");
require("./mode/xml/xml");
// Log CodeMirror instance to visually check methods.
console.log(CodeMirror);
// Example code from mode runner demo.
var src = `
<foobar>
<blah>Enter your xml here and press the button below to display it as highlighted by the CodeMirror XML mode</blah>
<tag2 foo="2" bar=""bar""/>
</foobar>
`
// Node.js doesn't have dom so we
var pre = `<pre class="cm-s-default">\n`;
// Call runMode method on example code.
CodeMirror.runMode(src, "xml", (token, tokenClass) => {
pre = `${pre}<span class="cm-${tokenClass}"\>${token}</span>\n`
});
pre = `${pre}</pre>\n`;
// Log the syntax highlighted xml code.
console.log(pre); runmode demo logs{
StringStream: [Function: StringStream],
modes: { null: [Function (anonymous)], xml: [Function (anonymous)] },
mimeModes: {
'text/plain': 'null',
'text/xml': 'xml',
'application/xml': 'xml',
'text/html': { name: 'xml', htmlMode: true }
},
defineMode: [Function: defineMode],
defineMIME: [Function: defineMIME],
resolveMode: [Function: resolveMode],
getMode: [Function: getMode],
modeExtensions: {},
extendMode: [Function: extendMode],
copyState: [Function: copyState],
innerMode: [Function: innerMode],
startState: [Function: startState],
registerGlobalHelper: [Function: min],
registerHelper: [Function: min],
splitLines: [Function (anonymous)],
defaults: { indentUnit: 2 },
runMode: [Function (anonymous)]
}
<pre class="cm-s-default">
<span class="cm-undefined">
</span>
<span class="cm-tag bracket"><</span>
<span class="cm-tag">foobar</span>
<span class="cm-tag bracket">></span>
<span class="cm-undefined">
</span>
<span class="cm-null"> </span>
<span class="cm-tag bracket"><</span>
<span class="cm-tag">blah</span>
<span class="cm-tag bracket">></span>
<span class="cm-null">Enter your xml here and press the button below to display it as highlighted by the CodeMirror XML mode</span>
<span class="cm-tag bracket"></</span>
<span class="cm-tag">blah</span>
<span class="cm-tag bracket">></span>
<span class="cm-undefined">
</span>
<span class="cm-null"> </span>
<span class="cm-tag bracket"><</span>
<span class="cm-tag">tag2</span>
<span class="cm-null"> </span>
<span class="cm-attribute">foo</span>
<span class="cm-null">=</span>
<span class="cm-string">"2"</span>
<span class="cm-null"> </span>
<span class="cm-attribute">bar</span>
<span class="cm-null">=</span>
<span class="cm-string">""bar""</span>
<span class="cm-tag bracket">/></span>
<span class="cm-undefined">
</span>
<span class="cm-tag bracket"></</span>
<span class="cm-tag">foobar</span>
<span class="cm-tag bracket">></span>
<span class="cm-undefined">
</span>
</pre>
Fixed by adding built runmode files to
I initially tried to use the
I initially tried this approach but rollup currently doesn't support custom UMD factory functions, that is there wasn't a clean non-hacky way of replacing the default generated header (function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}((function () { 'use strict'; with the one used the current (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) { So I think appending
Moved to |
I'd be okay with just duplicating the environment detection in the runmode implementation, so that we don't have to mess with the UMD header. I'd much prefer a solution that uses rollup throughout to one that appends files. |
That does work but would require either changing the I think iterating through the imported object and copying it to the
Sorry for the confusion here: we're still using rollup to autogenerate the entire file; it's because |
Yes, maybe we're on the same page already, but what I meant was that I prefer for the file to be |
I tried this approach initially to avoid multiple input files into rollup, but what I found was that regardless of where So even if we had import StringStream from "../../util/StringStream.js"
import * as modeMethods from "../../modes.js"
CodeMirror = {}
import "./addon/runmode/runmode.js" rollup would produce // imported StringStream and modeMethods
// immediately-invoked function requiring CodeMirror here
CodeMirror = {} As a result, the function would error because CodeMirror hasn't been defined yet. The multi-entry plugin on the other-hand respects the order of input files, so it works. The // runmode-CodeMirror.js, creates the minimal CodeMirror needed for runmode
import StringStream from "../../util/StringStream.js"
import * as modeMethods from "../../modes.js"
CodeMirror = ...
export default CodeMirror // single rollup entry point (wrapper)
import CodeMirror from "./runmode-CodeMirror.js"
import "./addon/runmode/runmode.js" These two files produce the same bundled result that the multi-entry plugin creates. Which do you prefer? Edit: The import wrapper way is included in the commit below. |
4094960
to
2f1b789
Compare
This PR changes the runmode addon to use Rollup to generate runmode-standalone and runmode-node, keeping methods always up to date with
runmode.js
,src/modes
,src/utils/misc.js
and/src/utils/StringStream.js
. This solves #6311 and #6313 with a more programmatic approach. To generate the two files, we runnpm run build
since I modified the mainrollup.config.js
.Two notes:
rollup.config.js
file)addon/runmode
file to generate therunMode
methods is that the following is included in the two files. We could get rid of this by creating a separate file that 'mirrors' therunMode
method and which doesn't have this header. I wouldn't think this header causes issues though, since this is included in every language inmode/[mode]/[mode].js
.