Ejemplo n.º 1
0
        public ChakraCoreTypeScriptTranspiler()
        {
            using var stream = typeof(ChakraCoreTypeScriptTranspiler).Assembly.GetManifestResourceStream(typeof(V8TypeScriptTranspiler), "typescriptServices.js");
            using var reader = new StreamReader(stream);
            var typescriptServicesSource = reader.ReadToEnd();

            _runtime = JavaScriptRuntime.Create(JavaScriptRuntimeAttributes.None);
            _context = JavaScriptContext.CreateContext(_runtime);

            // Install the compiler in the context
            using (var scope = _context.GetScope())
            {
                JavaScriptContext.RunScript(typescriptServicesSource);
            }
        }
Ejemplo n.º 2
0
        public string Transpile(string input)
        {
            lock (this)
            {
                using (var scope = _context.GetScope())
                {
                    var transpileFunction = JavaScriptContext.RunScript(@"
input => {
    const result = ts.transpileModule(input, {
        compilerOptions: {
            target: ts.ScriptTarget.ES2015,
            module: ts.ModuleKind.ES2015,
            lib: [ 'ES2015' ]
        }
    });
    return result.outputText;
}");

                    var outputText = transpileFunction.CallFunction(JavaScriptValue.GlobalObject, JavaScriptValue.FromString(input));

                    return(outputText.ToString());
                }
            }
        }
Ejemplo n.º 3
0
        public void AddPreload(string specifier, string code)
        {
            if (!RootModule.IsValid)
            {
                throw new InvalidOperationException("No root module set");
            }

            var module = JavaScriptModuleRecord.Initialize(RootModule, specifier);

            module.HostUrl = specifier; // Only for debugging
            _moduleLeases.Add(specifier, new ModuleLease(module));

            _dispatcher.Invoke(() =>
            {
                using (_context.GetScope())
                {
                    module.ParseSource(code);
                }
            });
        }