Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#.Net ClearsScript V8 enable modules

Is there a way to enable "import <module>.js;" statements in an embedded V8 Runtime using Microsoft ClearScript?

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

like image 355
fasih.rana Avatar asked Jan 21 '26 03:01

fasih.rana


1 Answers

You could also use a roll-your-own require() function similar to node.js. I wrote a blog article here and there's a working CodePen to illustrate the approach which was inspired by an article by Michele Nasti which was itself based on the ideas in chapter 10 of the book Eloquent JavaScript by Marijn Haverbeke.

Here is my coded example:

const myCode1 = `
  let utils = function (){
    this.say = function(x){
      log('utils: says = ' + x)
      };
      return this;
  }
  exports.utils = utils;
`;
const myCode2 = `
  let customer = function (){
    this.say = function(x){
      log('Customer: says = ' + x)
      };
      return this;
  }
  exports.customer = customer;
`;
/*

*/  
// I am loading simple source code strings into an array - in the real solution
// you would use some kind of source code fetch / load to string process.
let sourceCode = {c1: myCode1, c2: myCode2};
myRequire.cache = Object.create(null); 

function myRequire(name) {   
    log(`myRequire: You require file ${name}`)
    if (!(name in myRequire.cache)) {
        log(`myRequire: ${name} is not in cache; reading from disk`)
        let code = sourceCode[name]; // load the code - this is bespoke to your env      
        let module = {exports: {}};
        myRequire.cache[name] = module;     
        let wrapper = Function("require, exports, module", code);     
        wrapper(myRequire, module.exports, module);
    }
    log(`myRequire: ${name} is in cache. Returning it...`)
    return myRequire.cache[name].exports;
}

// myRequire() produces an object from the 'exports' object in the loaded code.
//let myExports = new myRequire('c1');
// we can then refer to these as 
// let util = myExports.utils();
// or just use 
// let util = new myRequire('c1').utils();

// So...Require c1 will create an object with exports.
let util = new myRequire('c1').utils();
util.say('I am alive!')
 
log("");

// Require c2 will create an object with exports.
let cust = new myRequire('c2').customer();
cust.say('I am alive too!')

function log(msg){
  $('#log').html($('#log').html() + "<br />" + msg);
}

The output is

myRequire: You require file c1
myRequire: c1 is not in cache; reading from disk
myRequire: c1 is in cache. Returning it...
utils: says = I am alive!

myRequire: You require file c2
myRequire: c2 is not in cache; reading from disk
myRequire: c2 is in cache. Returning it...
Customer: says = I am alive too!
like image 131
Vanquished Wombat Avatar answered Jan 22 '26 15:01

Vanquished Wombat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!