-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Is it ok to reuse Prism object when loading multiple languages? #1393
Comments
Hi, can you provide a full runnable snippet to illustrate the issue please? I'm not sure I get it. |
|
Oh, OK I see now. The JS components adds support for JS in Markup only if the Markup component is loaded before the JS component is. Hence, when you first load the JS component, the Markup component is not loaded and the support is not added. When you then load Markup + JS, it too late because of |
Basic idea is this: delete require.cache[require.resolve(pathToLanguage)] Would make a Good First Issue™. |
Should we do this on every call? Or should we provide a second parameter to force the cache invalidation? |
If its done that way, will previously loaded languages still work? |
Yes, they should. I don't think reloading a component can break any previously loaded component. |
I don't think it's that inefficient. We're not loading that many files. Users also probably shouldn't be loading the same language multiple times anyway. Maybe document all of this and just invalidate every time? Adding a second parameter seems like the type of thing people will just throw in when things aren't working. |
Alright, agreed. |
Actually, I am wrong about this. Consider we do implement the cache clearing, and a user wants to highlight JS in HTML: This will work, obviously: loadLanguages(['markup', 'javascript']); This will work too, thanks to the cache being cleared: loadLanguages('javascript'); loadLanguages(['markup', 'javascript']); But this will break, because of the cache being cleared... loadLanguages(['markup', 'javascript']); loadLanguages('markup'); Slightly more subtle, this won't work either, because loadLanguages(['markup', 'javascript']); loadLanguages('markdown'); I'm really reconsidering the idea of the second parameter... |
Wow, nice catch. If we wanted to still avoid said second parameter, we'd have to resolve them in the loadLanguages(['markup', 'javascript']); loadLanguages('markup'); We'd have to reload Is that "optional dependency" made explicit anywhere? We could add optional dependencies to It's not ideal, from an implementation perspective, but I'd much rather pull that complexity into Prism rather than expecting users to understand whether or not they should be invalidating the cache. Understanding whether to do so requires users to understand Prism's internals, which I'd like to avoid even at the expense of making |
Ok, I dug into this. Here is the list of the "optional dependencies" I've come up with:
(*** This is a weird one: OpenCL extends C++ which extends C, but it also add keywords to both languages. Apparently this was done so that C/C++ code blocks could use the OpenCL host API and it would be properly highlighted. The thing that worries me the most here is the name As you can see, this is not such a long list. Yet, it may have huge impacts on the loading order. One of the worst case scenario I've found is: loadLanguages(['actionscript', 'http', 'textile', 'django', 'haml', 'css', 'javascript', 'markup', 'xeora']); which will lead to the following files being actually required: Click to expand the "graph".
(That's 50 files Obviously, calling Am I overthinking an edge case? |
I don't know if that's a big deal for node applications. A standard Are people going to be re-require'ing language definitions often enough for this to be a problem? I'm inclined to think the surprise of it not working as expected and needing to dig into why would be worse than tracking down why so many files are loaded. That's also an easier problem to solve: load up all the required languages up front, then you don't get slowdown while your app is running. Edit: That said, I'm not writing a lot of node applications, so I don't have a lot of experience here to lean on. |
Loading all languages is Previously I was calling loadLanguages('language') for every document the user opened, so it would be worse for my current situation if that got slower. One time slow or multiple times fast, but not multiple times slow 👍 Maybe when the node global issue is fixed, you can just keep a dictionary of languages and prism objects, and not worry about them conflicting since each object is independent. |
Hey
I noticed that if I do:
loadLanguages(["javascript"]);
and then later
loadLanguages(["markup", "javascript"]);
Then the second one won't parse the javascript. However if
["markup", "javascript"]
is loaded first it works correctly.The text was updated successfully, but these errors were encountered: