Last active
July 24, 2021 17:16
-
-
Save virtueer/584c9d4662e36932697b66f954d85659 to your computer and use it in GitHub Desktop.
no need to use module-alias, At calling require, it searches directly from the root folder if there is an '@' at the beginning.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BuiltinModule = require('module'); | |
const path = require('path'); | |
// Guard against poorly mocked module constructors | |
const Module = module.constructor.length > 1 ? module.constructor : BuiltinModule; | |
const realResolveFilename = Module._resolveFilename; | |
Module._resolveFilename = (request, ...args) => { | |
if (request[0] !== '@') { | |
return realResolveFilename.call(this, request, ...args); | |
} | |
const _path = path.join(process.cwd(), request.slice(1)); | |
return realResolveFilename.call(this, _path, ...args); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment