1

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?

tresf
  • 7,103
  • 6
  • 40
  • 101
  • It turns out this specific AOT issue is already handled. `fs`, `path` and others fall victim to this problem and Angular has specific exceptions for them, as explained here: https://stackoverflow.com/a/63427821/3196753. Marked as duplicate of this solution. – tresf Nov 22 '20 at 19:03

0 Answers0