Remove references to globals through `this` in AMD factories

Fixes #17829

(cherry picked from commit fa4028b647e38d4f1cc183e6db65022f80cb5423)
Port Node.js loader plugin fixes from d2c

Fixes #18235, #18236.

(cherry picked from commit c58335c35155178b316953ec07d06d1fb8b16e54)