-
Notifications
You must be signed in to change notification settings - Fork 35
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
Highlight.js library was not imported and cannot read property 'themePath' of undefined #207
Comments
This error is because your project is missing the original package It is weird because it should be installed automatically! |
I don't know why you are importing highlight.pack.js in angular.json, this is not mention anywhere in the docs! can you make a reproduction? |
The HLJS token you are talking about is the HIGHLIGHT_OPTIONS token ? If yes, i use it as a provider in the app.module.ts EDIT @MurhafSousli I am not having the cannot read property themePath of null with the 6.0.0 (everything works fine). But when i switch to the 6.1.0 then the error appears |
Hi @MurhafSousli, sorry to insist but the 6.0.0 of your package works like a charm whereas the 6.1.0 and above are not working anymore with the 'cannot read property themePath of null' error :( Regards, |
I had this issue too. I think this is related to lazy-loaded modules importing this one. In fact, application lazy loaded modules provide the options in their own scope, and because
ngx-highlightjs/projects/ngx-highlightjs/src/lib/highlight.service.ts Lines 14 to 17 in c1229aa
Cloning this repo and changing Maybe a static |
@GerkinDev Sure thing! the injection token should be used in the root module only! but the module can be imported at child module level. That's why I didn't introduce Adding |
Yeah I get your point, but in my case, I use this module in my dev tools, and I'd like to not ship it at all in the main bundle. Thus, including it in the root module is not an option for me. |
@GerkinDev You are not shipping it using the injection token! you are only passing a function that returns a promise which will load the library. so it won't be included in your main script. Can you show that it is increasing it? |
It must. The injection token declaration, along with the configured value & imports declaration processed by webpack ARE in the root module output. This is not even close to the size of the full lib, I know. But still, there is a hard reference to multiple files, including highlightjs & your module, anyway. Tree shaking should make it as small as possible, just a few bytes, but a few bytes used by 0.01% of users isn't really worth it. Moreover, if your module is shipped conditionally (in dev environment only for example), constraining its declaration in the root module can be troublesome, though I suppose there are handy ways to come around this. Still. I'll go with my fork changing the providers scope. |
@GerkinDev When webpack process the import script, it will only include that script in the dist directory, but won't merge it in the main script, meaning that the script won't slow the app launch. it will be loaded separately on parallel with the loader service. If you are referring to the few bytes reserved for the config value the user pass, like the script string path, that is does not worth lazy loading in my opinion. But anyway, are you proposing to remove the |
Are there any real downside you expect, that I should be aware of to consider declaring providers is a necessity ? |
Not really! the only thing that matters is that it should not load the script twice if the highlight module is being used in different lazy modules. that's why I choice a singleton service. |
Well, Highlight.js is not exported via UMD globals, yet, I see in your loader service that you expose it as global if using line numbers plugin. Maybe this could be used also to check if the lib was already loaded (& eventually supports loading via a CDN). Or maybe another service could be globally used without any dependency, that serves just as a memo for configured loaders. Dunno. And anyway, in most case, if the same JS asset is served twice, a cached version would be used, reducing the loading time to 0-ish |
I don't see dependencies in the current loader service! The line number script requires hljs to be defined in the global variables. otherwise it will not work (you can have a look at their repo). While I am not against the idea of removing the loader service from root, loading the script twice sounds fundamentally wrong to me (even if it is cached). but anyway, the function In case of we want to change it, we should have a clear alternative design that execute the code only once |
I was talking about those, that force the loader to have ngx-highlightjs/projects/ngx-highlightjs/src/lib/highlight.loader.ts Lines 22 to 24 in c1229aa
|
Repro steps
Install ngx-hightlightjs 6.1.0
Add the following to app.module.ts:
Console logs
Debug mode :
Environment
The text was updated successfully, but these errors were encountered: