V
V
Valery Chupurnov2017-04-26 11:49:24
JavaScript
Valery Chupurnov, 2017-04-26 11:49:24

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$/));

When I translated it into TypeScript, this design stopped working.
Please tell me how this can be implemented.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Valery Chupurnov, 2017-04-26
@skoder

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$/));

G
GreatRash, 2017-04-26
@GreatRash

Triple slash.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question