Answer the question
In order to leave comments, you need to log in
How to load all modules from a folder in TypeScript?
Hello. There is a Jodit
project. I am
currently rewriting it to ES16, but after studying TypeScript I decided to switch to it right away.
Actually the task: in addition to the usual modules on which the editor itself works, plugins are also connected to it.
Plugins hang events, and work only through them. They do not climb into the API and do not affect it in any way. They may or may not be; this will not break the editor.
Plugins are simply thrown into the right folder and the order of their connection is not important.
Respectively there is a folder with plugins. Previously, in babel, I included all the files from it in index.js using this construction
let requireAll = (r) => {
r.keys().forEach(r);
}
requireAll(require.context('./plugins/', true, /\.js$/));
Answer the question
In order to leave comments, you need to log in
Valery Chupurnov @skoder
Once again, he asked the question himself, he answered it himself:
The design indicated above works quite well. The only thing that TypeScript doesn't know about the context method of require
Replace the type with any, and everything works:
declare var require: any;
let requireAll = (r) => {
r.keys().forEach(r);
}
requireAll(require.context('./plugins/', true, /\.ts$/));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question