Giter VIP home page Giter VIP logo

Comments (9)

kirbysayshi avatar kirbysayshi commented on August 27, 2024

So, vash itself will run in any environment that provides ES5 compatibility. But what you're running into is that the layout helpers need a way to load a file. And to do that they assume that either:

  • the fs and path modules are available to read files from the local filesystem and then cache it in vash.helpers.tplcache
  • vash.helpers.tplcache[TEMPLATEPATH] contains a compiled template (this is how it works in a browser)

So if you want to use another environment, you'll need to either:

  • prepopulate vash.helpers.tplcache with every template you'll ever need (I recommend this)
  • replace vash.loadFile with a method that can load files in your environment

from vash.

UweKeim avatar UweKeim commented on August 27, 2024

Thanks, Andrew.

Since I actually do provide replacements for fs and path (at least I think so) and still see the error "Cannot call method 'normalize' of undefined" on a path.normalize call, I'm currently trying to find the reason.

Can it be that vash creates kind of "context" when processing a template that makes my global replacements for fs and path somehow disappear? Can this be related to your solution for #37 where you show me how to make the require available to templates?

from vash.

kirbysayshi avatar kirbysayshi commented on August 27, 2024

How are you providing the replacements? Vash calls var path = require(β€˜path’), so if you’re just providing it as a global it’s going to
be overridden if the require call returns nothing.

Yip yip,
~ Drew*

On Tue, Apr 29, 2014 at 12:30 AM, Uwe Keim [email protected] wrote:

Thanks, Andrew.

Since I actually do provide replacements for fs and path (at least I
think so) and still see the error "Cannot call method 'normalize' of
undefined" on a path.normalize call, I'm currently trying to find the
reason.

Can it be that vash creates kind of "context" when processing a template
that makes my global replacements for fs and path somehow disappear? Can
this be related to your solution for #37https://github.com/kirbysayshi/vash/issues/37where you show me how to make the
require available to templates?

β€”
Reply to this email directly or view it on GitHubhttps://github.com//issues/39#issuecomment-41640753
.

from vash.

UweKeim avatar UweKeim commented on August 27, 2024

That did it! πŸ˜„

I'm now putting the following snippet at the very first line before loading the "vash.js" content:

function require(name) {
    return host.require(name);
}

With host being my C#-provided mockup object that I add to the engine via:

_hf = new HostFunctions();
_engine.AddHostObject(@"host", HostItemFlags.GlobalMembers, _hf);

And in turn implementing HostFunctions like this:

public class HostFunctions
{
    public object require(string name)
    {
        switch (name)
        {
            case "fs":
                return new MockFs();
            case "path":
                return new MockPath();
            default:
                throw new Exception();
        }
    }
}

I'm not yet through this completely, hope it's OK to ask a further question below.

from vash.

UweKeim avatar UweKeim commented on August 27, 2024

Last thing (hopefully):

I had to modify your sources. At 2 occurrances, I had to replace:

window['vash'] = vash

with

vashHolder = vash

That I defined before loading the "vash.js" content as:

var vashHolder = {};

Then I could call Vash with:

...
var tpl = vashHolder.compile(content);
...

The reason I had to do this was that the environment checks for AMD, NODSJS and BROWSER used the BROWSER case as a fallback which does not apply to my case.

My question

Is there a way to use Vash somehow like above without modifying your sources?

from vash.

meandmycode avatar meandmycode commented on August 27, 2024

Really interesting runtime, I'd not seen ClearScript before.

I'll add my 2c to this, the hoops you are jumping through at the moment are that ClearScript allows you to execute JavaScript and bind between .NET, but ClearScript doesn't support CommonJS (which is probably a good choice to make at a low level).

Vash determines the exporting type by trying to feature detect functionality of each distinct module syntax, for Node.js style (CommonJS) vash checks to see if 'module' is an object and that is has an exports property (see https://github.com/kirbysayshi/vash/blob/master/build/vash.js#L17).

You have a few options, the easiest is probably to define 'window' as a global, vash will then fallback and export itself to window.

Alternatively you can aim to make ClearScript support CommonJS, this may have already been done before, but if not you'll want to define a global called 'module' that has an exports property that is an object.

After vash is has executed, the module.exports property will be populated.

Another thing that may help from the vash source side is not to assume the global object is 'window', but to export to 'this' from a function being invoked at the global level: http://stackoverflow.com/questions/7290086/javascript-use-strict-and-nicks-find-global-function

from vash.

UweKeim avatar UweKeim commented on August 27, 2024

Thanks, @meandmycode I first tried to mock a window object but most likely I did something wrong on this, as I did not manage to make it working fully.

In addition the require calls are only executed when window is not defined, so this does not seem to be a solution for my case.

from vash.

UweKeim avatar UweKeim commented on August 27, 2024

So my final question remains:

Is there a way to use Vash inside ClearScript (as described above) without modifying the sources of Vash?

from vash.

kirbysayshi avatar kirbysayshi commented on August 27, 2024

@UweKeim, @meandmycode is right, the easiest thing for you to do is to just define a window as a global within your execution environment. I had never heard of ClearScript before today, but this was recommended via another thread:

_engine.Execute("window = this");

That should at least let vash export itself properly.

from vash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.