-
Notifications
You must be signed in to change notification settings - Fork 47
/
Custom module resolver.cs
54 lines (48 loc) · 1.5 KB
/
Custom module resolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExamplesFramework;
using NiL.JS;
namespace Examples.Using_modules
{
[Level(1)]
public sealed class Custom_module_resolver : Example
{
public override void Run()
{
var mainModule = new Module("fakedir/superscript.js", @"
import * as Consts from ""somelib/consts""
console.log(`PI equals ${Consts.Pi}`);
console.log(`E equals ${Consts.E}`);
console.log(`Gravitational acceleration on Earth approximately ${Consts.g} m/s^2`);
");
mainModule.ModuleResolversChain.Add(new MyTestModuleResolver());
mainModule.Run();
}
public sealed class MyTestModuleResolver : CachedModuleResolverBase
{
public override bool TryGetModule(ModuleRequest moduleRequest, out Module result)
{
if (moduleRequest.AbsolutePath == "/math consts.js")
{
result = new Module(moduleRequest.AbsolutePath, @"
export const Pi = Math.PI, E = Math.E;
");
return true;
}
else if (moduleRequest.AbsolutePath == "/fakedir/somelib/consts.js")
{
result = new Module(moduleRequest.AbsolutePath, @"
export * from ""/math consts.js"";
export const g = 9.8;
");
return true;
}
result = null;
return false;
}
}
}
}