How to guard against Angular 9 AOT

30 views Asked by At

I wrote a simple relativize function but since Angular 9 uses an AOT compiler, the following code no longer compiles:

var absolute = function(loc) {
    if (typeof window !== 'undefined' && typeof document.createElement === 'function') {
        var a = document.createElement("a");
        a.href = loc;
        return a.href;
    } else if (typeof exports === 'object') {
        //node.js
        require('path').resolve(loc);  // Angular's AOT gets stuck here
    }
    return loc;
};

What seems to be happening is that the AOT now jumps into the node.js part of the function, but that's not really what I want. For now, npm -i path temporarily resolves the AOT compilation, but I'd rather not do that, since the code will only be executed by the browser for the use-case that's failing.

It's the only function that fails.

Is there a way to tell the library to skip this specific function for AOT, or perhaps a way to detect that the AOT is the executor of the code so we can guard against it at AOT compile time?

0

There are 0 answers