microsoft / ClearScript

A library for adding scripting to .NET applications. Supports V8 (Windows, Linux, macOS) and JScript/VBScript (Windows).
https://microsoft.github.io/ClearScript/
MIT License
1.74k stars 148 forks source link

V8 enable modules #114

Closed fasihrana closed 5 years ago

fasihrana commented 5 years ago

Is there a way to enable "import <module>.js;" statements in a V8 Runtime?

I couldn't find any examples and makes me think I'd have to parse the script file myself first to enable this.

As on StackOverflow.

ClearScriptLib commented 5 years ago

Hi @fasihrana,

ClearScript currently doesn't support ES6 modules. We're evaluating adding support for both ES6 and Node.js-style modules in an upcoming release.

Thanks!

ClearScriptLib commented 5 years ago

ClearScript 5.6 includes basic support for modules.

Ariohor commented 4 years ago

ClearScript 5.6 includes basic support for modules.

Can you please show an example how to enable import/export in ClearScript? We try to use modules but allways return one error "SyntaxError: Unexpected identifier" in code

import ScriptClass1 from "ScriptClass1.js";

ClearScriptLib commented 4 years ago

Hi @Ariohor,

Take a look at the test method here.

Good luck!

Ariohor commented 4 years ago

Привет @Ariohor ,

Посмотрите на метод испытаний здесь .

Удачи!

Thank you very much!

karimi commented 4 years ago

I have a follow up question on this, What's the best way to import modules installed using NPM in visual studio? I have a package.json file that installs the dependencies in a node_modules folder.

ClearScriptLib commented 4 years ago

Hi @karimi,

You can use DocumentSettings.SearchPath to specify your module locations.

Please let us know if you need additional information.

Good luck!

karimi commented 4 years ago

Thanks, that's what I was looking for. I also had to have a build event that copies the node_modules to my build folder.

pp2110 commented 6 months ago

I have a question, I have this C# code

public JsonResult CreatePPT(string JSONToPass, string VisualInfo) {

var jsonObject = JsonConvert.DeserializeObject<JSONModel>(JSONToPass);
using (var engine = new V8ScriptEngine())
{
    // Load the JavaScript script from a file
    var script = System.IO.File.ReadAllText("C:\\Users\\PrathmeshHanamantPaw\\Downloads\\GlobalReportFramework\\GlobalReportFramework - Copy\\GlobalReportFramework\\createPPT.js");
    // Set the values of connectionString, containerName, and outputPath in the JavaScript context
    var connectionString = Common.Constants.blobConnectionString;
    var containerName = jsonObject.Guid;
    var outputPath = Common.Constants.sessionFolder + Common.Constants.updatedPPTFileNamePath + jsonObject.Guid;
    engine.Script.connectionString = connectionString;
    engine.Script.containerName = containerName;
    engine.Script.outputPath = outputPath;
    // Execute the JavaScript code
    engine.Execute(script);
    engine.Script.createPresentation();
}
return null;

}

the JS script doesn't run returns an Error : "require is not defined"

NOTE: I have used require to use installed npm packages

ClearScriptLib commented 6 months ago

Hi @pp2110,

The require function is available only within CommonJS modules. You can use ClearScript to execute a CommonJS module as follows:

var info = new DocumentInfo { Category = ModuleCategory.CommonJS };
engine.Execute(info, script);

Note however that ClearScript provides a bare JavaScript environment containing only the JavaScript built-ins It does not implement the Web or Node.js APIs.