Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V8 enable modules #114

Closed
fasihrana opened this issue May 15, 2019 · 10 comments
Closed

V8 enable modules #114

fasihrana opened this issue May 15, 2019 · 10 comments
Assignees

Comments

@fasihrana
Copy link

fasihrana commented May 15, 2019

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
Copy link
Collaborator

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 ClearScriptLib self-assigned this May 15, 2019
ClearScriptLib added a commit that referenced this issue Aug 1, 2019
…y, GitHub Issue #114); added initial support for CommonJS modules (V8 and JScript); added ScriptEngine.ExecuteDocument and ScriptEngine.EvaluateDocument; added V8ScriptEngine.CompileDocument and V8Runtime.CompileDocument; added support for machine-level deployment (GitHub Issue #117); added Extensions and JavaScriptExtensions to facilitate specific scenarios; added implicit conversion of host method arguments via user-defined operators (GitHub Issue #115); patched a V8 issue that causes occasional script execution failure in 32-bit processes (GitHub Issue #111); fixed accessibility of public methods overriden by internal classes (GitHub Issue #112); updated deployment and API documentation. Tested with V8 7.6.303.28.
@ClearScriptLib
Copy link
Collaborator

ClearScript 5.6 includes basic support for modules.

@Ariohor
Copy link

Ariohor commented Sep 20, 2019

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
Copy link
Collaborator

Hi @Ariohor,

Take a look at the test method here.

Good luck!

@Ariohor
Copy link

Ariohor commented Sep 23, 2019

Привет @Ariohor ,

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

Удачи!

Thank you very much!

@karimi
Copy link

karimi commented Jul 24, 2020

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
Copy link
Collaborator

Hi @karimi,

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

Please let us know if you need additional information.

Good luck!

@karimi
Copy link

karimi commented Jul 24, 2020

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
Copy link

pp2110 commented Mar 4, 2024

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants