Faking this and require (and similar) #538
-
When in ModuleCategory.Standard, there is no this reference. How can I fake one? When in ModuleCategory.CommonJS, how can override things like require so they do what i want them to do? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Hello @MikeCheel,
We aren't aware of a way to do that without modifying V8. Per the JavaScript language specification, V8 executes standard modules in a mode that forces
JavaScript code that depends on
You can't override the Good luck! |
Beta Was this translation helpful? Give feedback.
-
My scenario is I was hoping to get a third party script called bluebutton.js to run in ClearScript. I've tried various ways of getting it to run from Browserifying it to installing via node and then pulling the js files. I've tried running as Script, Standard and CommonJS. I get real close but theres always a hitch. If I run it as standard module it, wants access reference this and document which isn't there (and DOMParser which I was able to get going after a number of tries). If I run under CommonJS then it wants to require xmldom and it can't find the implementation I got working (which is why I was asking how I can override require). If I run under Script, it doesn't like to combine Script with other categories. How can I run BlueButton under ClearScript? https://github.com/blue-button/bluebutton.js/releases/tag/0.4.2 |
Beta Was this translation helpful? Give feedback.
-
Hi @MikeCheel, A quick look at However, this particular library only depends on First, let's set up the basics: using var engine = new V8ScriptEngine();
engine.AddHostType(typeof(Console)); Next, let's bring in var path = Path.Combine(Directory.GetCurrentDirectory(), @"node_modules\xmldom\lib\dom-parser.js");
var info = new DocumentInfo(new Uri(path)) { Category = ModuleCategory.CommonJS };
engine.DocumentSettings.AddSystemDocument("xmldom", new StringDocument(info, File.ReadAllText(path))); Note that this doesn't execute any script code; it just configures the engine to fulfill "xmldoc" loading requests via the given in-memory document. Using a Next, let's set up a mock engine.DocumentSettings.AddSystemDocument("ejs", ModuleCategory.CommonJS, "module.exports = undefined");
engine.Script.xml = File.ReadAllText("Sample.xml"); We're now ready to bring in engine.DocumentSettings.AccessFlags = DocumentAccessFlags.EnableAllLoading;
engine.DocumentSettings.SearchPath = "https://github.com/blue-button/bluebutton.js/releases/download/0.4.2";
engine.Execute(new DocumentInfo { Category = ModuleCategory.CommonJS }, "BlueButton = require('bluebutton')"); As you can see, we used a CommonJS module to load engine.Execute(@"
record = BlueButton(xml);
Console.WriteLine('type: ' + record.type);
Console.WriteLine('data: ' + record.data.json());
"); Please send any additional thoughts or questions our way! |
Beta Was this translation helpful? Give feedback.
-
Thank you so much, that totally works. I have a couple of questions please.
I really wish there was a book or other comprehensive guide on ClearScript. |
Beta Was this translation helpful? Give feedback.
Hi @MikeCheel,
A quick look at
bluebutton.js
reveals that it's designed to work in the browser and in Node.js. In addition to the standard JavaScript built-ins, those environments provide extensive APIs, making many such libraries difficult to run in an application scripting environment.However, this particular library only depends on
xmldom
, which has no dependencies, andejs
, which appears to be optional. It's therefore relatively easy to get it working in ClearScript.First, let's set up the basics:
Next, let's bring in
xmldom
. To do that, we can simplynpm install
it in the application directory. One wrinkl…